#!/usr/bin/perl -w
#
#    check_lxp-controller_load - nagios plugin
#
# 
#    Copyright (C) 2007 Priyantha Kumara
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#    Report bugs to priyantha.kumara_AT_gmail_DOT_com
#
#    11-06-2007 Version 4.1.1 
#    
#    Cleared up the code from ver. 3.x.y to be compatible with nagios plugin frame-work   
#    Use of Getopt::Long module instead of Getopt::Std
#
#    Primary MIB referernce - lxp.MIB from Emerson Network Power
#
use strict;
use Net::SNMP;
use Getopt::Long;
&Getopt::Long::config('bundling');
use utils qw(%ERRORS);
use vars qw/ %opt /;

my $script    = "check_lxp-controller_load";
my $script_version = "4.1.1";

my $oid_sysDescr = ".1.3.6.1.2.1.1.1.0";
my $oid_PlantLoad = "1.3.6.1.4.1.885.4.2.1.3.0";
my $oid_PlantVoltage = "1.3.6.1.4.1.885.4.2.1.1.0";

my ($hostname, $snmp_version, $community, $timeout) = ("192.168.1.1", "1", "public", 2);
my ($cstatus, $status, $alarm) = (0, 0, 0,);
my $returnstring = "";
my $err2 = "";
my $err1 = "";
my ($ml, $tcur, $clmt) = (132, 160, 165);
my ($lvd, $lv2, $lv1, $hv1, $hv2, $hvs) = (37, 47, 52.1, 54, 55.5, 56);
my ($PlantLoad, $PlantVoltage, $vstatus);
my $temp;
# my $opt_string = 'hVH:C:w:c:';
my ($opt_h, $opt_H, $opt_V, $opt_C, $opt_w, $opt_c);

####################################################################
# This is where we gather command-line options                     #
####################################################################

# Do we have enough information?
  if (@ARGV < 1) {
      print "Too few arguments\n";
      usage();
  }

#  getopts("$opt_string", \%opt);

GetOptions(
	'V|Version'	=> \$opt_V,
	'h|help'	=> \$opt_h,
	'H|Hostname=s'	=> \$opt_H,
	'C|Community=s'	=> \$opt_C,
	'w|warning=s'	=> \$opt_w,  # We use 's' here to combine multiple values
	'c|Critical=s'	=> \$opt_c,  # Same applies here
	);



  if ($opt_h){
     usage();
     exit $ERRORS{'OK'};
  }

  if ($opt_V){
#     print "$script v$script_version\n";
     version();
     exit $ERRORS{'OK'};
  }
  elsif ($opt_H){
     $hostname = $opt_H;
     # print "Hostname $opt_H\n";
  }
  else {
     print "Using default hostname $hostname...";
  }
  if ($opt_C){
     $community = $opt_C;
     # print "Using community $opt_C\n";
  }
  else {
     print "Using default community $community...";
  }
  if ($opt_w){
     ($lv1,$hv1,$ml) = split/,/,$opt_w;
     $ml =~s/\n//;
     # $major = $opt_w;
     # print "Major threshold: opt_w: $lv1:$hv1:$ml\n";
  }
  if ($opt_c){
     ($lv2,$lvd,$hv2,$hvs,$tcur,$clmt) = split/,/,$opt_c;
     $clmt =~ s/\n//;
     # $critical = $opt_c;
     # print "Critical threshold: $opt_c%\n";
  }


# Create the SNMP session
my ($s, $e) = Net::SNMP->session(
     -community  =>  $community,
     -hostname   =>  $hostname,
     -version    =>  $snmp_version,
     -timeout    =>  $timeout,
);

main();

# Close the session
$s->close();

if ($returnstring eq ""){
    $status = 3;
}

if ($status == 0){
    print "Status is OK - $returnstring\n";
}
elsif ($status == 1){
   print "Status is a MAJOR ALARM - $returnstring\n";
}
elsif ($status == 2){
    print "Status is CRITICAL ALARM - $returnstring\n";
}
#elsif ($status == 3){
#    print "Status is SHUTDOWN ALARM - $returnstring\n";     # We won't be able to reach the controller
#}                                                           # under this condition 
else{
    print "Status is UNKNOWN\n";
}
 
exit $status;

####################################################################
# This is where we poll plant load & voltage via SNMP              #
####################################################################

