Check HP storage system sensor status via Nagios

Check HP switch sensor status via NagiosThe perl script below allows you to check sensors of HP storage systems via Nagios. It displays status of all available sensors and returns WARNING/CRITICAL if at least one sensor reports it. Requirements: SNMP is enabled at storage systems and Net-SNMP is installed at the Nagios host.

#!/usr/bin/perl -w

use strict;
use Getopt::Long;

my $UNKNOWN = -1;
my $OK = 0;
my $WARNING = 1;
my $CRITICAL = 2;

my $host = undef;
my $community = "public";
my $version = "2c";
my $oid = undef;

# Defines OID parent paths
my $SNMPOIDstatus=".1.3.6.1.3.94.1.8.1.4";
my $SNMPOIDdescr=".1.3.6.1.3.94.1.8.1.3";

GetOptions(

"host|H=s" => \$host,
"community|C=s"  => \$community,
"version|V=s" => \$version,

);

($host) || die $!;

chomp($host);
chomp($community);

my $snmp_status =  `snmpwalk -v $version -c $community $host "$SNMPOIDstatus"`;

my $snmp_desc =  `snmpwalk -v $version -c $community $host "$SNMPOIDdescr"`;
$snmp_desc =~ s/"//g; # Remove -sensor- text from descriptions

my @sensor_status = split /\n/, $snmp_status;
my @sensor_desc = split /\n/, $snmp_desc;

my $ix=0;
my $sensor_count = 0;

my $status_message = undef;

my $worst_status = $UNKNOWN;

foreach (@sensor_status)
{
    my ($sensor) = ($_ =~ /INTEGER: (.*)/);
    $ix++;
   
    # sensor status 3 - ok, 4 - warning, 5 - critical, skip otherwise   
    next if $sensor < 3 or $sensor > 5;

    if ($sensor == 4 || $sensor == 5)
    {
        $status_message .= ($sensor == 4) ? "WARNING" : "CRITICAL";
        my ($lsensor) = ($sensor_desc =~ /STRING: (.*)/);
        $status_message .= " - " . $lsensor . ", ";
    }

    $worst_status = $OK if ($sensor == 3 && $worst_status < $OK);   
    $worst_status = $WARNING if ($sensor == 4 && $worst_status < $WARNING);
    $worst_status = $CRITICAL if ($sensor == 5 && $worst_status < $CRITICAL);
   
    $sensor_count++;
}

print  ((defined $status_message) ? $status_message : "All $sensor_count sensors Ok.");

exit $worst_status;