#!/usr/bin/perl
#
# check_ibmrsa2_snmp_system_health
#
# Copyright © 2011 Philip Garner, Sysnix Consultants Limited
#
#    This program is free software: you can redistribute it and/or modify
#    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.
#
#    This program is distributed in the hope that it will be useful,
#    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/>.
#
# Authors: Phil Garner - phil@sysnix.com & Peter Mottram - peter@sysnix.com
#
#
# v0.1 18-10-2011
#
# NOTES: Requires Perl 5.8 or higher and
#        the Perl Modules Nagios::Plugin, SNMP
#

use warnings;
use strict;
use Nagios::Plugin;
use SNMP;

my $np = Nagios::Plugin->new(
    shortname => 'check_ibmrsa2_snmp_system_health',
    version   => '0.1',
    usage   => "Usage: %s -H <hostaddress> -C <community>\n\t--help for help\n",
    license => "License - GPL v3 see code for more details",
    url     => "http://www.sysnix.com",
    blurb =>
      "\tNagios plugin that checks the system health of IBM RSA II using SNMP, 
\trequires Perl 5.8+, CPAN modules Nagios::Plugin and SNMP.",
);

# Args

$np->add_arg(
    spec     => 'hostaddress|H=s',
    help     => 'Target host address (RSA address)',
    required => 1,
);

$np->add_arg(
    spec     => 'community|C=s',
    help     => 'Community password',
    required => 1,
);

$np->getopts;

# Set up SNMP Session

my $s = SNMP::Session->new(
    DestHost   => $np->opts->hostaddress,
    Community  => $np->opts->community,
    Version    => 1,
    UseNumeric => 1,
) or $np->add_message( CRITICAL, ": Could not creat snmp connection" );

my $snmp_output = $s->get('.1.3.6.1.4.1.2.3.51.1.2.7.1.0');

if ( !defined $snmp_output || $snmp_output !~ m/^\d+$/ ) {

    # no response, undef return or non-numeric response so set critical

    $np->add_message( CRITICAL, "NO SNMP response or bad reponse" );
    $np->nagios_exit( $np->check_messages() );

}
elsif ( $snmp_output != 255 ) {

    # Response not 255 so get SNMP info and set critical even if no info

    # Set an alert level incase WALK dosn't return any info (being Paranoid).
    if ( $snmp_output == 2 or $snmp_output == 4 ) {
        $np->add_message( WARNING, "System Status is Warning" );
    }
    elsif ( $snmp_output == 0 ) {
        $np->add_message( CRITICAL, "System Status is Critical" );
    }
    else {
        $np->add_message( CRITICAL, "Unexpeceted system health response" );
    }

    my %hash;

    # do an SNMP walk of the oid to find faults and write data into hash
    my $oid = '.1.3.6.1.4.1.2.3.51.1.2.7.2';
    my $var = SNMP::Varbind->new( [$oid] );
    while ( $var->[0] =~ m/^$oid(\.|$)/ ) {
        my $val = $s->getnext($var);
        if ( $var->[0] =~ m/\.(\d+)\.(\d+)$/ ) {
            if ( $2 == 2 ) {
                $hash{$1}{sev} = $val;
            }
            elsif ( $2 == 3 ) {
                $hash{$1}{des} = $val;
            }
        }
        else {

            # ERROR pattern match failed this is very bad
            $np->add_message( CRITICAL, "ERROR unexpected SNMP response" );
        }
    }

    foreach my $index ( keys %hash ) {

        # Spin through hash and add info to messages

        my $alert;

        if ( defined $hash{$index}{sev}
            && $hash{$index}{sev} =~ m/(critical|warning)/i )
        {
            $alert = uc($1);
        }
        else {
            $alert = "CRITICAL";
        }

        if ( defined $hash{$index}{des} && $hash{$index}{des} ne '' ) {
            $np->add_message( $alert, ": $hash{$index}{des}" );
        }
        else {
            $np->add_message( $alert, ": Unknown Problem" );
        }
    }
}
else {

    # This only happens if returned 255 so all in theory should be ok
    $np->add_message( OK, "No Problems Found" );
}

#  and exit
$np->nagios_exit( $np->check_messages() );

# EOF
