#!/usr/bin/perl
#
# $Id: lrrd-html.in,v 1.10 2003/12/02 10:15:14 jimmyo Exp $
#
# $Log: lrrd-html.in,v $
# Revision 1.10  2003/12/02 10:15:14  jimmyo
# Minor bugfix reported by Chan Wilson
#
# Revision 1.9  2003/12/02 09:55:56  jimmyo
# Fixed stupid brain-bug (Chan Wilson)
#
# Revision 1.8  2003/11/07 20:46:12  jimmyo
# Only require Config::General if using old config format.
#
# Revision 1.7  2003/11/07 19:00:16  jimmyo
# Put lockfiles in the right place
#
# Revision 1.6  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
$|=1;

use strict;
use HTML::Template;
use Getopt::Long;
use LRRD;
use POSIX qw(strftime);

my @times = ( "day", "week", "month", "year" );


my $DEBUG=0;
my $VERSION="0.9.9r5";
my $conffile = "/etc/lrrd/server.conf";
my $force_root = 0;
my $do_usage = 0;
my $do_version = 0;

# Get options
$do_usage=1  unless 
GetOptions ( "host=s"       => \(),
             "force-root!"  => \$force_root,
	     "service=s"    => \(),
	     "config=s"     => \$conffile,
	     "debug!"       => \$DEBUG,
	     "help"         => \$do_usage, 
	     "version!"     => \$do_version );

if ($do_usage)
{
    print "Usage: $0 [options]

Options:
    --[no]force-root    Force running, even as root. [--noforce-root]
    --help		View this message.
    --debug		View debug messages.
    --version		View version information.
    --service <service>	Compatability. No effect.
    --host <host>	Compatability. No effect.
    --config <file>	Use <file> as configuration file. 
			[/etc/lrrd/server.conf]

";
    exit 0;
}

if ($do_version)
{
    print "lrrd-html (lrrd-server) version $VERSION.\n";
    print "Written by Knut Haugen, Audun Ytterdal, Jimmy Olsen, Tore Anderson / Linpro AS\n";
    print "\n";
    print "Copyright (C) 2002-2003\n";
    print "This is free software released under the GNU Public License. There is NO\n";
    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
    exit 0;
}

if ($> == 0 and !$force_root)
{
    print "You are running this program as root, which is neither smart nor necessary.
If you really want to run it as root, use the --force-root option. Else, run
it as the user \"lrrd\". Aborting.\n\n";
    exit (1);
}

my $config = &lrrd_config ($conffile);

open LOG,">>$config->{logdir}/lrrd-html.log" or die "Cannot open $config->{logdir}/lrrd-html.log";
logger("Starting lrrd-html, checking lock");

lrrd_runlock("$config->{rundir}/lrrd-html.lock");
my $template = HTML::Template->new(filename => "$config->{tmpldir}/lrrd-overview.tmpl",
				   die_on_bad_params => 0,
				   loop_context_vars => 1);

my $domaintemplate = HTML::Template->new(filename => "$config->{tmpldir}/lrrd-domainview.tmpl",
				   die_on_bad_params => 0,
				   loop_context_vars => 1);

my $nodetemplate = HTML::Template->new(filename => "$config->{tmpldir}/lrrd-nodeview.tmpl",
				       die_on_bad_params => 0,
				       loop_context_vars => 1);

my $servicetemplate = HTML::Template->new(filename => "$config->{tmpldir}/lrrd-serviceview.tmpl",
				       die_on_bad_params => 0,
				       loop_context_vars => 1);

my @domains;

my @domainorder;
if ($config->{domain}->{domain_order}) {
    @domainorder = split / /, $config->{domain}->{domain_order};
} else {
    @domainorder = sort (keys %{$config->{domain}});
}

#Make sure the logo and the stylesheet file is in the html dir
my @files = ("style.css", "logo.gif");
foreach my $file( (@files) ) {
    if ((! -e "$config->{htmldir}/$file") or
	 (-e "$config->{tmpldir}/$file") and 
	 ((stat ("$config->{tmpldir}/$file"))[9] > (stat("$config->{htmldir}/$file"))[9])) {
        unless (system("cp", "$config->{tmpldir}/$file", "$config->{htmldir}/")){
            logger("copied $file into htmldir");
        } else {
            logger("could not copy $file into htmldir");
        }
    }
}

