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