head	1.10;
access;
symbols;
locks
	sh1517:1.10; strict;
comment	@# @;


1.10
date	2006.01.15.23.33.46;	author sh1517;	state Exp;
branches;
next	1.9;

1.9
date	2006.01.15.23.21.21;	author sh1517;	state Exp;
branches;
next	1.8;

1.8
date	2006.01.15.08.02.54;	author sh1517;	state Exp;
branches;
next	1.7;

1.7
date	2006.01.04.04.28.22;	author sh1517;	state Exp;
branches;
next	1.6;

1.6
date	2006.01.04.02.55.51;	author sh1517;	state Exp;
branches;
next	1.5;

1.5
date	2005.12.18.05.54.46;	author sh1517;	state Exp;
branches;
next	1.4;

1.4
date	2005.12.01.03.22.51;	author sh1517;	state Exp;
branches;
next	1.3;

1.3
date	2005.12.01.03.10.15;	author sh1517;	state Exp;
branches;
next	1.2;

1.2
date	2005.11.30.23.21.28;	author sh1517;	state Exp;
branches;
next	1.1;

1.1
date	2005.11.30.03.52.50;	author sh1517;	state Exp;
branches;
next	;


desc
@1 Report with outages only (links are simply too long for a text report).
@


1.10
log
@1 Emphasise that 'time date' timeperiods (eg '07:00 01.01.2006') must be quoted.
@
text
@#!/usr/bin/perl

# $Id: host_down_report,v 1.9 2006-01-16 10:21:21+11 sh1517 Exp sh1517 $

# $Log: host_down_report,v $
# 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 nms sh1517>,
				[ 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) ;
@


1.9
log
@1 Change usage message.
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.8 2006-01-15 19:02:54+11 sh1517 Exp sh1517 $
d6 3
d60 1
a60 1
host_regex option.
d67 1
a67 1
  $0 -t 07:00 01.12.2005
@


1.8
log
@1 Allow -h option without -t. The 'timperiod' options defaults to 'thismonth'
  if only a host regex is specified.
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.7 2006-01-04 15:28:22+11 sh1517 Exp sh1517 $
d6 4
d33 1
a33 1
$0 -t <timeperiod> -h <host_regex>
a36 5
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.

d54 9
d68 1
@


1.7
log
@1 Add support for last<mins>mins timeperiod (requires 1.65 of Report 0.015).
2 Correct usage information.
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.6 2006-01-04 13:55:51+11 sh1517 Exp sh1517 $
d6 4
d23 1
a23 1
use vars qw($opt_t) ;
d25 1
a25 1
getopt 't' ;
d29 3
a31 1
$0 -t <timeperiod>
d33 4
a36 3
Displays host outages in the interval defined by the timeperiod. When the timeperiod has 
a start date or time, or is not one of 'yesterday', 'lastweek', 'lastmonth' or 'lastyear', 
the interval ends now.
d59 1
d64 1
a64 1
  unless $opt_t ;
d67 2
a68 1
  unless $opt_t =~ m#^(?:
d77 15
a91 1

d98 2
a99 1
				sub { my %F = @@_; $F{TOTAL_TIME_DOWN} > 300 },
@


1.6
log
@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.
@
text
@d3 1
a3 1
# $Id$
d5 8
a12 1
# $Log$
d27 10
a36 1
Displays those hosts with less than 100% total time up during the timeperiod.
d42 2
a43 1
               last12hours | last24hours | last7days | last31days |
d48 4
a51 1
eg $0 -t 07:00 01.12.2005
d63 1
a63 1
			last (?:week|month|year|24hours|12hours|7days|31days|\d+days?|\d+hours?) 	|
d97 7
@


1.5
log
@1 Add a pre-filter selector callback (to filter the CSV records before processing
  by mkreport()).
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.4 2005-12-01 14:22:51+11 sh1517 Exp sh1517 $
d5 1
a5 13
# $Log: host_down_report,v $
# Revision 1.4  2005-12-01 14:22:51+11  sh1517
# 1 Fix option processing (to allow the new time periods: allow 1 digit day and months).
#
# Revision 1.3  2005-12-01 14:10:15+11  sh1517
# 1 Update usage to show new timeperiods (HHMM etc).
#
# Revision 1.2  2005-12-01 10:21:28+11  sh1517
# 1 Allow new options (eg -t 0700 | 07:00 | dd.mm.yyyy etc).
#
# Revision 1.1  2005-11-30 14:52:50+11  sh1517
# Initial revision
#
d27 1
a27 1
	       HHMM      | HH::MM      |
d50 7
a56 1
my $x = Nagios::Report->new(q<local_cgi nms sh1517>, [ qw(24x7) ], $opt_t, 0, sub { my %F = @@_; my $u = $F{PERCENT_TOTAL_TIME_UP}; $u =~ s/%//; $u < 100 })
d66 1
a66 1
	       ],
d68 2
a69 2
		sub { my %F = @@_; my $u = $F{PERCENT_TOTAL_TIME_UP}; $u =~ s/%//; $u < 100 },
		# sub { 1 },
d71 5
a75 1
		&comp(alpha => 0, ascend => 0, fields => [qw(TOTAL_TIME_DOWN TOTAL_TIME_UNREACHABLE)]),
d77 1
a77 8
		sub {	$_ = shift @@_; my %F = @@_;
			my $d = $F{TOTAL_TIME_DOWN} ;
			my $u = $F{TOTAL_TIME_UNREACHABLE} ;
			push @@$_, 
				&t2hms($d),
				&t2hms($u) ;
			qw(TOTAL_TIME_DOWN_HMS TOTAL_TIME_UNREACHABLE_HMS)
		},
a84 1
							# 240 chars is needed for the URLs.
@


1.4
log
@1 Fix option processing (to allow the new time periods: allow 1 digit day and months).
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.3 2005-12-01 14:10:15+11 sh1517 Exp sh1517 $
d6 3
d62 1
a62 1
my $x = Nagios::Report->new(q<local_cgi nms sh1517>, [ qw(24x7) ], $opt_t)
@


1.3
log
@1 Update usage to show new timeperiods (HHMM etc).
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.2 2005-12-01 10:21:28+11 sh1517 Exp sh1517 $
d6 3
d53 1
a53 1
			(?: \d\d :? \d\d  \s+  \d\d [\./] \d\d [\./] \d\d)				|
d55 1
a55 1
			(?: \d\d [\./] \d\d [\./] \d\d)
@


1.2
log
@1 Allow new options (eg -t 0700 | 07:00 | dd.mm.yyyy etc).
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.1 2005-11-30 14:52:50+11 sh1517 Exp sh1517 $
d6 3
d28 10
a37 2
timeperiod ::= today | yesterday | thisweek | lastweek | thismonth | lastmonth | thisyear | lastyear
               last12hours | last24hours | last7days | last31days
@


1.1
log
@Initial revision
@
text
@d3 1
a3 1
# $Id: host_down_report,v 1.1 2005-11-24 19:06:44+11 sh1517 Exp sh1517 $
d6 1
a6 1
# Revision 1.1  2005-11-24 19:06:44+11  sh1517
d34 10
a43 1
  unless $opt_t =~ /^(?:today|yesterday|this(?:week|month|year)|last(?:week|month|year|24hours|12hours|7days|31days))/ ;
a50 1
			PERCENT_TOTAL_TIME_UP
d76 1
a76 1
$x->debug_dump(30, 5) ;
@
