#!/usr/bin/perl
# upsremainingbatt, Boone, 05/27/99
# How much battery capacity remains?
#
# Modifications:
# 05/27/99 Boone      Initial coding
# 11/29/04 Boone      Newer versions (5.? and later) of the Perl SNMP
#                     module default to SNMPv3, but our UPS doesn't
#                     speak anything later than SNMPv1; specify the SNMP
#                     version in the session setup
# 04/30/08 Boone      Community string from command line
# 12/02/09 Boone      Default MIB dir seems to have changed at some version
#                     of the underlying SNMP kit; explicitly add both known
#                     options to the search list
# End Modifications

# Libraries

	use SNMP 1.8;

# Initialize

	$host = shift(@ARGV);
	$comm = shift(@ARGV);

# Set up connection

	$SNMP::debugging = 0;
	$SNMP::use_enums = 1;
	&SNMP::addMibDirs("/usr/share/snmp/mibs", "/usr/share/mibs");
	&SNMP::initMib();
	&SNMP::loadModules("PowerNet-MIB");
	$sess = new SNMP::Session(DestHost => $host, Community => $comm,
		Version => 1) ||
		die "new session failed: " . $sess -> {ErrorStr};

# Extract items

	$pctcap = $sess -> get("upsAdvBatteryCapacity.0");
	if ($sess -> {ErrorStr})
	{
		die "get failed: " . $sess->{ErrorStr};
	}

	$remrun = $sess -> get("upsAdvBatteryRunTimeRemaining.0");
	if ($sess -> {ErrorStr})
	{
		die "get failed: " . $sess->{ErrorStr};
	}

	print $pctcap, " ", ($remrun / 6000), "\n";
