#!/usr/bin/perl

# $Id: host_down_report,v 1.10 2006-01-16 10:33:46+11 sh1517 Exp sh1517 $

# $Log: host_down_report,v $
# Revision 1.10  2006-01-16 10:33:46+11  sh1517
# 1 Emphasise that 'time date' timeperiods (eg '07:00 01.01.2006') must be quoted.
#
# Revision 1.9  2006-01-16 10:21:21+11  sh1517
# 1 Change usage message.
#
# Revision 1.8  2006-01-15 19:02:54+11  sh1517
# 1 Allow -h option without -t. The 'timperiod' options defaults to 'thismonth'
#   if only a host regex is specified.
#
# Revision 1.7  2006-01-04 15:28:22+11  sh1517
# 1 Add support for last<mins>mins timeperiod (requires 1.65 of Report 0.015).
# 2 Correct usage information.
#
# Revision 1.6  2006-01-04 13:55:51+11  sh1517
# 1 Substantive changes associated with tidying up for release.
#   1.1 Don't bother munging fields (converting down and unreach time to hh:mm:s) that don't appear
#       in the report.
#   1.2 Sort by duration of the outage (DOWN).
#   1.3 tidy up constructor call so that it is evident what parms do what.
#

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) ;
