#!/usr/bin/perl


use strict ;

use Nagios::Report ;
use Getopt::Std ;

use vars qw($opt_t) ;

getopt 't' ;

my $usage = <<USAGE ;

$0 -t <timeperiod>

Displays those hosts with less than 100% total time up during the timeperiod.

timeperiod ::= today | yesterday | thisweek | lastweek | thismonth | lastmonth | thisyear | lastyear
               last12hours | last24hours | last7days | last31days

USAGE

die $usage
  unless $opt_t ;

die $usage
  unless $opt_t =~ /^(?:today|yesterday|this(?:week|month|year)|last(?:week|month|year|24hours|12hours|7days|31days))/ ;

my $x = Nagios::Report->new(q<local_cgi Nagios_Server Auth_Nagios_User>, [ qw(24x7) ], $opt_t)
  or die "Can't construct Nagios::Report object." ;

$x->mkreport(
		[ qw(
			HOST_NAME
			PERCENT_TOTAL_TIME_UP
			TOTAL_TIME_DOWN
			TOTAL_TIME_DOWN_HMS
			TOTAL_TIME_UNREACHABLE
			TOTAL_TIME_UNREACHABLE_HMS
			AVAIL_URL
			TREND_URL
		   )
	       ],

		sub { my %F = @_; my $u = $F{PERCENT_TOTAL_TIME_UP}; $u =~ s/%//; $u < 100 },
		# sub { 1 },

		&comp(alpha => 0, ascend => 0, fields => [qw(TOTAL_TIME_DOWN TOTAL_TIME_UNREACHABLE)]),

		sub {	my $F = shift @_;
			my $d = $F->{TOTAL_TIME_DOWN} ;
			my $u = $F->{TOTAL_TIME_UNREACHABLE} ;
			$F->{TOTAL_TIME_DOWN_HMS}		= t2hms($d) ;
			$F->{TOTAL_TIME_UNREACHABLE_HMS}	= t2hms($u) ;
			qw(TOTAL_TIME_DOWN_HMS TOTAL_TIME_UNREACHABLE_HMS)
		}
) ;



$x->debug_dump(240, 2) ;
							# 240 chars is needed for the URLs.
