#!/usr/bin/perl

use strict ;

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

use vars qw($opt_t $opt_h) ;

getopt 'th' ;

my $usage = <<USAGE ;

$0 -t <timeperiod> -h '<host_regex>'

Displays host outages in the interval defined by the timeperiod.

  for each outage in the interval, sorted in ascending order of time down (ie when the outage occurred)
    displays the
      host_name
      time the host went down
      time the host came up
      the outage

timeperiod ::= today     | yesterday   |
	       thisweek  | lastweek    |
	       thismonth | lastmonth   |
	       thisyear  | lastyear    |
               last12hours    | last24hours      | last7days | last31days   |
               last<days>days | last<hours>hours | last<mins>min(ute)?(s)?  |
	       HHMM      | HH:MM       |
	       DD.MM.YY  | DD.MM.YYYY  | MM/DD/YY | MM/DD/YYYY    |
	       24hourtime date

Timeperiods other than 'yesterday', 'lastweek', 'lastmonth' or 'lastyear' end now.

The timeperiod defaults to 'thismonth' if a host or set of hosts is specified by the
host_regex option. Quote the timeperiod if it contains spaces ('time date').

The host regex should not include pattern delimiters (//); to specify case insensitive
matches use 'Extended patterns', with or with out clustering. You will need to 
quote the regex.

eg
  $0 -t '07:00 01.12.2005'
  $0 -t last7days
  $0 -t last2hours
  $0 -h '(?i:ben)'
  $0 -h '(?i)ben'

USAGE

die $usage
  unless $opt_t || $opt_h ;

die $usage
  unless defined($opt_h) ||
         $opt_t =~ m#^(?:
			today										|
			yesterday									|
			this (?:week|month|year)							|
			last (?:week|month|year|24hours|12hours|7days|31days|\d+days?|\d+hours?|\d+min(ute)?s?) 	|
			(?: \d\d :? \d\d  \s+  \d\d? [\./] \d\d? [\./] \d\d (?:\d\d)?)			|
			(?: \d\d :? \d\d)								|
			(?: \d\d? [\./] \d\d? [\./] \d\d (?:\d\d)?)
		     )$#x ;
my $host_re ;
if ($opt_h ) {
  eval { $host_re = qr<$opt_h> } ;
  die <<BADRE

$0 Host regex '$opt_h' failed to compile: '$@'
BADRE
    if $@ ;
}

$opt_t ||= 'thismonth' ;

my $pre_filter = $opt_h
	? sub { my %F = @_; my ($d, $h) =($F{TOTAL_TIME_DOWN}, $F{HOST_NAME}); $d > 300 && ($h =~ qr<$opt_h>) }
	: sub { my %F = @_; $F{TOTAL_TIME_DOWN} > 300 } ;

my $x = Nagios::Report->new(
				q<local_cgi NagServer AuthUser>,
				[ qw(24x7) ],
				$opt_t,
				0,
				$pre_filter,
				# sub { my %F = @_; $F{TOTAL_TIME_DOWN} > 300 },
			   )	
  or die "Can't construct Nagios::Report object." ;

$x->mkreport(
		[ qw(
			HOST_NAME
			DOWN
			UP
			OUTAGE
		   )
		],

		sub { my %F = @_; i2t($F{OUTAGE}) > 300 },
		# sub { my %F = @_; my $u = $F{PERCENT_TOTAL_TIME_UP}; $u =~ s/%//; $u < 100 },

		sub { 
				my %f = @_ ;
				d2t($Nagios::Report::a->[$f{DOWN}])     <=>
				d2t($Nagios::Report::b->[$f{DOWN}]) ;
		},

				# Sort by outage duration.
# 		sub { 
# 				my %f = @_ ;
# 				i2t($Nagios::Report::a->[$f{OUTAGE}])     <=>
# 				i2t($Nagios::Report::b->[$f{OUTAGE}]) ;
# 		},

		undef,

		1,
) ;



$x->debug_dump(30, 4) ;
