Writing Perl script

Lonny Selinger lonny at bangtherockstogether.net
Wed Apr 2 21:29:10 CEST 2003


When you execute this from the command line are you doing it *as* the user nagios
... or as root?

if thats not the problem ... my initial suspiction is that the command you are
executing (the snmpwalk) *might* not be outputting the way you want perl to return
it (variables being returned to the shell rather then the interpreter ... your in
the shell executing code that uses an interpreter) One thing you can try (although
its not really *propper*) is translating the output of the command by sending the
output to a dead pipe and working with it from there. As an example (from some of my
code):

open (UNAME, "uname -a |");
        while (<UNAME>) {
                $uline = $_;
                @uresults = split (/\s/, $uline);
        if ($uresults[0] ne 'AIX') {
                aix(); exit(-1);
        } else {
                $opt_server = $uresults[1];
        }
}

this block is meant to check the host machine and verify it is running AIX. The
command is sent to an open pipe which allows me to reassign the output (after being
read one line at a time) to variables within the rest of the code. I abuse open
pipes quite often in my code actually  :\

Does this make sense or am I out to lunch?

--
Lonny

> Please look at this simple code.
>
> When I runs it from the commando prompt it works. but nagios doesnt seem to
> recognize the variabels. There is no out put but the "CRITICAL -"
> The compare doesn't come our right when nagios is executing the script, it aleays
> ends up CRITICAL.
>
> In the test.cfg file it is only one row with the ip address I want to compare to.
>
> #!/usr/bin/perl -w
> use strict;
>
> #RFC1213-MIB::ipAdEntAddr.192.168.200.1 = IpAddress: 192.168.200.1
> #RFC1213-MIB::ipAdEntAddr.213.113.141.189 = IpAddress: 213.113.141.189
>
>
>
> use vars qw($DAT $cip $pos $a);
>
> my $cip         = "";
> my $pos         = 0;
> my $a           = "";
> my $DAT         = undef;
> my @tmp         = undef;
> my $ip          = "";
> my $x           = "";
>
> open(DAT, "test.cfg");
> $cip = <DAT>;
> chomp($cip);
> close(DAT);
>
> $a = qx(/usr/bin/snmpwalk -m ALL -v 1 -c public 192.168.200.1
> .1.3.6.1.2.1.4.20.1.1);
>
> @tmp = split(/:/, $a);
> chomp($ip = "$tmp[6]\n");
> chomp($ip);
>
> $ip = substr($ip, 1 ,length($ip)-1);
>
>
> $pos = index($a, $cip);
>
> if ($cip eq $ip) {
>         print "OK - $cip\n";
>         exit 0;
>
> } else  {
>         print "CRITICAL - $cip\n";
>         exit 2;
> }
>
> ----- Original Message -----
> From: "Lonny Selinger" <lonny at bangtherockstogether.net>
> To: <gert.lindstrom at brevik.nu>
> Cc: <nagios-users at lists.sourceforge.net>
> Sent: Tuesday, April 01, 2003 9:50 PM
> Subject: Re: [Nagios-users] Writing Perl script
>
>
>> One thing you can do is predeclare the variable as "undefined" oustide of
> any
>> functions. This is what I normally do when writing perl ... the
> alternative is to
>> declare it at the time of use (while using strict) in each function its
> called in.
>> An example of a pre-declaration might be:
>>
>> my $var = undef;
>>
>> Same goes for explicit declaration withing a function:
>>
>> my $var = $some_var;
>>
>> is this sort of what you're looking for? I'm assuming you're using strict.
>>
>> --
>> Lonny
>>
>>
>> > Can some explain this for a perl newbi ?
>> >
>> > Explicitly initialize each varialable in use. Otherwise with caching
> enabled, the
>> > plugin will not be recompilied each time, and therefore Perl will not
> reinitialize
>> > all the variables. All old variable values will still be in effect.
>> >
>> > I think this is happening for me and i cant figuring out what to do
> about it.
>> >
>> > Thanks
>> >
>> > ---
>> > Gert





-------------------------------------------------------
This SF.net email is sponsored by: ValueWeb: 
Dedicated Hosting for just $79/mo with 500 GB of bandwidth! 
No other company gives more support or power for your dedicated server
http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list