#!/usr/bin/perl
#
# $Id: munin-html.in,v 1.5.2.3 2004/06/08 15:26:04 jimmyo Exp $
#
# $Log: munin-html.in,v $
# Revision 1.5.2.3  2004/06/08 15:26:04  jimmyo
# The server programs now open the log file at an earlier point.
#
# Revision 1.5.2.2  2004/06/08 14:55:41  jimmyo
# Noise reduction in munin-update, when plugins say strange things (only log, don\'t complain on stderr).
#
# Revision 1.5.2.1  2004/05/11 20:23:07  jimmyo
# Bug regarding logo namechange from logo.gif to logo.png, when installing.
#
# Revision 1.5  2004/01/29 17:40:10  jimmyo
# Fixed pod typos patched by Lupe Christoph (SF#884092)
#
# Revision 1.4  2004/01/29 17:34:06  jimmyo
# Updated copyright information
#
# Revision 1.3  2004/01/29 17:13:46  jimmyo
# Enabled domain_order-option.
#
# Revision 1.2  2004/01/15 15:20:01  jimmyo
# Making things workable after name change. Upping for test verwion.
#
# Revision 1.1  2004/01/02 18:50:01  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# 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 Munin;
use POSIX qw(strftime);

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


my $DEBUG=0;
my $VERSION="1.0.2";
my $conffile = "/etc/munin/munin.conf";
my $force_root = 0;
my $do_usage = 0;
my $do_version = 0;
my $log = new IO::Handle;

# 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/munin/munin.conf]

";
    exit 0;
}

if ($do_version)
{
    print "munin-html version $VERSION.\n";
    print "Written by Knut Haugen, Audun Ytterdal, Jimmy Olsen, Tore Anderson / Linpro AS\n";
    print "\n";
    print "Copyright (C) 2002-2004\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 \"munin\". Aborting.\n\n";
    exit (1);
}

my $config;
$config = &munin_config ($conffile, $config);

logger("Starting munin-html, checking lock");

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

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

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

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

my @domains;

my @domainorder;
if ($config->{domain_order}) {
    @domainorder = split / /, $config->{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.png");
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 => $_ } } @domainorder;
my $timestamp = strftime("%Y-%m-%d T %T", localtime);
for my $domain (@domainorder) {
    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 (munin_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_open {
    my $dirname = shift;

    if (!$log->opened)
    {
	unless (open ($log, ">>$dirname/munin-html.log"))
	{
	    print STDERR "Warning: Could not open log file \"$dirname/munin-html.log\" for writing: $!";
	}
    }
}


sub logger {
    my ($comment) = @_;
    my $now = strftime "%b %d %H:%M:%S", localtime;
 
    if ($log->opened)
    {
	print $log "$now - $comment\n";
    }
    else
    {
	if (defined $config->{logdir})
	{
	    if (open ($log, ">>$config->{logdir}/munin-html.log"))
	    {
		 print $log "$now - $comment\n";
	    }
	    else
	    {
		 print STDERR "Warning: Could not open log file \"$config->{logdir}/munin-html.log\" for writing: $!";
		 print STDERR "$now - $comment\n";
	    }
	}
	else
	{
	    print STDERR "$now - $comment\n";
	}
    }
}


logger("munin-html finished");
close $log;

=head1 NAME

munin-html - A program to draw html-pages on an Munin installation

=head1 SYNOPSIS

munin-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/munin/munin.conf]

=item B<< --help >>

View help message.

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

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

=back

=head1 DESCRIPTION

Munin-html is a part of the package Munin, which is used in combination
with Munin's node.  Munin is a group of programs to gather data from
Munin's nodes, graph them, create html-pages, and optionally warn Nagios
about any off-limit values.

Munin-html creates the html pages.

=head1 FILES

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

=head1 VERSION

This is munin-html version 1.0.2

=head1 AUTHORS

Knut Haugen, Audun Ytterdal and Jimmy Olsen.

=head1 BUGS

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

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

=head1 COPYRIGHT

Copyright  2002-2004 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

