1
0
mirror of https://github.com/opinkerfi/nagios-plugins.git synced 2024-11-05 10:03:45 +01:00

Various check_brocade_env.pl fixes

This commit is contained in:
Tómas Edwardsson 2010-03-16 16:13:01 +00:00
parent c8cdc90147
commit b9ada8f469

View File

@ -17,23 +17,21 @@
use Nagios::Plugin;
use Net::SNMP;
use Getopt::Long;
use Data::Dumper;
use vars qw/ %opt /;
use strict;
# OID Base for Sensor Data
my $oidbase = "1.3.6.1.4.1.1588.2.1.1.1.1.22";
# Friendly type names for sensors
my %sensorTypes = (
1 => "temperature",
2 => "fan",
3 => "power-supply"
);
# Friendly status names for sensors
my %sensorStatus = (
1 => "Unknown",
2 => "Faulty",
@ -45,12 +43,14 @@ my %sensorStatus = (
sub snmp_fetchbase($$$$);
# Create the Nagios plugin object
my $np = Nagios::Plugin->new(
usage => "Usage: %s -H <hostname> -c <snmp_community>",
version => "0.01",
);
# Add valid arguments
$np->add_arg(
spec => 'hostname|H=s',
help => '-H, --hostname=<hostname>',
@ -77,9 +77,11 @@ $np->add_arg(
required => 0,
);
# Parse Arguments
$np->getopts();
# Fetch the snmp data from the switch
my $snmp_sensor_data = snmp_fetchbase(
$np->opts->hostname,
$np->opts->community,
@ -87,66 +89,83 @@ my $snmp_sensor_data = snmp_fetchbase(
$oidbase);
# Re-format snmp sensor data to easily parsable for the plugin
my %sensordata;
foreach my $k (keys %{$snmp_sensor_data}) {
# Remove spaces from front/end
my $v = $snmp_sensor_data->{$k};
$v =~ s/^\s+//g;
$v =~ s/\s+$//g;
if ($k =~ /^1\.3\.6\.1\.4\.1\.1588\.2\.1\.1\.1\.1\.22\.1\.(\d+)\.(\d+)$/) {
$sensordata{$2}->{$1} = $v;
}
}
# Hack for multiline status output
$np->add_message( OK, "" ) if ($np->opts->longserviceoutput);
foreach my $sensor (sort keys %sensordata) {
if ($sensorStatus{$sensordata{$sensor}->{3}} ne "Nominal") {
$np->add_message( CRITICAL, "$sensordata{$sensor}->{5} is $sensorStatus{$sensordata{$sensor}->{3}}");
} else {
$np->add_message( OK, "$sensordata{$sensor}->{5} is $sensorStatus{$sensordata{$sensor}->{3}}");
}
# Loop through each installed sensor
foreach my $sensor (sort keys %sensordata) {
my $label = "";
# Add the performance data
if ($sensorTypes{$sensordata{$sensor}->{2}} eq "temperature") {
$label = '°';
$np->add_perfdata(
label => $sensordata{$sensor}->{5},
value => $sensordata{$sensor}->{4},
uom => "Celsius");
} elsif ($sensorTypes{$sensordata{$sensor}->{2}} eq "fan") {
$label = 'RPM';
$np->add_perfdata(
label => $sensordata{$sensor}->{5},
value => $sensordata{$sensor}->{4},
uom => "RPM");
}
#printf("Type: %-14s Name: %-22s Status: %-12s Value: %i\n",
#$sensorTypes{$sensordata{$sensor}->{2}},
#$sensordata{$sensor}->{5},
#$sensorStatus{$sensordata{$sensor}->{3}},
#$sensordata{$sensor}->{4});
# Are you OK ?
if ($sensorStatus{$sensordata{$sensor}->{3}} ne "Nominal") {
$np->add_message( CRITICAL, "$sensordata{$sensor}->{5} is $sensorStatus{$sensordata{$sensor}->{3}} $sensordata{$sensor}->{4}$label");
# Nominal data
} else {
$np->add_message( OK, "$sensordata{$sensor}->{5} is $sensorStatus{$sensordata{$sensor}->{3}} $sensordata{$sensor}->{4}$label");
}
}
my ($code, $message) = $np->check_messages("join" => ($np->opts->longserviceoutput ? "\n" : ""));
# Process messages and get return code
my ($code, $message) = $np->check_messages("join" => ($np->opts->longserviceoutput ? "\n" : " - "));
# We're done, return exit code, message and perfdata
$np->nagios_exit( $code, $message );
# Fetch SNMP data
sub snmp_fetchbase($$$$) {
my $host = shift;
my $community = shift;
my $version = shift;
my $oidbase = shift;
# Setup SNMP session
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $community,
-version => $version,
-port => "161"
);
# Handle errors
$np->nagios_exit(CRITICAL, "Unable to connect to snmp host, $error") if ($error);
# Fetch oids
my $response = $session->get_table(-baseoid => $oidbase);
my $err = $session->error;
$np->nagios_exit(CRITICAL, "Unable to retrieve snmp data, $err") if ($err);
# Return SNMP Table hash
return $response;
}