#! /usr/bin/perl -w
use strict;

# Ce script envoie une alarme WARNING si une des deux alimentations
# d'un routeur Cisco est en panne.

use Net::SNMP;
use Getopt::Std;
use vars qw/$opt_h $opt_C $opt_p/;
# use Data::Dumper;

# snmpwalk -v2c -cxxx fb-ar1 ciscoEnvMonSupplyState
# CISCO-ENVMON-MIB::ciscoEnvMonSupplyState.1 = INTEGER: normal(1)
# CISCO-ENVMON-MIB::ciscoEnvMonSupplyState.2 = INTEGER: normal(1)

# snmpwalk -On -v2c -cxxx th2-br1 ciscoEnvMonSupplyState
# .1.3.6.1.4.1.9.9.13.1.5.1.3.1 = INTEGER: notPresent(5)
# .1.3.6.1.4.1.9.9.13.1.5.1.3.2 = INTEGER: normal(1)


# Options
my ($host , $community);

sub usage {
	my ($msg) = @_;
	$msg ||= "";
   print <<EOF;
Usage: $msg
$0 -h host -c community -p 1,2,3,...
 -h host : hĂ´te cible
 -C community : communautĂ© ©SNMP utisĂ©e

EOF
   exit(3);
}

getopt("h:C:");
$host = $opt_h || usage("host not defined");
$community = $opt_C || usage("community not defined");

my $warning_message = "";

my $supply_state_oid = ".1.3.6.1.4.1.9.9.13.1.5.1.3";

my ($session, $error) =
   Net::SNMP->session(-hostname => $host, -version => "2c", -community => $community);

if (not defined($session)) {
   print "Impossible d'ouvrir une session SNMP avec $host: $error !\n";
   exit(3); # UNKNOWN
}

for my $power_supply_id ( 1 .. 2 ) {


	my $power_state_oid = "$supply_state_oid.$power_supply_id";

	my $result = $session->get_request(-varbindlist => [ $power_state_oid ]);

	if (not defined($result)) {
   	 my $error = $session->error();
    	print "Impossible de rĂcupĂ©rer 'Ătat de l'alim pour $host : $error\n";
    	exit(3); # UNKNOWN
	}

	my $power_state = $result->{$power_state_oid};

	if ( $power_state != 1 ) {
		$warning_message .= "WARNING: POWER SUPPLY $power_supply_id DOWN\n";
	}

}

if ( $warning_message ) {
    print $warning_message;
    exit(1); # WARNING
}

else {
    print "OK\n";
    exit(0); # OK
}


