1
0
mirror of https://github.com/opinkerfi/nagios-plugins.git synced 2024-11-24 11:23:47 +01:00

Fix snmp session get without community defined

Fix snmp session get without community defined
Fix code indentation
Added options -m on usage
Added constants variables
This commit is contained in:
Sidney Souza 2014-12-02 12:56:01 -02:00
parent 944d47e18e
commit 3528589b39

View File

@ -6,7 +6,12 @@
#
# v0.0.1
#
# v.0.0.2 02/12/2014, Sidney Souza
# Fix snmp session get without community defined
# Fix code indentation
# Added options -m on usage
#
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4
use Net::SNMP;
use Getopt::Std;
@ -14,6 +19,17 @@ use Data::Dumper;
use vars qw/ %opt /;
use strict;
use constant{
OK => 0,
WARNING => 1,
CRITICAL => 2,
UNKNOWN => 3,
true => 1,
false => 0,
VERSION => '0.0.2'
};
sub getmasked_values ($$);
sub f2c ($);
@ -31,12 +47,20 @@ my $comm = $opt{C};
my $param = $opt{p};
my $warn = $opt{w};
my $crit = $opt{c};
my $metric = $opt{m};
my $metric = "degF" if ( defined ($opt{m}) );
my $list = $opt{l};
my $sensor_int_name = $opt{s};
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $comm,
-version => 'snmpv2c',
-port => "161"
);
my ($oid, $oidbase, $fval, $unit, $outmsg);
my $retval = 0;
my $retval = OK;
my %rpduamps;
my %oids = (
@ -194,44 +218,38 @@ my %oids = (
'oid' => '.1.3.6.1.4.1.318.1.1.13.3.2.2.2.25.0',
'cdef' => '$val * .10'
},
);
);
if ($list) {
my ($baseoid, $int_name_id, $value_id) = @_;
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $comm,
-version => 1,
# -translate => [-octetstring => 0x0],
-port => "161"
);
my $response = $session->get_table(-baseoid => ".1.3.6.1.4.1.5528.100.2.1.1");
my $err = $session->error;
if ($err){
$retval = 3;
$retval = UNKNOWN;
$outmsg = "UNKNOWN";
$session->close();
print "$outmsg $err - SNMP Error connecting to $host\n";
exit $retval;
}
my %sensor;
foreach my $k (keys %{$response}) {
my ($type, $id) = (split(/\./, $k))[-2,-1];
foreach my $key (keys %{$response}) {
my ($type, $id) = (split(/\./, $key))[-2,-1];
next if ($type != 1 and $type != 4);
next if ($id < 2000000000);
if ($type == 1) {
$sensor{$id}->{"int_name"} = $response->{$k};
$sensor{$id}->{"int_name"} = $response->{$key};
} else {
$sensor{$id}->{"friendly_name"} = $response->{$k};
$sensor{$id}->{"friendly_name"} = $response->{$key};
}
}
print <<" EO";
Specify a sensor by using the -s and the INTERNAL name of the sensor
Detected sensors:
EO
printf("\t%-32s %s\n", "Friendly Name", "Internal Name");
foreach my $id (sort { $sensor{$a}->{"friendly_name"} cmp $sensor{$b}->{"friendly_name"} } keys %sensor) {
@ -248,29 +266,21 @@ Detected sensors:
$oidbase = $oids{$param}->{oidbase};
}
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $comm,
-version => 1,
# -translate => [-octetstring => 0x0],
-port => "161"
);
if ($param eq "rpduamps") {
# $param = "RackPDU";
my $i;
for ($i=1;$i<4;$i++) {
for (my $i=1;$i<4;$i++) {
my $phoid = $oid . $i;
my $response = $session->get_request($phoid);
my $err = $session->error;
if ($err){
$retval = 3;
$retval = UNKNOWN;
$outmsg = "UNKNOWN";
$session->close();
print "$outmsg $err - SNMP Error connecting to $host\n";
exit $retval;
}
$rpduamps{$i} = $response->{$phoid};
}
$session->close;
@ -282,15 +292,15 @@ if ($param eq "rpduamps") {
my $tphase = ($rpduamps{$ph} * .1);
if (($tphase >= $crit) && ($retval < 2)) {
$retval = 2;
$retval = CRITICAL;
$outmsg = "CRITICAL";
} elsif (($tphase >= $warn) && ($retval < 1)) {
$retval = 1;
$retval = WARNING;
$outmsg = "WARNING";
} elsif ($retval < 1) {
$retval = 0;
$retval = OK;
$outmsg = "OK";
}
@ -306,12 +316,14 @@ if ($param eq "rpduamps") {
} else {
my $val;
if ($oid) {
my $response = $session->get_request($oid);
my $err = $session->error;
if ($err){
$retval = 3;
$retval = UNKNOWN;
$outmsg = "UNKNOWN";
$session->close();
print "$outmsg $err - SNMP Error connecting to $host\n";
@ -324,15 +336,18 @@ if ($param eq "rpduamps") {
} else {
my $snmpd = getmasked_values($oidbase, { $oids{$param}->{sensor_key} => 'sensor_key',
$oids{$param}->{sensor_val} => 'sensor_val' });
my $snmpd = getmasked_values(
$oidbase, {
$oids{$param}->{sensor_key} => 'sensor_key',
$oids{$param}->{sensor_val} => 'sensor_val' }
);
if ((keys %{$snmpd}) > 1 && !$sensor_int_name) {
print "UNKNOWN - Many sensors found but none specified, see -s and -l\n";
exit 3;
exit UNKNOWN;
} elsif ((keys %{$snmpd}) == 0) {
print "UNKNOWN - no sensors found on this device\n";
exit 3;
exit UNKNOWN;
} else {
my $id = (keys %{$snmpd})[0];
$val = $snmpd->{$id}->{sensor_val};
@ -348,17 +363,18 @@ if ($param eq "rpduamps") {
if ($val eq "") {
print Dumper $snmpd;
print "UNKNOWN Unable to get sensor status\n";
exit 3;
exit UNKNOWN;
}
}
if ($param eq "acscstatus" || $param eq "acrcstatus") {
if ($val == 1) {
$fval = "Standby";
$retval = 1;
$retval = WARNING;
$outmsg = "WARNING";
} elsif ($val == 2) {
$fval = "On";
$retval = 0;
$retval = OK;
$outmsg = "OK";
}
} else {
@ -375,13 +391,13 @@ if ($param eq "rpduamps") {
}
if ($fval > $crit) {
$retval = 2;
$retval = CRITICAL;
$outmsg = "CRITICAL";
} elsif ($fval > $warn) {
$retval = 1;
$retval = WARNING;
$outmsg = "WARNING";
} else {
$retval = 0;
$retval = OK;
$outmsg = "OK";
}
}
@ -394,23 +410,20 @@ exit $retval;
sub usage {
print "Usage: $0 -H <hostip> -C <community> -p <parameter> -w <warnval> -c <critval> [-l] [-s sensor]\n";
print "Usage: $0 -H <hostip> -C <community> -p <parameter> -w <warnval> -c <critval> [-l] [-s sensor] [-m]\n";
print "\nParameters:\n";
print <<" EO";
APC NetBotz
nbmstemp NetBotz main sensor temp | nbmshum NetBotz main sensor humidity
nbmsairflow NetBotz main sensor airflow | nbmsaudio NetBotz main sensor audio
-l List connected sensors | -s sensor Sensor we want info from
APC Metered Rack PDU
rpduamps Amps on each phase
APC ACSC In-Row
acscstatus System status (on/standby) | acscload Cooling load
acscoutput Cooling output | acscsupair Supply air
acscairflow Air flow | acscracktemp Rack inlet temp
acsccondin Condenser input temp | acsccondout Condenser outlet temp
APC ACRC In-Row
acrcstatus System status (on/standby) | acrcload Cooling load
acrcoutput Cooling output | acrcairflow Air flow
@ -433,16 +446,10 @@ sub f2c($) {
sub getmasked_values ($$) {
my ($baseoid, $values) = @_;
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $comm,
-version => 1,
-port => "161"
);
my $response = $session->get_table(-baseoid => $baseoid);
my $err = $session->error;
if ($err){
$retval = 3;
$retval = UNKNOWN;
$outmsg = "UNKNOWN";
$session->close();
print "$err - SNMP Error connecting to $host\n";