Check HP switch sensor status via Nagios

1 min read
Check HP switch sensor status via Nagios

The perl script below allows you to check sensors of HP switches 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 switches and Net-SNMP is installed at the Nagios host.

Show code
    #!/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.4.1.11.2.14.11.1.2.6.1.4";
    my $SNMPOIDdescr=".1.3.6.1.4.1.11.2.14.11.1.2.6.1.7";
    
    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/ Sensor//g; # Remove -sensor- text from descriptions
    $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 $status_message = "";
    
    foreach my $sensor (@sensor_status) {
      $ix++;
      
      next if $sensor =~ /INTEGER: 5/ or $sensor =~ /INTEGER: 1/; # skip not present, unknown
      
      $status_message .= "OK" if $sensor =~ /INTEGER: 4/;
      $status_message .= "WARNING" if $sensor =~ /INTEGER: 3/;
      $status_message .= "CRITICAL" if $sensor =~ /INTEGER: 2/;
      
      my ($lsensor) = ($sensor_desc =~ /STRING: (.*)/);
      
      $status_message .= " - " . $lsensor . ", ";  
    }
    
    print "Sensors - " . $status_message;
    
    exit $CRITICAL if $status_message =~ /CRITICAL/;
    exit $WARNING if $status_message =~ /WARNING/;
    exit $OK;