#!/usr/pkg/bin/perl
#
# $Header: /home/vikas/src/nocol/perlnocol/RCS/upsmon,v 1.1 1999/10/31 17:36:38 vikas Exp $
#
# A program to update stats related to a UPS.  It is designed to be
# called from a separate monitoring script rather than it doing the
# monitoring itself.
# Unlike APCmon, this is designed to run out of some other script.
# It is called to set various status's from within other scripts.
# This keeps track of status via the status file.
#
# Written by: Frank Crawford - <frank@ansto.gov.au> Jan 1999
#
# Called as:
#
#	upsmon [0|1] [<Address>] [<Host>]
# where:
#	0|1		up (1) or down (0)
#	<Address>	address field (e.g. "SOLA-51/900")
#	<Host>		host field (default to local host)
#
# e.g.:
#	/usr/local/nocol/bin/upsmon 1 "SOLA-51/900"
#
#
## global variables required in the nocollib.pl library
$varname = 'AC_Power';
$varunits= 'Avail' ;
$sender = 'upsmon';

############################
#push (@INC, '/usr/local/nocol/bin');	# location of nocollib.pl
require "nocollib.pl" ;

$debug = 0;				# set to 1 for debugging output
$libdebug = 0;				# set to 1 for debugging output
$prognm = $0;				# save program name
$datafile = "$datadir/$sender-output";

$status = shift(@ARGV) || 0;
$address = shift(@ARGV) || '-';
$host = shift(@ARGV);

chop($host = `hostname -s`) unless $host;

$allitems = $item = 0;

if (open(IEVENTS,"<$datafile")) {
    while (&readevent(IEVENTS, ++$allitems)) {
	$item = $allitems
	    if $sitename{$allitems} eq $host && $siteaddr{$allitems} eq $address;
    }
    close(IEVENTS);
}

if ($item) {
    $allitems--;
} else {
    $item = $allitems;
    &init_event ($host, $address, $item); # fill in initial values
}

$varvalue{$item} = $status;

# The event gets written directly at the level instead
# of escalating gradually like typical nocol monitors
$severity{$item} = $E_CRITICAL + 1 if !$status && $severity{$item} != $E_CRITICAL;

&update_event($item, $status, $status, $E_CRITICAL) unless $status eq '-';

open(OEVENTS,">$datafile");
for $item (1 .. $allitems) {
    &writeevent(OEVENTS, $item) unless $varvalue{$item} eq '-';
}
close(OEVENTS);

exit(0);
