#!/usr/local/perl/bin/perl -w
#
# Parse the outputs from Nagios' avail.cgi into an HTML table.


use Date::Manip;
use LWP::UserAgent;
use Time::Local;
use Data::Dumper;
$~ = ReportFormat;
@stime = ("07","00",00);
@etime = (18,30,00);

$date = `date +"\%m \%d \%Y"`;  #month date year
chomp $date;

($mon,$day,$year)=split ' ', $date;

#$sec1x = &UnixDate ($epoch_date1x, "%s");
#$sec2x = &UnixDate ($epoch_date2x, "%s");

use vars qw($agent);
#use strict;

my $nagios_url = "http://yoursite/nagios/cgi-bin";

$agent = new LWP::UserAgent();
$agent->agent("Mozilla/6.0");
$agent->timeout(600);

    my $request = new HTTP::Request(GET => "$nagios_url/avail.cgi?show_log_entries=&host=all&timeperiod=custom&smon=$mon&sday=$day&syear=$year&shour=$stime[0]&smin=$stime[1]&ssec=$stime[2]
                    &emon=$mon&eday=$day&eyear=$year&ehour=$etime[0]&emin=$etime[1]&esec=$etime[2]&assumeinitialstates=yes&assumestateretention=yes&initialassumedstate=-1&backtrack=4&csvoutput=");

    $request->header('Accept' => 'text/html');
    $request->authorization_basic('username', 'password');

    my $response = $agent->request($request);

    if (!$response->is_success()) {
      exit;
    }
  (@response) = split '\n' , $response->content();

foreach $line (@response){
      
        #print  "$mon/$day/$year,$stime[0]:$stime[1],$etime[0]:$etime[1],\n$line\n";
  if ($line !~ /^HOST_NAME/){

      (@info) = split ',' , $line;
      @info[0] = map {uc} @info[0];
      @info[0] =~ s/"//g;
       push @{$sys_avail{$info[0]}}, ($info[7],$info[8],$info[16],$info[17]);  ##host_name total_time_up percent_total_time_up total_time down percent_total_time_down##
       }
}


foreach $key (sort keys %sys_avail){
             
               write;}

format ReportFormat_TOP =

===========================================================
                       System Availability 
                              for
                          @</@</@<<<
                     $mon, $day, $year
                         @<:@< - @<:@<
            $stime[0], $stime[1], $etime[0], $etime[1]
                             page@|||
                                     $%
===========================================================
HostName           Percent of Uptime     Percent Down Time
___________________________________________________________
.
format ReportFormat = 

@<<<<<<<<<<<<<<<<<  @<<<<<<<                 @<<<<<<<<
$key, "@{$sys_avail{$key}}[1]","@{$sys_avail{$key}}[3]"
.



