What is wrong with my perl script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What is wrong with my perl script?
# 1  
Old 12-22-2011
What is wrong with my perl script?

i managed to write a perl script for nagios. now, the script works. but i think there's somethign wrong with the exit codes. because the script doesn't show the output of the results in nagios, it instead shows null.

please tell me what i'm doing wrong here:

Code:
#!/usr/local/bin/perl

use STUN::Client;
use Data::Dumper;
use Getopt::Long;
use warnings;
use strict;

my %ERRORS = ( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 );

my $timeout = 30;

$SIG{ ALRM } = sub {
   print "CRITICAL: Timeout expired (${timeout}s) before server responded.";
   exit $ERRORS{ CRITICAL };
};

my $server = $ARGV[0];

my $userip = $ARGV[1];

my $stun_client = STUN::Client->new;

$stun_client->stun_server("$userip");

my $r = $stun_client->get;

if ($r eq '')
{
   print "CRITICAL:  Did not receive a response from the device = [ $server, $userip ]\n";
   exit $ERRORS{CRITICAL};
}

my $ip = $r->{ma_address};

my $transid = $r->{transaction_id};

my $portreturned = $r->{ma_port};

# Split the IP address into an array of octets

my @x = split /\./, $ip;
 
# Print it unless it is invalid


if (($#x != 3 or grep {$_ =~ /\D/ or $_ < 0 or $_ > 255} @x) && ($transid eq '') && ($portreturned eq ''))

{

   print "CRITICAL:  IP returned is not a public IP. Response from query  ==  IP [ $ip ], TranID [ $transid ], Port [ $portreturned ].\n";
   exit $ERRORS{CRITICAL};
}

else

{

   print "OK: Response received from device [ $server ] is sufficienct  ==  IP [ $ip ], TranID [ $transid ], Port [ $portreturned ].\n";
   exit $ERRORS{OK};
}

# 2  
Old 12-22-2011
Not sure.

But anyway following line misses newline - try with newline.

Code:
    print "CRITICAL: Timeout expired (${timeout}s) before server responded.";

# 3  
Old 12-22-2011
Quote:
Originally Posted by thegeek
Not sure.

But anyway following line misses newline - try with newline.

Code:
    print "CRITICAL: Timeout expired (${timeout}s) before server responded.";


thanks for responding to me. i tried that. but it's still not passing.

Nagios is a monitoring application that works based off the "exit codes" it receives. it looks like this script isn't sending the proper exit codes. i dont know what i'm missing.

the code seems to be having an issue with the "exit $ERRORS"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing perl script in Linux gives :Exec format error. Wrong Architecture

i have perl script that used to be working great , once i edit it in windows and convert it to UTF-8 and then via FTP return it . also did: chmod +x foo.pl and then when i try to run it : ./foo.pl im getting this error: ./foo.pl: Exec format error. Wrong Architecture.... (4 Replies)
Discussion started by: umen
4 Replies

2. UNIX for Dummies Questions & Answers

Perl find & replace - what am I doing wrong?

Hi! I have a directory full of .plist type files from which I need to delete a line. Not every file contains the line, but of course I'd like to do it recursively. The line which I want to delete is: <string>com.apple.PhotoBooth</string> and looks like this in its native habitat: ... (9 Replies)
Discussion started by: sudon't
9 Replies

3. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

4. Shell Programming and Scripting

Perl...getting wrong output?

Good morning! Im trying to write and learn at the same time a simple script. fTHe script should tell me if a number is odd or even/ #!/usr/bin/perl $num = 10; $string1 = "This number is odd"; $string2 = "This number is even"; if ($num /= 2) { print "$string1\n"; }else{ ... (3 Replies)
Discussion started by: bigben1220
3 Replies

5. Shell Programming and Scripting

wrong output in perl script

Hi, Here is my piece of code-- #!/usr/bin/perl my $Time_Stamp ; my $User_Name; my $Success; my $Failure; my $ErrorCode; my $ErrorMsg; my $logDir = $ARGV; my $logPrefix = $ARGV; die "usage: $0 <logDir> <logPrefix>" unless $logDir and $logPrefix; die "Log dir $logDir doesn't... (2 Replies)
Discussion started by: namishtiwari
2 Replies

6. Shell Programming and Scripting

whats wrong with this line using perl

E:\>perl -00ne 'push @a,"$_\0$ARGV\n";END{print reverse split/\0/ for sort @a}' file1-obj_prof.out.txt file2-obj_prof.out.txt' Can't find string terminator "'" anywhere before EOF at -e line 1. (6 Replies)
Discussion started by: richsark
6 Replies

7. Shell Programming and Scripting

Perl Script issue. What am I doing wrong?

#!/usr/local/bin/perl open (MYFILE, 'logs_report'); while (<MYFILE>) { $rec=$_; chomp ($rec); @arr=split(/ /,$rec); print $rec,"\n" if ($arr!~/OK/); open (MYF, '>data.txt'); print $rec,"\n" if ($arr!~/OK/); close (MYF); (14 Replies)
Discussion started by: SkySmart
14 Replies

8. UNIX for Dummies Questions & Answers

what is wrong with this script?

Hi, I have this example script which gives error ": unexpected operator/operand". I need the '' brackets for operator precedence. #!/bin/ksh x="abc" y="xyz" z="123" if -a then print "yes" else print "no" fi Thanks (2 Replies)
Discussion started by: rs1969
2 Replies

9. Shell Programming and Scripting

What's wrong with this script

I am trying to create a script but it is giving me errors on Cygwin for the following script. Could someone tell me, what am I doing wrong? choice=1000 echo "choice is $choice" while ; do echo "choice is $choice" echo 'Please select your option:' echo '1. Option 1' echo '2. Option 2'... (3 Replies)
Discussion started by: amitg1980
3 Replies

10. Shell Programming and Scripting

perl regexp error , I cant understand what is wrong

perl regexp error , I cant understand what is wrong Hello all I have simple perl regexp that is searching for pattern in string and replace it with the same string + addition string here is what I have : my $rec = q| new Array("Attributes Management" ... (4 Replies)
Discussion started by: umen
4 Replies
Login or Register to Ask a Question