#
# Host is $args{"host"}, host alias is $args{"alias"}
#
my $host = $args{"host"};
my $alias = $args{"alias"};
my $snmp;

#
# Create an evaluator check object for "ups"
#
$args{"_eval"} = "ups";
do "_evaluator";

#
# create snmp query object
#
my $community = $args{"community"}?$args{"community"}:"public";
unless( $snmp = $memory{"snmp_$args{host}_$community_$args{frequency}"} ) {
    &uxmon::load_module( "snmp" );
    &uxmon::checker(
	$memory{"snmp_$args{host}_$community_$args{frequency}"} = $snmp = Monitor::snmp->new( $host, $community )
    );
    $snmp->sched_frequency( $args{"frequency"} );
}


#
# register all the necessary variables our SNMP object should poll
#
my $query = $args{"query"};
if( $query eq "longtime" ) {
    $snmp->add_walk( "upsOutputPercentLoad" );
    $snmp->add_walk( "upsOutputPower" );
    $snmp->add_walk( "upsBatteryVoltage" );
}
else {
    my %source_types = (
	1 => "other",
	2 => "none",
	3 => "normal",
	4 => "bypass",
	5 => "battery",
	6 => "booster",
	7 => "reducer"
    );
    $snmp->add_walk( "upsOutputSource" );
    $snmp->add_walk( "upsInputLineBads" );

    my $alias = $args{"alias"};
    my $check = $args{"check"}?$args{"check"}:"ups";
    my $linebads = {};

    #
    # add an evaluator based check against the queried variables
    #
    $args{"_evaluator"}->add_check( ($args{"check"}?$args{"check"}:"ups"), sub {
	my( $vars, $monitor ) = @_;

	my $text = "&html <HR><H3>UPS Status</H3>";
	my $status = 3;
        my $status_text = "";
	$lines = $vars->{"snmp.upsOutputPercentLoad"};
	my $number;
	foreach my $line (@$lines) {
	    my $power = $vars->{$line};
	    ($number = $line) =~ s/^[^\d]*//;
	    if( $power ) {
	        $text .= "Output Load $number: $power %<BR>";
	    }
	}
	$lines = $vars->{"snmp.upsOutputPower"};
	foreach my $line (@$lines) {
	    my $power = $vars->{$line};
	    ($number = $line) =~ s/^[^\d]*//;
	    if( $power ) {
	        $text .= "Output Power $number: $power W<BR>";
	    }
	}
	$lines = $vars->{"snmp.upsBatteryVoltage"};
	foreach my $line (@$lines) {
	    my $battery = $vars->{$line} /10;
	    ($number = $line) =~ s/^[^\d]*//;
	    if( $battery ) {
		    $text .= "Battery Voltage $number: $battery V<BR>";
	    }
	}
	$lines = $vars->{"snmp.upsInputLineBads"};
	foreach my $line (@$lines) {
	    my $bads = $vars->{$line};
	    ($number = $line) =~ s/^[^\d]*//;
	    my $former = $linebads->{$line};
	    if( ! $former ) {
		$former = $linebads->{$line} = [ time, $bads ];
	    }
	    if( $former->[1] < $bads ) {
		$status = 1;
		$status_text .= "UPS line bad during last hour";
		if( $former->[0] < time - 3600 ) {
		    $former->[0] = time;
		    $former->[1] = $bads;
		}
	    }
	    else {
		$former->[0] = time;
		$former->[1] = $bads;
	    }
	    $text .= "Line Bads $number: $bads<BR>";
	}
	$lines = $vars->{"snmp.upsOutputSource"};
	foreach my $line (@$lines) {
	    my $source = $vars->{$line};
	    if( defined $source_types{$source} ) {
	 	$source = $source_types{$source};
	    }
	    ($number = $line) =~ s/^[^\d]*//;
	    if( $source ) {
		if( $status_text ) { $status_text = ", " }
	        if( $source eq "normal" ) {
		    $status_text = "UPS pwr '$source' OK";
		}
		else {
		    $status_text = "UPS pwr '$source' - FAILED";
		    $status = 0;
		}
	        $text .= "Output Source $number: $source<BR>";
	    }
	}

	$text .= "<HR>";

	$monitor->report( $alias.".".$check, 101, $text );
	return( $status, $status_text );
      } );
}


sub _compile {
    my( $expr ) = @_;

    foreach my $query (split( ",", $args{"query"})) {
	$expr =~ s/\$$query/\(\$vars->{"snmp.$query"}\)/g;
    }
    return $expr;
}

1;
