Writing Perl script

Gert Lindström gert.lindstrom at brevik.nu
Wed Apr 2 20:49:15 CEST 2003


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