sub main {

    if (!defined($s->get_request($oid_PlantLoad))) {
        if (!defined($s->get_request($oid_sysDescr))) {
            $returnstring = "SNMP agent not responding";
            $status = 1;
             return 1;
        }
        else {
            $returnstring = "SNMP OID does not exist";
            $status = 1;
            return 1;
        }
    }
     foreach ($s->var_bind_names()) {
         $PlantLoad = $s->var_bind_list()->{$_};
    }

    if (!defined($s->get_request($oid_PlantVoltage))) {
        if (!defined($s->get_request($oid_sysDescr))) {
            $returnstring = "SNMP agent not responding";
            $status = 1;
            return 1;
        }
        else {
            $returnstring = "SNMP OID does not exist";
            $status = 1;
            return 1;
        }
    }
    foreach ($s->var_bind_names()) {
         $PlantVoltage = $s->var_bind_list()->{$_};
    }
    if ($PlantVoltage < $lvd){
        $vstatus = 2;
        $err1 = ":Low Voltage Disconnect!";
    }
    elsif ($PlantVoltage < $lv2){
        $vstatus = 2;
        $err1 = ":Critical Low Voltage!";
    }
    elsif ($PlantVoltage < $lv1){
        $vstatus = 1;
        $err1 = ":Major Low Voltage!";
    }
    elsif ($PlantVoltage < $hv1){
        $vstatus = 0;
        $err1 = "";
    }
    elsif ($PlantVoltage < $hv2){
        $vstatus = 1;
        $err1 = ":Major High Voltage!";
    }
    elsif ($PlantVoltage < $hvs){
        $vstatus = 2;
        $err1 = ":Critical High Voltage!";
    }
    elsif ($PlantVoltage > $hvs){
        $vstatus = 2;
        $err1 = ":High Voltage Shutdown!";
    }
    else {
        $status = 3;
    }
    if ($PlantLoad < $ml){
        $cstatus = 0;
        $err2 = "";
    }
    elsif ($PlantLoad >= $clmt) {
        $cstatus = 3;
        $err2 = ":Serious High Load!"
    }
    elsif ($PlantLoad >= $tcur){
        $cstatus = 2;
        $err2 = ":Critical High Load!";
    }
    elsif ($PlantLoad >= $ml){
        $cstatus = 1;
        $err2 = ":Major Over Load!";
    }
    else {
        $status = 3;
    }

    if ($vstatus == 0 && $cstatus == 0){
        $alarm = 0;
    }
    else {
        $alarm = 1;
    }

    if ($alarm == 0){
        $status = 0;
        $temp = "Plant Load: $PlantLoad (Amps), Plant Voltage: $PlantVoltage (Vdc)";
    }
    else {
        if ($cstatus >= $vstatus){
            $status = $cstatus;
            $temp = "Plant Load: $PlantLoad (Amps), Plant Voltage: $PlantVoltage (Vdc)$err2$err1";
        }
        elsif ($cstatus < $vstatus){
            $status = $vstatus;
            $temp = "Plant Load: $PlantLoad (Amps), Plant Voltage: $PlantVoltage (Vdc)$err1$err2";
        }  
        else{
            $status = 3;
            $temp = "Plant Load: $PlantLoad (Amps), Plant Voltage: $PlantVoltage (Vdc)$err1$err2";
        } 
     }
#    $temp = "Plant Load: $PlantLoad (Amps), Plant Voltage: $PlantVoltage (Vdc) $err$err1";
    append($temp);    
}

####################################################################
# help and usage screen                                            #
####################################################################

sub usage {
    print << "USAGE";
--------------------------------------------------------------------	 
$script v$script_version
Returns the LXP-Controller Plant Load
Usage: $script -H|(--hostname) <hostname> [-C|(--community) <community>]
               [-w|(--warning <LV1,HV1,MaxLoad>]
               [-c|(--critical) <LV2,LVD,HV2,HVS,TCUR,CLMT>]
Options: -h|(--help)       Prints this help screen
         -V|(--version)    Prints the script version
         -H (--hostname)   Hostname or IP address
         -C (--Community)  Community string (default is public)
         -w (--warning)    Major (warning) thresholds for PlantVolatge & PlantLoad
                           This is a comma (",") separated list in the following order
                                 -w LV1,HV1,MaxLoad
                           where LV1=Low Voltage 1, HV1=High Voltage 1, 
                           MaxLoad=Max. Plant Load allowed The default's are; 
                                 LV1=52.1, HV1=54, MaxLoad=132
         -c (--critical)   Critical threshold for PlantVoltage & Plant Load
                           This is a comma (",") separated list in the following order
                                 -c LV2,LVD,HV2,HVS,TCUR,CLMT
                           where LV2=Low Voltage 2, LVD=Low Voltage Disconnect HV2=High Voltage 2,
                           HVS=High Voltage Shutdown, TCUR=High Current Alarm, CLMT=Current Limit
                           The defaults are;
                                 LV2=47, LVD=37, HV2=55.5, HVS=56, TCUR=160, CLMT=165
------------------------------------------------------------------------

USAGE
     exit $ERRORS{'OK'};
}

####################################################################
# Version screen                                                   #
###################################################################

sub version {
   print << "VERSION";
-----------------------------------------------------------------------
$script v$script_version

Copyright 2007 (C) Priyantha Kumara

This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
------------------------------------------------------------------------

VERSION
     exit $ERRORS{'OK'};
}


####################################################################
# Appends string to $returnstring at exit                          #
####################################################################

sub append {
    my $appendstring =    $_[0];    
    $returnstring = "$returnstring$appendstring";
}

