#!/usr/bin/perl
#
# apc_checks.pl
#
##########################################
###
### This is used for the oemreport Temperature check command
### SNMP OIDs are based on APC 6000RT UPS
###
### Updated: 2/16/2018
### Chris Maroney
###
##########################################
#
# Generate initial variables
#
$statusColor=""; # Hobbit Status color for report
$ReadingI=""; # Clear Reading variable
$ReadingO=""; # Clear Reading variable
$ReadBatt=""; # Clear Reading variable
$ReadTime=""; # Clear Reading variable
$voltCol="volt"; # Xymon Voltage Column
$batCol="batt"; # Xymon Battery Column
$tempReading=""; # Clear temp variable
$tempCol="TempC"; # Xymon TempC Column
$tempMin="26"; # Minimum Warning Threshold in Celsium
$tempMax="30"; # Maximum Warning Threshold in Celsium
$tempDate = `date`; # Get current date
$UPS = "FQN-UPS-HOSTNAME"; # UPS Hostname
chomp $tempDate; # Take off the end character date
#
# get local time and insert information in an array
#
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#
# Set the hostname variable
#
# $hostname = `hostname`; # Generate local hostname. Server independent
# NEW Temp Reading
$tempReading = `snmpwalk "$UPS" -v 1 -c CATSPUB .1.3.6.1.4.1.318.1.1.1.2.2.2.0`;
@value = split(/Gauge32\:\ /,$tempReading);
$tempReading = $value[1];
chomp($tempReading);
#chop($tempReading);
print ">$tempReading<\n";
$f=($tempReading*(9/5))+32;
open(OUT,">>/usr/share/xymon-client/ext/logs/$UPS");
print OUT "$tempDate:$f\n";
close(OUT);
if ( $tempReading >= $tempMax ) {
$statusOutput = "UPS located in Bldg BLAH / Room BLAH / Rack BLAH is currently $tempReading C / $f F. Temperature is critical.\n\nReading : $tempReading\n";
$statusColor = "red";
} elsif (( $tempReading > $tempMin ) || ( $tempReading < tempMax )) {
$statusOutput = "UPS located in Bldg BLAH / Room BLAH / Rack BLAH is currently $tempReading C / $f F. Temperature is approaching critical.\n\nReading : $tempReading\n";
$statusColor = "yellow";
} else {
$statusOutput = "UPS located in Bldg BLAH / Room BLAH / Rack BLAH is currently $tempReading C / $f F. Temperature is OK.\n\nReading : $tempReading\n";
$statusColor = "green";
}
# Generate script to display temp report in Hobbit
#
$tempCheck = "status $UPS.". "$tempCol $statusColor ".`date`;
#$tempCheck = trim($tempCheck);
$tempCheck = $tempCheck. $statusOutput."\n\n";
$ReadingI = `snmpwalk "$UPS" -v 1 -c CATSPUB .1.3.6.1.4.1.318.1.1.1.3.2.1.0`;
@value = split(/\:\ /,$ReadingI);
$ReadingI = $value[1];
chomp($ReadingI);
print ">$ReadingI<\n";
chop($ReadingO);
$ReadingO = `snmpwalk "$UPS" -v 1 -c CATSPUB .1.3.6.1.4.1.318.1.1.1.4.2.1.0`;
@value = split(/\:\ /,$ReadingO);
$ReadingO = $value[1];
chomp($ReadingO);
print ">$ReadingO<\n";
if ( $ReadingI == 0 ) {
$statusOutput = "UPS OFF Commercial Power. PowerChute will begin shutdown if not restored. \n\nInputVoltage : $ReadingI \nOutputVoltage : $ReadingO \n";
$statusColor = "red";
} elsif (( $ReadingI < 200 ) || ( $ReadingI < 1 )) {
$statusOutput = "UPS has Low Commercial Power. Report Low Voltage alarms.\n\nInputVoltage : $ReadingI \nOutputVoltage : $ReadingO \n";
$statusColor = "yellow";
} else {
$statusOutput = "UPS is OK. Running On Commercial Power.\n\nInputVoltage : $ReadingI \nOutputVoltage : $ReadingO \n";
$statusColor = "green";
}
# Generate script to display volt report in Xymon
#
$voltCheck = "status $UPS.". "$voltCol $statusColor ".`date`;
$voltCheck = $voltCheck. $statusOutput."\n\n";
#1.3.6.1.4.1.318.1.1.1.2.2.3.0
$ReadBatt = `snmpwalk "$UPS" -v 1 -c CATSPUB .1.3.6.1.4.1.318.1.1.1.2.2.1.0`;
@value = split(/\:\ /,$ReadBatt);
$ReadBatt = $value[1];
chomp($ReadBatt);
print ">$ReadBatt<\n";
$ReadTime = `snmpwalk "$UPS" -v 1 -c CATSPUB .1.3.6.1.4.1.318.1.1.1.2.2.3.0`;
@value = split(/\:/,$ReadTime);
$ReadTime = $value[4];
chomp($ReadTime);
#chop($ReadTime);
print ">$ReadTime<\n";
if ( $ReadBatt < 75 ) {
# $statusOutput = "Battery is either Charging, Discharging or Requires maintenance ( % ). \n\nChargeLevel : $ReadBatt \n";
$statusOutput = "UPS Battery Charge is currently $ReadBatt % and is either Charging, Discharging or Requires maintenance! \n\nBattery Runtime has $ReadTime minutes remaining. \n\nBatteryTime : $ReadTime \n";
$statusColor = "yellow";
} else {
$statusOutput = "UPS Battery is $ReadBatt % Charged. \n\nStandby Battery Runtime is $ReadTime minutes.\n\nBatteryTime : $ReadTime \n";
$statusColor = "green";
}
# Generate script to display batt report in Hobbit
#
$battCheck = "status $UPS.". "$batCol $statusColor ".`date`;
$battCheck = $battCheck. $statusOutput."\n\n";
#$f=($Reading*(9/5))+32;
open(OUT,">>/usr/share/xymon-client/ext/logs/$UPS");
print OUT "$tempDate:$f\n";
close(OUT);
#
# Print variables to screen when command is manually executed
#
print <<END;
$statusOutput
$statusColor
END
# Call script to send report to Xymon server
print "$tempCheck\n";
`/usr/share/xymon-client/bin/xymon $XYMONSERVER "$tempCheck"`;
# Call script to send report to Xymon server
print "$voltCheck\n";
`/usr/share/xymon-client/bin/xymon $XYMONSERVER "$voltCheck"`;
# Call script to send report to Xymon server
print "$battCheck\n";
`/usr/share/xymon-client/bin/xymon $XYMONSERVER "$battCheck"`;
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}