#!/usr/bin/perl -w
#
# Check Solaris Fault Manager with SNMP  
#
# Scott Nolin scott.nolin@ssec.wisc.edu
# 29 September 2010 
#
# credit to Nico <nico@gcu.info> for script check_snmp_fmdpl.gz 
# 
# Changes by Scott:
#	-use Nagios::Plugin instead of deprecated utils.pm
#	-add community string as option
#	-use ResourceCount oid to signal there are errors, 
#	    add various failure to get data checks. This
#	    avoids false "OK" reports on errors 
#
#	29Sep2010 - added 'critical' state instead of 'unknown'


use strict;
use Getopt::Std;
use Net::SNMP;
use Nagios::Plugin;

use vars qw($warn $critical $result);

# To compose SNMP OIDs
my $oid_resource_table = ".1.3.6.1.4.1.42.2.195.1.5.1.4";  # sunFmResourceTable
my $oid_resource_count = '.1.3.6.1.4.1.42.2.195.1.4.0'; #number believed to be faulty

my $plugin = Nagios::Plugin->new(
        usage => "Usage: %s -H hostname -C community",
	blurb => "This plugin checks the Solaris Fault Manager Utility via snmp. It returns errors if there are any reported problems.", 
);

#add host
$plugin->add_arg(
        spec => 'host|H=s',
        help => "hostname",
        required => 1,
);

#add community string
$plugin->add_arg(
        spec => 'community|C=s',
        help => "SNMP community string",
        required => 1,
);

## parse arguments and process standard
$plugin->getopts;

########
#get the data via snmp

my ($session, $session_error) = Net::SNMP->session(
        -hostname => $plugin->opts->host,
        -community => $plugin->opts->community,
        -port   => 161,
#       -debug => 1,
        -version => 2,
);

if (!defined($session)) { 
        $plugin->nagios_exit(
                return_code => 'CRITICAL',
                message => "$session_error",
        );
}

my ($faults) = $session->get_request(
	-varbindlist	=> [ $oid_resource_count ],
);

if (!defined($faults)) {        
       $plugin->nagios_exit(
               return_code => 'CRITICAL',
               message => "Failed to get SNMP data.",
	);
}

if ($faults->{$oid_resource_count} == 0) { # no problems, exit
	$plugin->nagios_exit('OK',"");
}

#Faults != 0, so there are some faults, print them 

my $result = $session->get_table(-baseoid => $oid_resource_table);
print "Faults = $faults->{$oid_resource_count} : "; 

if($result) {
	my %events = %{$result};
	my $key;
	foreach $key (keys %events) {
		print ("Event \=> ",$events{$key}," - ");
	}
	print ": Run 'fmadm faulty' on host for more information.\n";
	$plugin->nagios_exit(
		return_code=> 'CRITICAL'
	);
} else { #I expected errors! Warn
	$plugin->nagios_exit(
        	return_code => 'WARNING',
        	message => "Error count was !=0, but found nothing in resource_table.",
        );
}


