1
0
mirror of https://github.com/opinkerfi/nagios-plugins.git synced 2024-11-23 02:43:45 +01:00
nagios-plugins/check_brocade_env/trunk/check_brocade_env.pl

172 lines
4.1 KiB
Perl
Raw Normal View History

2010-03-16 16:39:25 +01:00
#!/usr/bin/perl -w
#
# Copyright 2010, Tomas Edwardsson
#
2010-03-16 16:40:55 +01:00
# check_brocade_env.pl is free software: you can redistribute it and/or modify
2010-03-16 16:39:25 +01:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2010-03-16 16:40:55 +01:00
# check_brocade_env.pl is distributed in the hope that it will be useful,
2010-03-16 16:39:25 +01:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use Nagios::Plugin;
use Net::SNMP;
use strict;
2010-03-16 17:13:01 +01:00
# OID Base for Sensor Data
2010-03-16 16:39:25 +01:00
my $oidbase = "1.3.6.1.4.1.1588.2.1.1.1.1.22";
2010-03-16 17:13:01 +01:00
# Friendly type names for sensors
2010-03-16 16:39:25 +01:00
my %sensorTypes = (
1 => "temperature",
2 => "fan",
3 => "power-supply"
);
2010-03-16 17:13:01 +01:00
# Friendly status names for sensors
2010-03-16 16:39:25 +01:00
my %sensorStatus = (
1 => "Unknown",
2 => "Faulty",
3 => "Below minimum",
4 => "Nominal",
5 => "Above maximum",
6 => "Absent"
);
sub snmp_fetchbase($$$$);
2010-03-16 17:13:01 +01:00
# Create the Nagios plugin object
2010-03-16 16:39:25 +01:00
my $np = Nagios::Plugin->new(
usage => "Usage: %s -H <hostname> -c <snmp_community>",
version => "0.01",
);
2010-03-16 17:13:01 +01:00
# Add valid arguments
2010-03-16 16:39:25 +01:00
$np->add_arg(
spec => 'hostname|H=s',
help => '-H, --hostname=<hostname>',
required => 1,
);
$np->add_arg(
spec => 'community|c=s',
help => '-C, --community=<snmp community>',
required => 1,
);
$np->add_arg(
spec => 'snmpversion|n=s',
help => '-n, --snmpversion=[1/2c]',
default => '2c',
required => 0,
);
$np->add_arg(
spec => 'longserviceoutput|l',
help => '-l, --longserviceoutput',
default => undef,
required => 0,
);
2010-03-16 17:13:01 +01:00
# Parse Arguments
2010-03-16 16:39:25 +01:00
$np->getopts();
2010-03-16 17:13:01 +01:00
# Fetch the snmp data from the switch
2010-03-16 16:39:25 +01:00
my $snmp_sensor_data = snmp_fetchbase(
$np->opts->hostname,
$np->opts->community,
$np->opts->snmpversion,
$oidbase);
2010-03-16 17:13:01 +01:00
# Re-format snmp sensor data to easily parsable for the plugin
2010-03-16 16:39:25 +01:00
my %sensordata;
foreach my $k (keys %{$snmp_sensor_data}) {
2010-03-16 17:13:01 +01:00
# Remove spaces from front/end
2010-03-16 16:39:25 +01:00
my $v = $snmp_sensor_data->{$k};
$v =~ s/^\s+//g;
$v =~ s/\s+$//g;
2010-03-16 17:13:01 +01:00
2010-03-16 16:39:25 +01:00
if ($k =~ /^1\.3\.6\.1\.4\.1\.1588\.2\.1\.1\.1\.1\.22\.1\.(\d+)\.(\d+)$/) {
$sensordata{$2}->{$1} = $v;
}
}
2010-03-16 17:13:01 +01:00
# Hack for multiline status output
2010-03-16 16:39:25 +01:00
$np->add_message( OK, "" ) if ($np->opts->longserviceoutput);
2010-03-16 17:13:01 +01:00
# Loop through each installed sensor
2010-03-16 16:39:25 +01:00
foreach my $sensor (sort keys %sensordata) {
2010-03-16 17:13:01 +01:00
my $label = "";
2010-03-16 16:39:25 +01:00
2010-03-16 17:13:01 +01:00
# Add the performance data
2010-03-16 16:39:25 +01:00
if ($sensorTypes{$sensordata{$sensor}->{2}} eq "temperature") {
2010-03-16 17:13:01 +01:00
$label = '°';
2010-03-16 16:39:25 +01:00
$np->add_perfdata(
label => $sensordata{$sensor}->{5},
value => $sensordata{$sensor}->{4},
uom => "Celsius");
} elsif ($sensorTypes{$sensordata{$sensor}->{2}} eq "fan") {
2010-03-16 17:13:01 +01:00
$label = 'RPM';
2010-03-16 16:39:25 +01:00
$np->add_perfdata(
label => $sensordata{$sensor}->{5},
value => $sensordata{$sensor}->{4},
uom => "RPM");
}
2010-03-16 17:13:01 +01:00
# 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");
}
2010-03-16 16:39:25 +01:00
}
2010-03-16 17:13:01 +01:00
# 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
2010-03-16 16:39:25 +01:00
$np->nagios_exit( $code, $message );
2010-03-16 17:13:01 +01:00
# Fetch SNMP data
2010-03-16 16:39:25 +01:00
sub snmp_fetchbase($$$$) {
my $host = shift;
my $community = shift;
my $version = shift;
my $oidbase = shift;
2010-03-16 17:13:01 +01:00
# Setup SNMP session
2010-03-16 16:39:25 +01:00
my ($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => $community,
-version => $version,
-port => "161"
);
2010-03-16 17:13:01 +01:00
# Handle errors
2010-03-16 16:39:25 +01:00
$np->nagios_exit(CRITICAL, "Unable to connect to snmp host, $error") if ($error);
2010-03-16 17:13:01 +01:00
# Fetch oids
2010-03-16 16:39:25 +01:00
my $response = $session->get_table(-baseoid => $oidbase);
my $err = $session->error;
$np->nagios_exit(CRITICAL, "Unable to retrieve snmp data, $err") if ($err);
2010-03-16 17:13:01 +01:00
# Return SNMP Table hash
2010-03-16 16:39:25 +01:00
return $response;
}