#make domain list
my @domainlist = map { { DOMAIN => $_ } } sort keys %{$config->{domain}};
my $timestamp = strftime("%Y-%m-%d T %T", localtime);
for my $domain (sort (keys %{$config->{domain}})) {
    logger("processing domain: $domain");
    my %domain;
    $domain{domain}=$domain;
    my @nodes;
    my @nodeorder;
    if ($config->{domain}->{$domain}->{node_order}) {
	@nodeorder = split / /, $config->{domain}->{$domain}->{node_order};
    } else {
	@nodeorder = sort (keys %{$config->{domain}->{$domain}->{node}});
    }
    for my $node (@nodeorder) {
        logger("processing node: $node");
	my %node;
	$node{node}=$node;
	$node{url}="$domain/$node.html";
	my @services;
	my @serviceorder;
	if ($config->{domain}->{$domain}->{node}->{$node}->{service_order}) {
	    @serviceorder = split / /, $config->{domain}->{$domain}->{node}->{$node}->{service_order};
	} else {
	    @serviceorder = sort (keys %{$config->{domain}->{$domain}->{node}->{$node}->{client}});
	}
	
	for my $service (@serviceorder) {
	    logger("processing service: $service");
	    next unless defined( $config->{domain}->{$domain}->{node}->{$node}->{client}->{$service} )
	                    &&  $config->{domain}->{$domain}->{node}->{$node}->{client}->{$service} ne "";
	    next unless (lrrd_get_bool ($config, "graph", 1, $domain, $node, $service));
	    my @service;
	    my %service;
	    $service{service}=$service;
	    $service{label}=$config->{domain}->{$domain}->{node}->{$node}->{client}->{$service}->{graph_title};
	    $service{imgday}="$node-$service-day.png";
	    $service{imgweek}="$node-$service-week.png";
	    $service{imgmonth}="$node-$service-month.png";
	    $service{imgyear}="$node-$service-year.png";
	    $service{url}="$node-$service.html";
	    $service{domain}="$domain";
	    $service{node}=$node;
	    push @services, \%service;
	    push @service, \%service;
	    $servicetemplate->param(SERVICES => \@service,
				    SERVICE => $service,
				    NODE => $node,
				    DOMAIN => $domain, 
                                    DOMAINS => \@domainlist, 
                                    TIMESTAMP => $timestamp);
	    open (FILE, ">$config->{htmldir}/$domain/$node-$service.html") or die "Cannot open $config->{htmldir}/$domain/$node-$service.html";
	    print FILE $servicetemplate->output;
	    close FILE;
	}
	$nodetemplate->param(SERVICES => \@services,
			     NODE => $node,
			     DOMAIN => $domain,
                             DOMAINS => \@domainlist, 
                             TIMESTAMP => $timestamp);
	open (FILE, ">$config->{htmldir}/$domain/$node.html") or die "Cannot open $config->{htmldir}/$domain/$node.html";
 	print FILE $nodetemplate->output;
	close FILE;
	$node{services} = \@services;
	$node{domain} = $domain;
	push @nodes,\%node;

    }
    $domaintemplate->param(NODES => \@nodes,
                           DOMAIN => $domain, 
                           DOMAINS => \@domainlist, 
                           TIMESTAMP => $timestamp);
    open (FILE, ">$config->{htmldir}/$domain/index.html") or die "Cannot open $config->{htmldir}/index.html";
    print FILE $domaintemplate->output;
    close FILE;
    
    $domain{nodes} = \@nodes;
    $domain{domain} = $domain;
    push @domains,\%domain;
}

$template->param(DOMAINS => \@domains, 
                 TIMESTAMP => $timestamp);
open (FILE, ">$config->{htmldir}/index.html") or die "Cannot open $config->{htmldir}/index.html";
print FILE $template->output;
close FILE;

sub logger {
  my ($comment) = @_;
  my $now = strftime "%b %d %H:%M:%S", localtime;
  print LOG "$now - $comment\n";
}

logger("lrrd-html finished");
close LOG;

=head1 NAME

lrrd-html - A program to draw html-pages on an lrrd-server

=head1 SYNOPSIS

lrrd-html [options]

=head1 OPTIONS

=over 5

=item B<< --[no]force-root >>

Force running as root (stupid and unnecessary). [--noforce-root]

=item B<< --service <service> >>

Limit services to those of E<lt>serviceE<gt>. Multiple --service options may be supplied. [unset]

=item B<< --host <host> >>

Limit hosts to those of E<lt>host<gt>. Multiple --host options may be supplied. [unset]

=item B<< --config <file> >>

Use E<lt>fileE<gt> as configuration file. [/etc/lrrd/server.conf]

=item B<< --help >>

View help message.

=item B<< --[no]debug >>

If set, view debug messages. [--nodebug]

==back

=head1 DESCRIPTION

LRRD-html is a part of the package LRRD-server, which is used in combination with LRRD-client.
LRRD-server is a group of programs to gather data from LRRD-clients, graph them, create html-pages,
and optionally warn Nagios about any off-limit values.

LRRD-html creates the html pages.

=head1 FILES

	/etc/lrrd/server.conf
	/var/lib/lrrd/datafile
	/var/log/lrrd/lrrd-html
	/var/www/lrrd/*
	/var/run/lrrd/*

=head1 VERSION

This is lrrd-html version 0.9.9r5

=head1 AUTHORS

Knut Haugen, Audun Ytterdal and Jimmy Olsen.

=head1 BUGS

lrrd-html does, as of now, not check the syntax of the configuration file.

Please report other bugs in the bug tracker at L<http://lrrd.sf.net/>.

=head1 COPYRIGHT

Copyright  2002 Knut Haugen, Audun Ytterdal, and Jimmy Olsen / Linpro AS.

This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

This program is released under the GNU General Public License

=cut

