#!/usr/bin/perl

#    Copyright (C) 2004 Altinity Limited
#    E: info@altinity.com    W: http://www.altinity.com/
#    
#    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., 59 Temple Place, Suite 330, Boston, MA    02111-1307    USA


##1 - KJS - Added code for performance data


use Net::SNMP;
use Getopt::Std;

$script    = "check_snmp_apcups";
$script_version = "2.1.0";

$metric = 1;

# $ipaddress = "inccup03"; 	# default IP address, if none supplied
$version = "1";			# SNMP version
$timeout = 2;			# SNMP query timeout
# $warning = 100;			
# $critical = 150;
$status = 0;
$returnstring = "";

$community = "public"; 			# Default community string
$issue = "";
$converttemp = 0;


# .1.3.6.1.4.1.
# enterprises.318.1.1.1.1.1.1.0 = STRING: "SMART-UPS 1000"		upsIdent

# enterprises.318.1.1.1.2.2.1.0			Battery capacity (%)
# enterprises.318.1.1.1.2.2.2.0			Temperature (Celcius)
# enterprises.318.1.1.1.2.2.3.0			Battery runtime remaining
# enterprises.318.1.1.1.2.2.4.0			Battery replace indicator (1=ok, 2=replace)
# enterprises.318.1.1.1.2.2.5.0			Number of batter packs
# enterprises.318.1.1.1.2.2.6.0			Number of bad battery packs
 
# enterprises.318.1.1.1.3.2.1.0			Input voltage
# enterprises.318.1.1.1.3.2.4.0			Input frequency
# enterprises.318.1.1.1.3.2.5.0			Reason for last transfer to UPS battery power:

# enterprises.318.1.1.1.4.2.1.0			Output voltage
# enterprises.318.1.1.1.4.2.2.0			Output frequency
# enterprises.318.1.1.1.4.2.3.0			Output load as % of capacity
# enterprises.318.1.1.1.4.2.4.0			Output current in ampheres

# enterprises.318.1.1.1.4.2.1.0			Configured voltage
# enterprises.318.1.1.1.8.1.0			Whether agent is communicating with UPS (1)

$oid_upstype = ".1.3.6.1.4.1.318.1.1.1.1.1.1.0";
$oid_battery_capacity = ".1.3.6.1.4.1.318.1.1.1.2.2.1.0";
$oid_battery_temperature = ".1.3.6.1.4.1.318.1.1.1.2.2.2.0";
$oid_battery_runtimeremain = ".1.3.6.1.4.1.318.1.1.1.2.2.3.0";
$oid_battery_replace = ".1.3.6.1.4.1.318.1.1.1.2.2.4.0";
$oid_input_voltage = ".1.3.6.1.4.1.318.1.1.1.3.2.1.0";
$oid_upsPhaseInputVoltage1 = ".1.3.6.1.4.1.318.1.1.1.9.2.3.1.3.1.1.1";
$oid_upsPhaseInputVoltage2 = ".1.3.6.1.4.1.318.1.1.1.9.2.3.1.3.1.1.2";

$oid_upsPhaseOutputVoltage1 = ".1.3.6.1.4.1.318.1.1.1.9.2.3.1.3.1.1.2";
$oid_upsPhaseOutputVoltage2 = ".1.3.6.1.4.1.318.1.1.1.9.3.3.1.3.1.1.2";


