#!/usr/bin/perl -w

use strict;
use FileHandle;

print <<"ENDOFHERE";
Content-Type: text/html

<html>
  <head>
    <title>Environment graph list</title>
  </head>
  <body>

    <h1>Environment Graphs</h1>

    <table cellpadding="2" cellspacing="2">
ENDOFHERE

my $localhost = `hostname`;
chomp $localhost;

my @hosts = ();
my $fh = new FileHandle;
my $fn = '/home/swarren/cron_scripts/monitor/conf.cgi.hosts.txt';
if (!$fh->open($fn)) {
    die "Can't open '${fn}' to read: $!";
}
while (<$fh>) {
    chomp;
    if ($_ eq 'localhost') {
        $_ = $localhost;
    }
    push(@hosts, $_);
}
$fh->close();

foreach my $host (@hosts) {
    my $host_str = $host;
    $fh = new FileHandle;
    $fn = '/home/swarren/cron_scripts/monitor/' . $host . '/conf.vars.txt';
    if (!$fh->open($fn)) {
        die "Can't open '${fn}' to read: $!";
    }
    while (<$fh>) {
        chomp;

        my $line = $_;
        $line =~ s:#.*$::;
        $line =~ s:^\s+::;
        $line =~ s:\s+$::;
        if ($line eq '') {
            next;
        }

        my @line = split('\s+', $line);
        if ($#line < 8) {
            die "Wrong field count in var line: ${line}\n";
        }

        my ($var_type, $params, $var_name, $low_set, $low_clear, $high_clear, $high_set, $graph_max, @description) = @line;
        my $description = join(' ', @description);

        print <<"ENDOFHERE";
      <tr>
        <td><tt>$host_str</tt></td>
        <td>$description</td>
        <td><tt>[<a href="sensor_graph.cgi?host=${host}&value=${var_name}&max_samples=1440">1 Day</a>]</tt></td>
        <td><tt>[<a href="sensor_graph.cgi?host=${host}&value=${var_name}&max_samples=10080">1 Week</a>]</tt></td>
        <td><tt>[<a href="sensor_graph.cgi?host=${host}&value=${var_name}&max_samples=20160">2 Weeks</a>]</tt></td>
        <td><tt>[<a href="sensor_graph.cgi?host=${host}&value=${var_name}&max_samples=40320&width=1536">4 Weeks</a>]</tt></td>
        <td><tt>[<a href="sensor_graph.cgi?host=${host}&value=${var_name}&max_samples=80640&width=3072">8 Weeks</a>]</tt></td>
      </tr>
ENDOFHERE
      $host_str = '';
    }
    $fh->close();
}

print <<"ENDOFHERE";
    </table>
  </body>
</html>
ENDOFHERE
