--- /home/raf/check_snmpv2.pl 2012-12-23 13:43:00.000000000 +0100 +++ /usr/lib/nagios/plugins/check_snmpv2.pl 2012-12-23 14:33:19.000000000 +0100 @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/perl -w #Written by Lane Williams #01-10-2012 #Script is used to check values of snmp enabled devices. Yes another one...the others do not do what I like..so I wrote Yet another SNMP check. @@ -6,32 +6,70 @@ #There is no warranty or guarantee this will do what you would like, but you can always add or change it. #Thanks to Stephan Hradek for input on port option and giving the idea which lead to the expanded first line output. +# patched for pragma "use strict" by Rafał Frühling 2012-12-23 + +use strict; use Getopt::Std; use Net::SNMP; -getopts('hH:C:V:O:t:p:'); -if ($opt_h){ - print "\nUSAGE: check_snmp.pl -H hostname -C community -V snmp version -t Title -O label:oid:warn:crit:[(l)less than|(g)greater than]:[(a)alpha|(n)number\n"; +our $opt_h; +our $opt_H; +our $opt_C; +our $opt_V; +our $opt_O; +our $opt_t; +our $opt_p; +our $opt_v; + +getopts('hH:C:V:O:t:p:v'); + +if ($opt_H eq ""){ + print "\nUSAGE: check_snmp.pl [-v] -H hostname -C community -V snmp version -t Title -O label:oid:warn:crit:[(l)less than|(g)greater than]:[(a)alpha|(n)number\n"; + print "Option -v makes the plugin verbose.\n"; print "The -O option must have all of the values in order for it to work.\n"; print "The last 2 options are if the value should be greater than or less than, and/or whether the check output will be numeric or alphabet\n\n"; exit; } -$host=$opt_H; -$community=$opt_C; -$version=$opt_V; -@oids=$opt_O; -$title=$opt_t; -$port=$opt_p; - -$STATE_OK = 0; -$STATE_WARNING = 1; -$STATE_CRITICAL = 2; -$STATE_UNKNOWN = 3; +my $host=$opt_H; +my $community=$opt_C; +my $version=$opt_V; +my @oids=$opt_O; +my $title=$opt_t||""; +my $port=$opt_p||161; +my $verbose = $opt_v; + +my $STATE_OK = 0; +my $STATE_WARNING = 1; +my $STATE_CRITICAL = 2; +my $STATE_UNKNOWN = 3; + +my $crit_exit = 0; +my $warn_exit = 0; + +my @oid2 = (); + +my $an; +my $banner; +my $count; +my $crit; +my $element; +my $error; +my $get; +my $label; +my $obj; +my $oid; +my $perf; +my $session; +my $string; +my $value; +my $v; +my $warn; + +my %results; -$crit_exit = 0; -$warn_exit = 0; #split the input for use + foreach $oid (@oids){ @oid2 = split (',',$oid); foreach $obj (@oid2){ @@ -58,8 +96,8 @@ } #format the first line of output and prime the pump $banner = $title." Status:"; -$string eq ""; -$perf = " | "; +$string = ""; +$perf = ""; $count = 0; foreach $element (sort keys %results){ @@ -129,16 +167,20 @@ if ($crit_exit != 0){ chomp $string; - print "$banner [$count OK]\n$string $perf \n"; +# print "$banner [$count OK]\n$string $perf \n"; + print "CRITICAL $banner" . ($verbose ? ", $count check(s): $string" : " $perf") . "\n"; exit $STATE_CRITICAL; } -elsif($warn_exit != 0){ +if($warn_exit != 0){ chomp $string; - print "$banner [$count OK]\n$string $perf \n"; +# print "$banner [$count OK]\n$string $perf \n"; + print "WARNING $banner" . ($verbose ? ", $count check(s): $string" : " $perf") . "\n"; exit $STATE_WARNING; } -else { chomp $string; - print "$banner [$count OK]\n$string $perf\n"; - exit $STATE_OK; } +chomp $string; +# print "$banner [$count OK]\n$string $perf \n"; +print "OK $banner" . ($verbose ? ", $count check(s): $string" : " $perf") . "\n"; +exit $STATE_OK; +