$oid_input_frequency = ".1.3.6.1.4.1.318.1.1.1.3.2.4.0";
$oid_input_reasonforlasttransfer = ".1.3.6.1.4.1.318.1.1.1.3.2.5.0";
$oid_output_voltage = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
$oid_output_frequency = ".1.3.6.1.4.1.318.1.1.1.4.2.2.0";
$oid_output_load = ".1.3.6.1.4.1.318.1.1.1.4.2.3.0";
$oid_output_current = ".1.3.6.1.4.1.318.1.1.1.4.2.4.0";
$oid_output_configuredvoltage = ".1.3.6.1.4.1.318.1.1.1.4.2.1.0";
$oid_comms = ".1.3.6.1.4.1.318.1.1.1.8.1.0";
$oid_test_result = ".1.3.6.1.4.1.318.1.1.1.7.2.3.0";
$oid_test_date = ".1.3.6.1.4.1.318.1.1.1.7.2.4.0";
$oid_sysDescr = ".1.3.6.1.2.1.1.1.0";
$oid_ambientTemp1 = ".1.3.6.1.4.1.318.1.1.25.1.2.1.6.1.1";
$oid_ambientTemp2 = ".1.3.6.1.4.1.318.1.1.25.1.2.1.6.2.1";
$oid_ambientTemp_iem = "1.3.6.1.4.1.318.1.1.10.2.3.2.1.4";
$oid_humidity1 = ".1.3.6.1.4.1.318.1.1.25.1.2.1.7.1.1";
$oid_humidity2 = ".1.3.6.1.4.1.318.1.1.25.1.2.1.7.2.1";
$oid_humidity_iem = "1.3.6.1.4.1.318.1.1.10.2.3.2.1.4";

$upstype = "";
$battery_capacity = 0;
$battery_temperature = 0;
$battery_runtimeremain = 0;
$battery_replace = "";
$input_voltage = 0;
$upsPhaseInputVoltage1 = 0;
$upsPhaseInputVoltage2 = 0;
$upsPhaseOutputVoltage1 = 0;
$upsPhaseOutputVoltage2 = 0;
$input_frequency = 0;
$input_reasonforlasttransfer = "";
$output_voltage = 0;
$output_frequency = 0;
$output_load = 0;
$output_current = 0;
$output_configuredvoltage = 0;
$outagecause = "";
$test_result = "";
$test_date = "";
$ambient_temperature = 0;
$humidity = 0;
# Do we have enough information?
if (@ARGV < 1) {
     print "Too few arguments\n";
     usage();
}

getopts("h:H:C:w:c:t:f");
if ($opt_h){
    usage();
    exit(0);
}
if ($opt_H){ $hostname = $opt_H; }
else {
    print "No hostname specified\n";
    usage();
}
if ($opt_C) { $community = $opt_C; }
if ($opt_t) { $check = $opt_t; } else { $check = "all"; }
if ($opt_w) { $warning = $opt_w; }
if ($opt_c) { $critical = $opt_c; }
if ($opt_f) { $converttemp = 1; }

# Create the SNMP session
my ($s, $e) = Net::SNMP->session(
     -community  =>  $community,
     -hostname   =>  $hostname,
     -version    =>  $version,
     -timeout    =>  $timeout,
     -translate  =>  [-timeticks => 0],
);

main();

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

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

if ($status == 0){
    print "OK - $returnstring\n";
    # print "$returnstring\n";
}
elsif ($status == 1){
    print "WARNING - $issue- $returnstring\n";
}
elsif ($status == 2){
    print "CRITICAL - $issue- $returnstring\n";
}
else{
    print "Problem with plugin. No response from SNMP agent.\n";
}
 
exit $status;


####################################################################
# This is where we gather data via SNMP and return results         #
####################################################################

