#!/usr/bin/perl
##
# Adam Hock
# May 4, 2006
# check_npower.pl
# ahock@ittc.ku.edu
#############


use Net::SNMP '5.0.2.pre1' || die("Cannot load module\n");
use Getopt::Std;
use lib "/usr/system/nagios/libexec";
use utils qw(%ERRORS &print_revision &support &usage);

no strict;

my %options = ();
getopt("VhrHwc",\%options);

if ($options{V}) {
  print_revision($PROGNAME, '$Revision: 1.0 $');
  exit  $ERRORS{'OK'};
}

if ($options{h}) {
  print_help();
  exit $ERRORS{'OK'};
}

sub print_usage () {
  print "Usage: $0 -w <warning> -c <critical> -r <read_community> -H <host_ip>\n";
  print "	[-w <warning>]\n";
  print "	[-c <critical>]\n";
  print "	[-r <read_community>]\n";
  print "	[-H <ip>]\n";
}

sub print_help () {
  print_revision($PROGNAME, '$Revision: 1.0 $');
  print "\n";
  print "This plugin reports battery usage of the Npower UPS \n";
  print "by Liebert Inc. \n";
  print "\n";
  print_usage();
  print "\n";
  print "Example: $0 -w 95 -c 90 -r read -H 172.27.120.1\n";
  print "\n";
}

if ( $options{r} ) {
  $SNMP_COMMUNITY = $options{r};

}else {
  $SNMP_COMMUNITY = "read";
} 


if ( ! $options{H} ) {
  print_usage();
  exit $ERRORS{'OK'};
}else {
  $SNMP_TARGET = $options{H};
}

#ranges
if ( ! $options{w} ) {
  print_usage();
  exit $ERRORS{'OK'};
}else {
 #Warning 
  $warningbattery = $options{w};#percent battery left
}

if ( ! $options{c} ) {
  print_usage();
  exit $ERRORS{'OK'};
}else {
 #Critical
  $criticalbattery = $options{c};#percent battery left
}
 
#known oid's
%oid = (
     'ups'                              => '1.3.6.1.2.1.33',

     'upsIdentManufacturer'		=> '1.3.6.1.2.1.33.1.1.1.0',
     'upsIdentModel'			=> '1.3.6.1.2.1.33.1.1.2.0',
     'upsIdentName'			=> '1.3.6.1.2.1.33.1.1.5.0',

     'upsEstimatedChargeRemaining'	=> '1.3.6.1.2.1.33.1.2.4.0',
     'upsBatteryVoltage'		=> '1.3.6.1.2.1.33.1.2.5.0',
     'upsBatteryCurrent'		=> '1.3.6.1.2.1.33.1.2.6.0',
     'upsBatteryTemperature'		=> '1.3.6.1.2.1.33.1.2.7.0',

    );

######
#MAIN#
######

$ENV{'MIBS'}="ALL";  #Load all available MIBs
($session,$error) = Net::SNMP->session (-hostname => $SNMP_TARGET, 
                                        -community => $SNMP_COMMUNITY,
                                        -version => "1");

die "session error: $error" unless ($session);


foreach $elem (keys %oid) {
 
  $response=$session->get_request($oid{$elem});
  
  die "request error: ".$session->error unless (defined $response). "\n";

  $data{$elem} = $response->{$oid{$elem}};

}

if($data{upsEstimatedChargeRemaining} <= $warningbattery && $data{upsEstimatedChargeRemaining} > $criticalbattery) {
  print "WARNING: [Battery: $data{upsEstimatedChargeRemaining}%]\n";
  exit $ERRORS{'WARNING'};
}

if($data{upsEstimatedChargeRemaining} <= $criticalbattery) {
 print "CRITICAL: [Battery: $data{upsEstimatedChargeRemaining}%]\n";
 exit $ERRORS{'CRITICAL'};
}

if($data{upsEstimatedChargeRemaining} >= $warningbattery) {
 print "[Battery: $data{upsEstimatedChargeRemaining}%]\n";
 exit $ERRORS{'OK'};
} 

