#!/usr/bin/perl -w

use CGI qw/:standard/;
use strict;

=head1 NAME

WritePerfGraphs.pl - creates a HTML-page with all graphs created for L<PerfDataConsumer.pl>.

=head1 VERSION

Version 2.0

=head1 SYNOPSIS

call in a CGI-Environment.

Parameters: C<host> (optional).

=head1 DESCRIPTION

If C<host> / C<servs> is not given, all hosts and services are listet for
choosing.

If the parameters are given, a by-service sorted list is shown with all
pictures for all given hosts/service.

=head1 LIMITATIONS

The script depends I<heavily> on the naming convention of the pictures, which
is

  HOSTNAME__SERVICE__DAYS.png

Don't mess with this. It also assumes, that there are exactly 3 pictures for
every host/service combination.

=head1 CONFIGURATION

If directories change, also change them here in the source.

=head1 AUTHOR

(c) 2003 Hannes Schulz <mail@hannes-schulz.de>

=cut

my $imgdir  = "/usr/local/nagios/share/images/rrdimg";
my $hostdir = "/usr/local/nagios_indep/var/rrds";

my (@hosts,@servs);

@hosts = param('host');
@servs = param('servs');

print header, start_html(
 -title => 'Performance Stats',
 -meta  =>{'refresh' => '60','expires'=>'120'}
), h1('Performance Statistic Histograms');

if($#hosts<0 or $#servs<0 or $hosts[0] eq "all"){
	print p('Hint: For greater flexibility in date ranges, use <a href="ExtPerfGraph.pl">Extended Performance Graphs</a>!');
	print p('Hint: To create an Excel Graph using CSV data, use <a href="ExtPerfGraph.pl">Extended Performance Graphs</a>!');
	print p('Please choose one (Click) or more (Ctrl or Shift-Click) hosts/services:');
	print p('Be aware, that not every service is defined for every host!');
	show_hosts();
}
else{
	print h2('Statistics for '.join(', ',@hosts).' ('.join(', ',@servs).')');
	print p('All times/dates are local times at the monitored site.');
	show_stats(\@hosts,\@servs);
}

print end_html;

sub show_hosts{
	my $selfurl = self_url;
	my @hs    = map{m#/([^/]+)$#;$1}<$hostdir/*>;
	my %servs = map{m#__(.*)__#;$1,1}<$imgdir/*>;
	print start_form(-method=>"GET"),
		  submit,"<br>",
	      scrolling_list(-name => "host",  -size=>25, -multiple =>1, -Values => [ reverse @hs ]),
	      scrolling_list(-name => "servs", -size=>25, -multiple =>1, -Values => [ "all", sort keys %servs ], default => [ "all" ]),
          submit,end_form;
}

sub show_stats{
	my %hosts = map {$_,1} @{ shift() };
	my %servs = map {$_,1} @{ shift() };
	my ($s,$h,$t,%ts);

	my @todo = grep{
		m#/([^/]+)__([^/]+)__\d+\.\w+$#o;
		($hosts{$1} && ($servs{$2} || $servs{all}))
	}<$imgdir/*>;

	@todo = sort {($s)=($b=~m/__(.*)__/); $a=~m/__(.*)__/; $s cmp $1}@todo;
	@todo = map{m#/([^/]+)$#;$1}@todo;
	@todo = map{img{src=>"/nagios/images/rrdimg/$_"}}@todo;

	print "<table>";
	
	while($#todo>0){
		$h = shift @todo;
		($s,$t) = ( $h =~ m#/([^/]+)__(.*)__#o);
		unless($ts{$t}){ print Tr(th({align=>"left"},"&nbsp;<br>$t")); $ts{$t} = 1 }
		print Tr(td({-nowrap => "true"}, 
		  [ a({href=>"status.cgi?host=$s&ok=ok"},$h), shift @todo, shift @todo ]))
	}
	
	print "</table>";

}

