#!/usr/bin/perl -w

# availability report module
#
# Version: $Revision: 0.3 $
# Author: Benjamin Oshrin
# Date: $Date: 2005/11/11 03:57:15 $
#
# Copyright (c) 2004 - 2005
# The Trustees of Columbia University in the City of New York
# Academic Information Systems
# 
# License restrictions apply, see doc/license.html for details.

use strict;
use FindBin;
use lib "$FindBin::Bin/../common";
use Survivor::Report;

# Create a new Report object.

my $sw = new Survivor::Report();

my $pok = $sw->ParseReportRequest();

if($pok == MODEXEC_OK) {
    if($sw->Style() eq "html")
    {
	print ("<HTML><PRE>\n");
	print ("<H1>Availability Report</H1>\n");
    }
    elsif($sw->Style() eq "text")
    {
	print ("Availability Report\n\n");
    }

    my @dataset = $sw->DataSet();
    
    foreach my $d (@dataset)
    {
	my $up = 0;
	my $seen = 0;

	foreach my $cr (@{${%$d}{"checkdata"}}) {
	    $seen++;
	    
	    if(${%$cr}{"checkrc"} == MODEXEC_OK) {
		$up++;
	    }
	}

	my $avail = 0;

	if($seen > 0) {
	    $avail = ($up / $seen) * 100;
	}
	
        if($sw->Style() eq "check") {
	    print ${%$d}{"service"} . " " . ${%$d}{"host"} . " ";
            print $avail . " " . $up . " " . $seen . "\n";
	} else {
	    print ${%$d}{"service"} . "@" . ${%$d}{"host"} . ": ";
            print $avail . "% (" . $up . "/" . $seen . ")\n";
        }
    }
    
    if($sw->Style() eq "html")
    {
	print ("</PRE></HTML>\n");
    }
} else {
    print "Error: $pok\n";
}

exit MODEXEC_OK;