sub main {

    #######################################################
 
    if ($check eq "all" || $check eq "comms") {
        if (!defined($s->get_request($oid_comms))) {
            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()) {
             $temp = $s->var_bind_list()->{$_};
        }
        
        if ($temp eq "1"){
        }
        else {
            append("SNMP agent not communicating with UPS");
            $status = 2;
            return 1;
        }
    }
    
    
    #######################################################
 
    if ($check eq "all" || $check eq "comms") {
        if (!defined($s->get_request($oid_upstype))) {
            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()) {
             $upstype = $s->var_bind_list()->{$_};
        }
    }
    
    #######################################################
 
    if ($check eq "all" || $check eq "battery_capacity") {
        if (!defined($s->get_request($oid_battery_capacity))) {
            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()) {
             $battery_capacity = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = 95; }
        if ($battery_capacity < $warning) {
            $issue = $issue . "BATTERY CHARGE WARNING! ";
            $status = 1;
        }
	unless (defined($critical)) { $critical = 90; }
        if ($battery_capacity < $critical) {
            $issue = $issue . "BATTERY RUNNING LOW! ";
            $status = 2;
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "battery_temperature") {
        if (!defined($s->get_request($oid_battery_temperature))) {
            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()) {
        	$battery_temperature = $s->var_bind_list()->{$_};
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "battery_runtimeremain") {
        if (!defined($s->get_request($oid_battery_runtimeremain))) {
            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()) {
             $battery_runtimeremain = $s->var_bind_list()->{$_};
        }
	# This value is in 100ths of a second.  Devide by 6000
        # to get it in minutes.
	$battery_runtimeremain = $battery_runtimeremain / 6000;
	unless (defined($warning)) { $warning = 10; }
        if ($battery_runtimeremain < $warning) {
            $issue = $issue . "BATTERY CAPACITY WARNING! ";
            $status = 1;
        }
	unless (defined($critical)) { $critical = 5; }
        if ($battery_runtimeremain < $critical) {
            $issue = $issue . "BATTERY CAPACITY CRITICAL! ";
            $status = 2;
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "battery_replace") {
        if (!defined($s->get_request($oid_battery_replace))) {
            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()) {
             $battery_replace = $s->var_bind_list()->{$_};
        }
        if ($battery_replace eq "2") {
            $issue = $issue . "REPLACE BATTERY! ";
            $status = 2;
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "input_voltage") {
        if (!defined($s->get_request($oid_input_voltage))) {
            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()) {
             $input_voltage = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "110:126"; }
	unless (defined($critical)) { $critical = "106:127"; }
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
        if ($input_voltage < 1){
            $status = 2;
            $issue = $issue . "RUNNING ON BATTERY! ";
        }
	elsif ($input_voltage < $critical_low) {
            $issue = $issue . "LOW VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($input_voltage > $critical_high) {
            $issue = $issue . "HIGH VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($input_voltage < $warning_low) {
            $issue = $issue . "LOW VOLTAGE WARNING! ";
            $status = 1;
        }
        elsif ($input_voltage > $warning_high) {
            $issue = $issue . "HIGH VOLTAGE WARNING! ";
            $status = 1;
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "input_voltagePhase1") {
        if (!defined($s->get_request($oid_upsPhaseInputVoltage1))) {
            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()) {
             $upsPhaseInputVoltage1 = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "110:126"; }
	unless (defined($critical)) { $critical = "106:127"; }
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
        if ($upsPhaseInputVoltage1 < 1){
            $status = 2;
            $issue = $issue . "RUNNING ON BATTERY! ";
        }
	elsif ($upsPhaseInputVoltage1 < $critical_low) {
            $issue = $issue . "LOW VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseInputVoltage1 > $critical_high) {
            $issue = $issue . "HIGH VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseInputVoltage1 < $warning_low) {
            $issue = $issue . "LOW VOLTAGE WARNING! ";
            $status = 1;
        }
        elsif ($upsPhaseInputVoltage1 > $warning_high) {
            $issue = $issue . "HIGH VOLTAGE WARNING! ";
            $status = 1;
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "input_voltagePhase2") {
        if (!defined($s->get_request($oid_upsPhaseInputVoltage2))) {
            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()) {
             $upsPhaseInputVoltage2 = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "110:126"; }
	unless (defined($critical)) { $critical = "106:127"; }
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
        if ($upsPhaseInputVoltage2 < 1){
            $status = 2;
            $issue = $issue . "RUNNING ON BATTERY! ";
        }
	elsif ($upsPhaseInputVoltage2 < $critical_low) {
            $issue = $issue . "LOW VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseInputVoltage2 > $critical_high) {
            $issue = $issue . "HIGH VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseInputVoltage2 < $warning_low) {
            $issue = $issue . "LOW VOLTAGE WARNING! ";
            $status = 1;
        }
        elsif ($upsPhaseInputVoltage2 > $warning_high) {
            $issue = $issue . "HIGH VOLTAGE WARNING! ";
            $status = 1;
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "input_frequency") {
        if (!defined($s->get_request($oid_input_frequency))) {
            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()) {
             $input_frequency = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "59:61"; }
	unless (defined($critical)) { $critical = "55:65"; }
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
	if ($input_frequency < $critical_low) {
            $issue = $issue . "LOW FREQUENCY CRITICAL! ";
            $status = 2;
        }
        elsif ($input_frequency > $critical_high) {
            $issue = $issue . "HIGH FREQUENCY CRITICAL! ";
            $status = 2;
        }
        elsif ($input_frequency < $warning_low) {
            $issue = $issue . "LOW FREQUENCY WARNING! ";
            $status = 1;
        }
        elsif ($input_frequency > $warning_high) {
            $issue = $issue . "HIGH FREQUENCY WARNING! ";
            $status = 1;
        }

    }

    #######################################################
 
    if ($check eq "all" || $check eq "input_reasonforlasttransfer") {
        if (!defined($s->get_request($oid_input_reasonforlasttransfer))) {
            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()) {
             $input_reasonforlasttransfer = $s->var_bind_list()->{$_};
        }
        if ($input_reasonforlasttransfer eq "1"){
            $outagecause = "No events"
        }
        elsif ($input_reasonforlasttransfer eq "2"){
            $outagecause = "High line voltage"
        }
        elsif ($input_reasonforlasttransfer eq "3"){
            $outagecause = "Brownout"
        }
        elsif ($input_reasonforlasttransfer eq "4"){
            $outagecause = "Loss of mains power"
        }
        elsif ($input_reasonforlasttransfer eq "5"){
            $outagecause = "Small temporary power drop"
        }
        elsif ($input_reasonforlasttransfer eq "6"){
            $outagecause = "Large temporary power drop"
        }
        elsif ($input_reasonforlasttransfer eq "7"){
            $outagecause = "Small spike"
        }
        elsif ($input_reasonforlasttransfer eq "8"){
            $outagecause = "Large spike"
        }
        elsif ($input_reasonforlasttransfer eq "9"){
            $outagecause = "UPS self test"
        }
        elsif ($input_reasonforlasttransfer eq "10"){
            $outagecause = "Excessive input voltage fluctuation"
        }
        else {
            $outagecause = "Cannot establish reason"
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "output_voltage") {
        if (!defined($s->get_request($oid_output_voltage))) {
            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()) {
             $output_voltage = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "110:126"; }
	unless (defined($critical)) { $critical = "106:127"; } 
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
        if ($output_voltage < 1){
            $status = 2;
            $issue = $issue . "RUNNING ON BATTERY! ";
        }
	elsif ($output_voltage < $critical_low) {
            $issue = $issue . "LOW VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($output_voltage > $critical_high) {
            $issue = $issue . "HIGH VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($output_voltage < $warning_low) {
            $issue = $issue . "LOW VOLTAGE WARNING! ";
            $status = 1;
        }
        elsif ($output_voltage > $warning_high) {
            $issue = $issue . "HIGH VOLTAGE WARNING! ";
            $status = 1;
        }
    }

    #######################################################

    if ($check eq "all" || $check eq "output_voltagePhase1") {
        if (!defined($s->get_request($oid_upsPhaseOutputVoltage1))) {
            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()) {
             $upsPhaseOutputVoltage1 = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "110:126"; }
	unless (defined($critical)) { $critical = "106:127"; }
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
        if ($upsPhaseOutputVoltage1 < 1){
            $status = 2;
            $issue = $issue . "RUNNING ON BATTERY! ";
        }
	elsif ($upsPhaseOutputVoltage1 < $critical_low) {
            $issue = $issue . "LOW VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseOutputVoltage1 > $critical_high) {
            $issue = $issue . "HIGH VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseOutputVoltage1 < $warning_low) {
            $issue = $issue . "LOW VOLTAGE WARNING! ";
            $status = 1;
        }
        elsif ($upsPhaseOutputVoltage1 > $warning_high) {
            $issue = $issue . "HIGH VOLTAGE WARNING! ";
            $status = 1;
        }
    }

    #######################################################

    if ($check eq "all" || $check eq "output_voltagePhase2") {
        if (!defined($s->get_request($oid_upsPhaseOutputVoltage2))) {
            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()) {
             $upsPhaseOutputVoltage2 = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "110:126"; }
	unless (defined($critical)) { $critical = "106:127"; }
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
        if ($upsPhaseOutputVoltage2 < 1){
            $status = 2;
            $issue = $issue . "RUNNING ON BATTERY! ";
        }
	elsif ($upsPhaseOutputVoltage2 < $critical_low) {
            $issue = $issue . "LOW VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseOutputVoltage2 > $critical_high) {
            $issue = $issue . "HIGH VOLTAGE CRITICAL! ";
            $status = 2;
        }
        elsif ($upsPhaseOutputVoltage2 < $warning_low) {
            $issue = $issue . "LOW VOLTAGE WARNING! ";
            $status = 1;
        }
        elsif ($upsPhaseOutputVoltage2 > $warning_high) {
            $issue = $issue . "HIGH VOLTAGE WARNING! ";
            $status = 1;
        }
    }

    #######################################################

    if ($check eq "all" || $check eq "output_frequency") {
        if (!defined($s->get_request($oid_output_frequency))) {
            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()) {
             $output_frequency = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = "59:61"; }
	unless (defined($critical)) { $critical = "55:65"; }
	($warning_low, $warning_high) = split(/:/, $warning);
	($critical_low, $critical_high) = split(/:/, $critical);
	if ($output_frequency < $critical_low) {
            $issue = $issue . "LOW FREQUENCY CRITICAL! ";
            $status = 2;
        }
        elsif ($output_frequency > $critical_high) {
            $issue = $issue . "HIGH FREQUENCY CRITICAL! ";
            $status = 2;
        }
        elsif ($output_frequency < $warning_low) {
            $issue = $issue . "LOW FREQUENCY WARNING! ";
            $status = 1;
        }
        elsif ($output_frequency > $warning_high) {
            $issue = $issue . "HIGH FREQUENCY WARNING! ";
            $status = 1;
        }

    }

    #######################################################
 
    if ($check eq "all" || $check eq "output_load") {
        if (!defined($s->get_request($oid_output_load))) {
            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()) {
             $output_load = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = 80; }
	unless (defined($critical)) { $critical = 90; }
        if ($output_load > $critical) {
            $issue = $issue . "HIGH OUTPUT LOAD! ";
            $status = 2;
        }
        elsif ($output_load > $warning) {
            $status = 1;
            $issue = $issue . "OUTPUT LOAD WARNING! ";
        }
    }

    #######################################################
 
    if ($check eq "all" || $check eq "test_result") {
        if (!defined($s->get_request($oid_test_result))) {
            if (!defined($s->get_request($oid_sysDescr))) {
                $returnstring = "SNMP agent not responding";
                $status = 3;
                return 1;
            }
            else {
                $returnstring = "SNMP OID does not exist";
                $status = 3;
                return 1;
            }
        }
        foreach ($s->var_bind_names()) {
             $test_result = $s->var_bind_list()->{$_};
        }

        if ($test_result eq "1") {
            $test_result_string = "passed";
        }
        elsif ($test_result eq "2") {
            $test_result_string = "failed";
            $issue = $issue . "SELF TEST FAILED! ";
            $status = 2;
        }
        elsif ($test_result eq "4") {
            $test_result_string = "in progress";
        }
        else {
            $test_result_string = "unknown";
	    $status = 3;
        }
    }

    #######################################################

    if ($check eq "all" || $check eq "test_result") {
        if (!defined($s->get_request($oid_test_date))) {
            if (!defined($s->get_request($oid_sysDescr))) {
                $returnstring = "SNMP agent not responding";
                $status = 3;
                return 1;
            }
            else {
                $returnstring = "SNMP OID does not exist";
                $status = 3;
                return 1;
            }
        }
        foreach ($s->var_bind_names()) {
             $test_date = $s->var_bind_list()->{$_};
        }
    }

    #######################################################

	if ($check eq "all" || $check eq "ambientTemp_Port1") {
        if (!defined($s->get_request($oid_ambientTemp1))) {
            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()) {
             $ambient_temperature = $s->var_bind_list()->{$_};
        }
	if($converttemp){
		unless (defined($warning)) { $warning = "40:85"; }
        	unless (defined($critical)) { $critical = "35:95"; }
		($warning_low, $warning_high) = split(/:/, $warning);
		($critical_low, $critical_high) = split(/:/, $critical);
	}
	else {
		unless (defined($warning)) { $warning = $warning = "4.4:29.4"; }
		unless (defined($critical)) { $critical = $critical = "1.67:35"; }
		($warning_low, $warning_high) = split(/:/, $warning);
                ($critical_low, $critical_high) = split(/:/, $critical);
	}
    }

    #######################################################

        if ($check eq "all" || $check eq "ambientTemp_Port2") {
        if (!defined($s->get_request($oid_ambientTemp1))) {
            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()) {
             $ambient_temperature = $s->var_bind_list()->{$_};
        }
	if($converttemp){
                unless (defined($warning)) { $warning = "40:85"; }
                unless (defined($critical)) { $critical = "35:95"; }
                ($warning_low, $warning_high) = split(/:/, $warning);
                ($critical_low, $critical_high) = split(/:/, $critical);
        }
        else {
                unless (defined($warning)) { $warning = $warning = "4.4:29.4"; }
                unless (defined($critical)) { $critical = $critical = "1.67:35"; }
                ($warning_low, $warning_high) = split(/:/, $warning);
                ($critical_low, $critical_high) = split(/:/, $critical);
        }
    }

    #######################################################

        if ($check eq "all" || $check eq "humidity" || $check eq "humidity_Port1") {
        if (!defined($s->get_request($oid_humidity1))) {
            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()) {
             $humidity = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = $warning = "40:60"; }
        unless (defined($critical)) { $critical = $critical = "30:70"; }
        ($warning_low, $warning_high) = split(/:/, $warning);
        ($critical_low, $critical_high) = split(/:/, $critical);
    }
    #######################################################

        if ($check eq "humidity_Port2") {
        if (!defined($s->get_request($oid_humidity2))) {
            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()) {
             $humidity = $s->var_bind_list()->{$_};
        }
	unless (defined($warning)) { $warning = $warning = "40:60"; }
        unless (defined($critical)) { $critical = $critical = "30:70"; }
        ($warning_low, $warning_high) = split(/:/, $warning);
        ($critical_low, $critical_high) = split(/:/, $critical);
    }
    #######################################################
     
    if ($check eq "comms") {
    	if ($status == 0) {
            append("SNMP agent is communicating with $upstype");
        }
    }
    if ($check eq "battery_capacity") {
        append("Battery capacity: $battery_capacity%");
##1
        append("|Battery_Capacity=$battery_capacity%;$warning;$critical");
##1
    }
    if ($check eq "battery_temperature") {
	if ($converttemp) {
	    $battery_temperature = $battery_temperature*1.8+32;
            append("Battery temperature: $battery_temperature degrees F");
##1
            append("|Battery_Temp=$battery_temperature");
	    append("F;$warning;$critical");
##1
        }
        else {
            append("Battery temperature: $battery_temperature degrees C");
##1
            append("|Battery_Temp=$battery_temperature");
	    append("C;$warning;$critical");
##1
        }
    }
    if ($check eq "battery_runtimeremain") {
        append("Battery run time remaining: $battery_runtimeremain minutes");
##1
	append("|Battery_Runtime=$battery_runtimeremain;$warning;$critical");
##1
    }
    if ($check eq "battery_replace") {
        if ($status == 0) {
            append("Battery does not need to be replaced at this time");
        }
	if ($status == 2) { 
            append("Battery needs replaced!");
	}
    }
    if ($check eq "input_voltage") {
        append("Input voltage: $input_voltage V AC");
##1
	append("|Input_Voltage=$input_voltage");
	append("V;$warning;$critical");
##1
    }
    if ($check eq "input_voltagePhase1") {
        append("Input voltage: $upsPhaseInputVoltage1 V AC");
##1
	append("|Input_Voltage=$upsPhaseInputVoltage1");
	append("V;$warning;$critical");
##1
    }
    if ($check eq "input_voltagePhase2") {
        append("Input voltage: $upsPhaseInputVoltage2 V AC");
##1
	append("|Input_Voltage=$upsPhaseInputVoltage2");
	append("V;$warning;$critical");
##1
    }
    if ($check eq "input_frequency") {
        append("Input frequency: $input_frequency Hz");
##1
	append("|Input_Frequency=$input_frequency");
	append("Hz;$warning;$critical");
##1
    }
    if ($check eq "input_reasonforlasttransfer") {
        append("Reason for last transfer to battery: $outagecause");
    }
    if ($check eq "output_voltage") {
        append("Output voltage: $output_voltage V AC");
##1
	append("|Output_Voltage=$output_voltage");
	append("V;$warning;$critical");
##1
    }
    if ($check eq "output_voltagePhase1") {
        append("Output voltage: $upsPhaseOutputVoltage1 V AC");
##1
	append("|Output_Voltage=$upsPhaseOutputVoltage1");
	append("V;$warning;$critical");
##1
    }
    if ($check eq "output_voltagePhase2") {
        append("Output voltage: $upsPhaseOutputVoltage2 V AC");
##1
	append("|Output_Voltage=$upsPhaseOutputVoltage2");
	append("V;$warning;$critical");
##1
    }
    if ($check eq "output_frequency") {
        append("Output frequency: $output_frequency Hz");
##1
	append("|Output_Frequency=$output_frequency");
	append("Hz;$warning;$critical");
##1
    }
    if ($check eq "output_load") {
        append("Output load: $output_load%");
##1
	append("|Output_Load=$output_load%;$warning;$critical");
##1
    }
    if ($check eq "test_result") {
        append("Self test $test_result_string on $test_date");
    }

    if ($check eq "all") {

        if ($status == 0){
            $temp = sprintf "$upstype - BATTERY:(capacity $battery_capacity%%, temperature $battery_temperature C, runtime $battery_runtimeremain) INPUT:(voltage $input_voltage V, frequency $input_frequency Hz) OUTPUT:(voltage $output_voltage V, frequency $output_frequency Hz, load $output_load%%) SELF TEST:($test_result_string on $test_date) LAST EVENT:($outagecause)";
        }
        else {
            $temp = sprintf "$upstype - BATTERY:(capacity $battery_capacity%%, temperature $battery_temperature C, runtime $battery_runtimeremain) INPUT:(voltage $input_voltage V, frequency $input_frequency Hz) OUTPUT:(voltage $output_voltage V, frequency $output_frequency Hz, load $output_load%%)    LAST EVENT:$outagecause";
        }
        append($temp);
    }

    #######################################################

if ($check eq "ambientTemp_Port1") {
        if ($converttemp) {
            $ambient_temperature = $ambient_temperature*1.8+32;
        	if ($ambient_temperature < $critical_low) {
                    $issue = $issue . "AMBIENT TEMPERATURE TOO LOW CRITICAL! ";
                    $status = 2;
                }
                elsif ($ambient_temperature > $critical_high) {
                    $issue = $issue . "AMBIENT TEMPERATURE TOO HIGH CRITICAL! ";
                    $status = 2;
                }
                elsif ($ambient_temperature < $warning_low) {
                    $issue = $issue . "AMBIENT TEMPERATURE LOW WARNING! ";
                    $status = 1;
                }
                elsif ($ambient_temperature > $warning_high) {
                    $issue = $issue . "AMBIENT TEMPERATURE HIGH WARNING! ";
                    $status = 1;
                }
		append("Ambient_Temperature: $ambient_temperature degrees F");
	}
        else {
		if ($ambient_temperature < $critical_low) {
	            $issue = $issue . "AMBIENT TEMPERATURE TOO LOW CRITICAL! ";
	            $status = 2;
	        }
	        elsif ($ambient_temperature > $critical_high) {
	            $issue = $issue . "AMBIENT TEMPERATURE TOO HIGH CRITICAL! ";
	            $status = 2;
	        }
	        elsif ($ambient_temperature < $warning_low) {
	            $issue = $issue . "AMBIENT TEMPERATURE LOW WARNING! ";
	            $status = 1;
	        }
	        elsif ($ambient_temperature > $warning_high) {
	            $issue = $issue . "AMBIENT TEMPERATURE HIGH WARNING! ";
	            $status = 1;
		}
		append("Ambient_Temperature: $ambient_temperature degrees C");
	}
        append("|Ambient_Temperature=$ambient_temperature");
        append("C;$warning;$critical");
}

if ($check eq "ambientTemp_Port2") {
	if ($converttemp) {
		$ambient_temperature = $ambient_temperature*1.8+32;
        	if ($ambient_temperature < $critical_low) {
                    $issue = $issue . "AMBIENT TEMPERATURE TOO LOW CRITICAL! ";
                    $status = 2;
                }
                elsif ($ambient_temperature > $critical_high) {
                    $issue = $issue . "AMBIENT TEMPERATURE TOO HIGH CRITICAL! ";
                    $status = 2;
                }
                elsif ($ambient_temperature < $warning_low) {
                    $issue = $issue . "AMBIENT TEMPERATURE LOW WARNING! ";
                    $status = 1;
                }
                elsif ($ambient_temperature > $warning_high) {
                    $issue = $issue . "AMBIENT TEMPERATURE WARNING! ";
                    $status = 1;
                }
                append("Ambient_Temperature: $ambient_temperature degrees F");
	}
	else {
		if ($ambient_temperature < $critical_low) {
                    $issue = $issue . "AMBIENT TEMPERATURE TOO LOW CRITICAL! ";
                    $status = 2;
                }
                elsif ($ambient_temperature > $critical_high) {
                    $issue = $issue . "AMBIENT TEMPERATURE TOO HIGH CRITICAL! ";
                    $status = 2;
                }
                elsif ($ambient_temperature < $warning_low) {
                    $issue = $issue . "AMBIENT TEMPERATURE LOW WARNING! ";
                    $status = 1;
                }
                elsif ($ambient_temperature > $warning_high) {
                    $issue = $issue . "AMBIENT TEMPERATURE WARNING! ";
                    $status = 1;
                }
                append("Ambient_Temperature: $ambient_temperature degrees C");
        }
        append("|Ambient_Temperature=$ambient_temperature");
        append("C;$warning;$critical");

    }

if ($check eq "humidity" || $check eq "humidity_Port1") {
	if ($humidity < $critical_low) {
		$issue = $issue . "AMBIENT HUMIDITY TOO LOW CRITICAL! ";
		$status = 2;
	}
	elsif ($humidity > $critical_high) {
		$issue = $issue . "AMBIENT HUMIDITY TOO HIGH CRITICAL! ";
		$status = 2;
	}
	elsif ($humidity < $warning_low) {
		$issue = $issue . "AMBIENT HUMIDITY LOW WARNING! ";
		$status = 1;
	}
	elsif ($humidity > $warning_high) {
		$issue = $issue . "AMBIENT HUMIDITY WARNING! ";
		$status = 1;
	}
	append("Ambient Humidity=$humidity%");
	append("|Ambient_Humidity=$humidity");
	append("%;$warning;$critical");
}

if ($check eq "humidity_Port2") {
	if ($humidity < $critical_low) {
		$issue = $issue . "AMBIENT HUMIDITY TOO LOW CRITICAL! ";
		$status = 2;
	}
	elsif ($humidity > $critical_high) {
		$issue = $issue . "AMBIENT HUMIDITY TOO HIGH CRITICAL! ";
		$status = 2;
	}
	elsif ($humidity < $warning_low) {
		$issue = $issue . "AMBIENT HUMIDITY LOW WARNING! ";
		$status = 1;
	}
	elsif ($humidity > $warning_high) {
		$issue = $issue . "AMBIENT HUMIDITY WARNING! ";
		$status = 1;
	}
	append("Ambient Humidity=$humidity%");
	append("|Ambient_Humidity=$humidity");
	append("%;$warning;$critical");
}

} #end of Main()


####################################################################
# help and usage information                                       #
####################################################################

sub usage {
    print << "USAGE";
-----------------------------------------------------------------	 
$script v$script_version

Monitors APC SmartUPS via AP9617 SNMP management card.

Usage: $script -H <hostname> -c <community> [...]

Options: -H 	Hostname or IP address
         -C 	Community (default is public)
	 
-----------------------------------------------------------------	 
Copyright 2004 Altinity Limited	 
	 
This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
-----------------------------------------------------------------

USAGE
     exit 1;
}

####################################################################
# Appends string to existing $returnstring                         #
####################################################################

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

