Project

General

Profile

Download (16.4 KB) Statistics
| Branch: | Revision:
5b3eb8e6 Hamish Coleman
#!/usr/bin/perl
11780c4c Hamish Coleman
#
# Flexible MRTG index page
#
# Based on original work done by mick@lowdown.com, mostly rewritten
# by Hamish@zot.org
#
5b3eb8e6 Hamish Coleman
# (c) 1997, Mick Ghazey mick@lowdown.com
# Thanks to Dave Rand, Peter W. Osel and Tobias Oetiker.
11780c4c Hamish Coleman
#
# Requires the CGI package (in mandriva this is perl-CGI)

87ca9250 Hamish Coleman
our $VERSION=0.5;
4ee5ba7c Hamish Coleman
11780c4c Hamish Coleman
use strict;
use warnings;
5b3eb8e6 Hamish Coleman
f7fe9beb Hamish Coleman
use FileHandle;

use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;

5b3eb8e6 Hamish Coleman
use CGI ':all';
use CGI::Carp qw(fatalsToBrowser);
use Sys::Hostname;

ddad10fd Hamish Coleman
use HTML::Entities();

f1213034 Hamish Coleman
# List of config files to search for
11780c4c Hamish Coleman
my @config_files = (
'/etc/mrtg.cfg',
'/usr/share/WWW/mrtg/mrtg.cfg',
);

1b114acb Hamish Coleman
my @munin_datafiles = (
'/var/lib/munin/datafile',
);

11780c4c Hamish Coleman
# FIXME - globals
my $gifdone = 0; # Scan for newest graph and save info for later
my $warnings;
f7fe9beb Hamish Coleman
my $db = {}; # storage for all tests found

$db->{default}->{post}->{xsize} = 500;
$db->{default}->{post}->{ysize} = 135;

sub config_save_val($$$$) {
my ($filename,$target,$var,$val) = @_;

my $this_ysize;
my $this_xsize;
my $this_title;

# HACK!
3aae3ad6 Hamish Coleman
if ( $val && $var eq 'ysize' ) {
f7fe9beb Hamish Coleman
$val += 35;
3aae3ad6 Hamish Coleman
} elsif ( $val && $var eq 'xsize' ) {
f7fe9beb Hamish Coleman
$val += 100;
}

# Save any default initializers
if ($target eq '_') {
$db->{default}->{post}->{$var} = $val;
return;
}
if ($target eq '^') {
$db->{default}->{pre}->{$var} = $val;
return;
}

b9a569f1 Hamish Coleman
# Check for target name collision, and uniqueify
f7fe9beb Hamish Coleman
my $unique_target = $target;
my $unique_id = 1;
while (defined $db->{target}->{$unique_target}
&& $db->{target}->{$unique_target}->{_cfgfile} ne $filename) {
$unique_target = $target . '_' . $unique_id;
$unique_id++;
}

if (!defined $db->{target}->{$unique_target}) {
# initialize a new target

#TODO - instantiate new tests from all of the "_"/"^" defaults

push @{$db->{sequence}}, $unique_target;

$db->{target}->{$unique_target}->{_target} = $target;
$db->{target}->{$unique_target}->{_cfgfile} = $filename;
$db->{target}->{$unique_target}->{_sequence} = @{$db->{sequence}};

$db->{target}->{$unique_target}->{xsize} = $db->{default}->{post}->{xsize};
$db->{target}->{$unique_target}->{ysize} = $db->{default}->{post}->{ysize};

}
$db->{target}->{$unique_target}->{$var} = $val;
}

# Load one or more files
#
sub config_load_file(@) {
while (@_) {
my $filename = shift;

my $fh = new FileHandle $filename,"r";
if (!defined $fh) {
# FIXME globals
$warnings .= "Warning: Could not open $filename $!\n";
804e829a Hamish Coleman
return;
f7fe9beb Hamish Coleman
}

#TODO - multi-line values

while(<$fh>){

# Is this a standard MRTG setting?
if ( $_ =~ /^([^#[][^[]+)\[(.*)\]:\s*(.+)$/ ) {
my $var=lc $1;
my $target=lc $2;
my $val=$3;

config_save_val($filename,$target,$var,$val);

}
# TODO - Handle extra statements:
# Include
# WorkDir
# LogDir
# ##HC Magic for baseurl
}
}
}

1b114acb Hamish Coleman
sub config_load_munin(@) {
while (@_) {
my $filename = shift;

my $fh = new FileHandle $filename,"r";
if (!defined $fh) {
# FIXME globals
$warnings .= "Warning: Could not open $filename $!\n";
return;
}

my $d={};

while(<$fh>){
if ( m/^version/) {
next;
}

08f54d40 Hamish Coleman
# suck them all in prior to sorting
if ( m/^(.*).graph_(title|category) (.*)/) {
$d->{$1}->{$2}=$3;
1b114acb Hamish Coleman
}
08f54d40 Hamish Coleman
}
1b114acb Hamish Coleman
f62e9a91 Hamish Coleman
# sort by title, then by internal name
# this keeps comparable graphs together
my @sorted = sort {
($d->{$a}{title}||'NONE') cmp ($d->{$b}{title}||'NONE') ||
$a cmp $b
} keys %{$d};
for my $i (@sorted) {
my $title = $d->{$i}{title} || 'NONE';
my $category = lc $d->{$i}{category} || 'NOCATEGORY';
08f54d40 Hamish Coleman
$i =~ m/^([^;]+);([^:]+):(([^.]+)\.?([^.]+)?)/;
#print "group=$1\n";
#print "host=$2\n";
#print "test=$6\n";
#print "instance=$3\n";
#print "title=$title\n";

my $target;
$target = "$1,$2,$category,$3";
config_save_val($filename,$target,'_grapher','munin');
config_save_val($filename,$target,'xsize',497-100);
config_save_val($filename,$target,'ysize',undef);
config_save_val($filename,$target,'title',$title);

my $path_local='/var/cache/munin/www/';
my $path_www='/munin/';

my $path_this="$1/$2/$4";
if (defined $5) {
$path_this.="/$5";
1b114acb Hamish Coleman
}
08f54d40 Hamish Coleman
config_save_val($filename,$target,'_imagefile',$path_local.$path_this);
config_save_val($filename,$target,'_url',$path_www.$path_this);
1b114acb Hamish Coleman
}
}
}

f7fe9beb Hamish Coleman
# Itterate through the loaded targets and read their mtime and
# any other filesystem details for them.
#
sub config_read_filesystem() {
# check and update details for all known tests;
for my $i (keys %{$db->{target}}) {
my $target = $db->{target}->{$i};
my $testname = $target->{_target};

# TODO - check workdir and validate it here

a88cab50 Hamish Coleman
my $filename;
# use precalculated filename if available
if (defined $target->{_imagefile}) {
$filename=$target->{_imagefile};
} else {
$filename=$testname;
}

f7fe9beb Hamish Coleman
# Default is GIF, unless a PNG file exists
$target->{_imagetype} = 'gif';
a88cab50 Hamish Coleman
if ( -e "$filename-day.png" ) {
f7fe9beb Hamish Coleman
$target->{_imagetype} = "png";
}

# TODO - change the filename based on the displayed period
a88cab50 Hamish Coleman
$filename = $filename . "-day." . $target->{_imagetype};
f7fe9beb Hamish Coleman
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($filename);
if (!defined $mtime) {
# if we cannot stat the file, skip it
next;
}

$target->{_mtime} = $mtime;

# FIXME - global
# find the newest file
if ($mtime > $gifdone) {
$gifdone = $mtime;
}
}
}

af3a7f97 Hamish Coleman
sub extract_grouphosttest($) {
my ($name) = @_;
f7fe9beb Hamish Coleman
af3a7f97 Hamish Coleman
# Do not classify a test with no separators in it
f7fe9beb Hamish Coleman
if ($name !~ /,/) {
af3a7f97 Hamish Coleman
return undef;
f7fe9beb Hamish Coleman
}

my ($group,$host,$test) = split ',', $name;
# TODO cope with missing group/host/test parts.
if (!defined $group || !defined $host || !defined $test) {
af3a7f97 Hamish Coleman
return undef;
}
return ($group,$host,$test);
}

# Read through the list of targets and find common hostname prefixes,
# so that we can group these hosts together
# (a hosttype entry)
#
sub maketree_find_hosttypes() {

for my $name (keys %{$db->{target}}) {
my ($group,$host,$test) = extract_grouphosttest($name);
if (!defined($group)) {
next;
}
my $hosttype;
# TODO - this needs to have a better way...
# hosttype is the non digit hostname prefix - thus
# "proxy-1" and "proxy-2" would both end up in the
# "proxy-" hosttype
if ($host =~ m/^(.*[^0-9])(\d+)$/) {
$hosttype = $1.'*';
} else {
next;
}

$db->{target}->{$name}->{_hosttype} = $hosttype;
b5758d6e Hamish Coleman
$db->{hosttype}->{$hosttype}->{$host}++;
af3a7f97 Hamish Coleman
}

b5758d6e Hamish Coleman
for my $hosttype (keys %{$db->{hosttype}}) {
if ((keys %{$db->{hosttype}->{$hosttype}})<2) {
delete $db->{hosttype}->{$hosttype};
}
}
af3a7f97 Hamish Coleman
}

sub maketree_grouphosttest($$) {
my ($name,$sequence) = @_;

my ($group,$host,$test) = extract_grouphosttest($name);
if (!defined($group)) {
# TODO cope with missing group/host/test parts.
# for now, just assume it is not a group/host/test
f7fe9beb Hamish Coleman
$db->{tree}->{other}->{$name} = $sequence;
return;
}

b5758d6e Hamish Coleman
# a valid hosttype both:
# defined($db->{target}->{$name}->{hosttype}) &&
# defined($db->{hosttype}->{$hosttype})
my $hosttype = $db->{target}->{$name}->{_hosttype};
804e829a Hamish Coleman
if (defined($hosttype) && !defined($db->{hosttype}->{$hosttype})) {
b5758d6e Hamish Coleman
$hosttype=undef;
f7fe9beb Hamish Coleman
}

353908b6 Hamish Coleman
# TODO - determine a better description for this tree leaf
af3a7f97 Hamish Coleman
my $testtreename = 'Graph Type';
353908b6 Hamish Coleman
f7fe9beb Hamish Coleman
if (defined $hosttype) {
353908b6 Hamish Coleman
$db->{tree}->{group}->{$group}->{$hosttype}->{$host}->{$test}->{$name} = $sequence;
$db->{tree}->{group}->{$group}->{$hosttype}->{$testtreename}->{$test}->{$name} = $sequence;
$db->{tree}->{group}->{ALL}->{$hosttype}->{$host}->{$test}->{$name} = $sequence;
$db->{tree}->{group}->{ALL}->{$testtreename}->{$test}->{$hosttype}->{$host}->{$name} = $sequence;
$db->{tree}->{group}->{$group}->{$testtreename}->{$test}->{$hosttype}->{$host}->{$name} = $sequence;
f7fe9beb Hamish Coleman
} else {
353908b6 Hamish Coleman
$db->{tree}->{group}->{$group}->{$host}->{$test}->{$name} = $sequence;
$db->{tree}->{group}->{ALL}->{$host}->{$test}->{$name} = $sequence;
$db->{tree}->{group}->{ALL}->{$testtreename}->{$test}->{$host}->{$name} = $sequence;
$db->{tree}->{group}->{$group}->{$testtreename}->{$test}->{$host}->{$name} = $sequence;
f7fe9beb Hamish Coleman
}
}

256ae084 Hamish Coleman
sub maketree_cfgfile($$) {
my ($name,$sequence) = @_;

# config files
my $cfgfile = $db->{target}->{$name}->{_cfgfile};

my @cfgpath = split /\//,$cfgfile;

shift @cfgpath; # remove initial slash

unshift @cfgpath,"config"; # ensure that the config node exists
my $node = $db->{tree};

while (@cfgpath ) {
if (!defined $node->{$cfgpath[0]}) {
$node->{$cfgpath[0]} = {};
}
$node = $node->{$cfgpath[0]};
shift @cfgpath;
}

# assign a target name and sequence to the final node
$node->{$name} = $sequence;
}

f7fe9beb Hamish Coleman
# Look through the target database and create tree entries for each cfgfile
#
sub maketree() {
af3a7f97 Hamish Coleman
maketree_find_hosttypes();

f7fe9beb Hamish Coleman
for my $name (keys %{$db->{target}}) {
my $sequence = $db->{target}->{$name}->{_sequence};
256ae084 Hamish Coleman
maketree_cfgfile($name,$sequence);
maketree_grouphosttest($name,$sequence);
# TODO - parse the Target setting and create trees
# TODO - parse magic comments and create trees here
f7fe9beb Hamish Coleman
}
87ca9250 Hamish Coleman
# TODO - look for nodes that have only one child and that child is
# childless and prune the child
f7fe9beb Hamish Coleman
}

# FIXME - global
my $node_next = 1;
256ae084 Hamish Coleman
sub emit_tree_one($$$);
f7fe9beb Hamish Coleman
sub emit_tree_one($$$) {
my ($parentnode,$path,$dir) = @_;

af3a7f97 Hamish Coleman
my $debug;
if (defined(param('debug'))) {
$debug = 'debug=1&';
} else {
$debug = '';
}

f7fe9beb Hamish Coleman
if (!ref($dir)) {
#print "FIXME !REF $dir\n";
print "insDoc($parentnode, gLnk(\"S\", \"FIXME !REF $dir\", \"?\"))\n";
}

for my $i (sort keys %{$dir}) {
my $val = $dir->{$i};
if (!ref($val)) {
1b1c276c Hamish Coleman
#print "insDoc($parentnode, gLnk(\"S\", \"$i\", \"?path=$path:$i\"))\n";
f7fe9beb Hamish Coleman
next;
}

my $node = 'n'.$node_next++;
af3a7f97 Hamish Coleman
print "$node = insFld($parentnode, gFld(\"$i\", \"?".$debug."path=$path/$i\"))\n";
256ae084 Hamish Coleman
emit_tree_one("$node","$path/$i",$val);
f7fe9beb Hamish Coleman
}
}

sub emit_tree($) {
my ($style) = @_;

my $page = 'list';
my $target = 'R';

print "\n";
if ($style ne 'frame') {
print "USEFRAMES = 0\n";
print "PRESERVESTATE =1\n";
# USEICONS = 0
# WRAPTEXT = 1
$page=$style;
$target='S';
}
print "USETEXTLINKS = 1\n";
print "STARTALLOPEN = 0\n";
print "HIGHLIGHT = 1\n";
print "ICONPATH = ''\n";

print 'foldersTree = gFld("<i>MRTG Tree</i>", "javascript:undefined")',"\n";
print 'foldersTree.treeID = "Frameless"',"\n";

emit_tree_one('foldersTree',"",$db->{tree});
}

sub emit_path_contents($$) {
my ($path,$graph) = @_;
256ae084 Hamish Coleman
my @path = split /\//,$path;
f7fe9beb Hamish Coleman
my @pathfound;

my $node = $db->{tree};
while (@path) {
if (!$path[0]) {
shift @path;
next;
}
if (!ref($node)) {
print "Path Error ($path[0])\n";
return;
}
if (!defined $node->{$path[0]}) {
print "Path Not Found ($path[0])\n";
return;
}
push @pathfound, $path[0];
$node = $node->{$path[0]};
shift @path;
}

my @node;
my %targets;

push @node,$node;
while (@node) {
if (!ref($node[0])) {
print "Descent Error ($node[0])\n";
return;
}
for my $i (keys %{$node[0]}) {
my $val = $node[0]->{$i};
if (!ref($val)) {
1b1c276c Hamish Coleman
$targets{$i}=$val; # set sequence number
f7fe9beb Hamish Coleman
} else {
push @node,$val;
}
}
shift @node;
}

# TODO - sort by sequence numbers
1b1c276c Hamish Coleman
for my $i (sort {$targets{$a} <=> $targets{$b}} keys %targets) {
f7fe9beb Hamish Coleman
my $target = $db->{target}->{$i};

# FIXME - what if mtime is undef?
my $time = localtime $target->{_mtime}; # $st_mtime saved in above loop
($time) = $time =~ /(\d+:\d+:\d+)/; # Just the time

my $title = $target->{title} || 'UNTITLED';
print "$time <b>$title</b><br/>";

my ($ysize, $xsize);
3aae3ad6 Hamish Coleman
$ysize = $target->{ysize} ||"";
f7fe9beb Hamish Coleman
$xsize = $target->{xsize};

a88cab50 Hamish Coleman
my $target_urlbase;
# use a precalculated url if defined
if (defined $target->{_url}) {
$target_urlbase=$target->{_url};
} else {
$target_urlbase=$target->{_target};
}

87ca9250 Hamish Coleman
my $target_url;
if ($graph eq 'test') {
a88cab50 Hamish Coleman
# FIXME - does not work with precalculated URL
87ca9250 Hamish Coleman
$target_url = 'gnuplot1.cgi?'.
a88cab50 Hamish Coleman
'log='.$target_urlbase.'.log';
87ca9250 Hamish Coleman
if (defined $target->{ylegend}) {
$target_url .= '&ylabel='.$target->{ylegend};
}
if (defined $target->{legendi}) {
$target_url .= '&li='.$target->{legendi};
}
if (defined $target->{legendo}) {
$target_url .= '&lo='.$target->{legendo};
}
} else {
a88cab50 Hamish Coleman
$target_url = $target_urlbase."-$graph.".$target->{_imagetype};
87ca9250 Hamish Coleman
}

f7fe9beb Hamish Coleman
# TODO - use workpath and other info to point to other directories
a88cab50 Hamish Coleman
print a({-href=>$target_urlbase.".html"},
f7fe9beb Hamish Coleman
img{
87ca9250 Hamish Coleman
-src=>$target_url,
f7fe9beb Hamish Coleman
-height=>"$ysize",
-width=>"$xsize"
}
);
print br,br,"\n";
}
}
11780c4c Hamish Coleman
804e829a Hamish Coleman
sub emit_graph_period_chooser($$) {
my ($path,$this_graph_period) = @_;

my $selfurl = "?path=$path";
if (defined(param('debug'))) {
$selfurl .= '&debug=1';
}

if ($this_graph_period eq 'day') {
print 'Daily';
} else {
print a({-href=>$selfurl.'&graph=day',-target=>'_self'}, 'Daily');
}
print '&nbsp;';
if ($this_graph_period eq 'week') {
print 'Weekly';
} else {
print a({-href=>$selfurl.'&graph=week',-target=>'_self'}, 'Weekly')
}
print '&nbsp;';
if ($this_graph_period eq 'month') {
print 'Monthly';
} else {
print a({-href=>$selfurl.'&graph=month',-target=>'_self'}, 'Monthly')
}
print '&nbsp;';
if ($this_graph_period eq 'year') {
print 'Yearly';
} else {
print a({-href=>$selfurl.'&graph=year',-target=>'_self'}, 'Yearly')
}
print '&nbsp;';
87ca9250 Hamish Coleman
if ($this_graph_period eq 'test') {
print 'Test';
} else {
print a({-href=>$selfurl.'&graph=test',-target=>'_self'}, 'Test')
}
print '&nbsp;';
804e829a Hamish Coleman
print '(This List is ',$path,")\n";
}

ebfd527a Hamish Coleman
5b3eb8e6 Hamish Coleman
my $this_host = hostname;
f1213034 Hamish Coleman
if(!defined param('page')) {
ebfd527a Hamish Coleman
print header,
start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)",
-bgcolor=>"white",
-leftmargin=>"0", -topmargin=>"0",
-marginheight=>"0", -marginwidth=>"0",
-onResize=>"if (navigator.family == 'nn4') window.location.reload()" ),
"\n";

1b1c276c Hamish Coleman
config_load_file(@config_files);
1b114acb Hamish Coleman
config_load_munin(@munin_datafiles);
1b1c276c Hamish Coleman
config_read_filesystem();
maketree();
ebfd527a Hamish Coleman
print <<EOF;
<STYLE>
/* */
/* Styles for the tree. */
/* */
SPAN.TreeviewSpanArea A {
font-size: 10pt;
font-family: verdana,helvetica;
text-decoration: none;
color: black;}
SPAN.TreeviewSpanArea A:hover {
color: '#820082';}

/* */
/* Styles for the remainder of the document. */
/* */
BODY {
background-color: white;}
TD {
font-size: 10pt;
font-family: verdana,helvetica;}
</STYLE>
EOF
print '<script src="ua.js"></script>';
print '<script src="ftiens4.js"></script>';
print '<script>';
1b1c276c Hamish Coleman
emit_tree('treenf');
ebfd527a Hamish Coleman
print '</script>';
print <<EOF;
<TABLE cellpadding="0" cellspacing="0" border="0" width="772">
<TR>
<TD width="178" valign="top">

<TABLE cellpadding="4" cellspacing="0" border="0" width="100%">
<TR>
<TD bgcolor="#ECECD9">

<TABLE cellspacing="0" cellpadding="2" border="0" width="100%">
<TR>
bc24f7db Hamish Coleman
<td width="200" valign="top" bgcolor="#efefef" style="border-right: 1px solid rgb(170, 170, 170); padding: 5px;">
ebfd527a Hamish Coleman
bc24f7db Hamish Coleman
<TABLE border=0><TR><TD><FONT size=-2><A style="font-size:7pt;text-decoration:none;color:silver" href="http://www.treemenu.net/" target=_blank /></FONT></TD></TR></TABLE>
ebfd527a Hamish Coleman
<SPAN class=TreeviewSpanArea>
<SCRIPT>initializeDocument()</SCRIPT>
<NOSCRIPT>
A tree for site navigation will open here if you enable JavaScript in your browser.
</NOSCRIPT>
</SPAN>
</TD>
</TR>
</TABLE>

</TD>
</TR>
</TABLE>

</TD>
<TD bgcolor="white" valign="top">

<TABLE cellpadding="10" cellspacing="0" border="0" width="100%">
<TR>
<TD>
EOF

# Time the next update to occur a little while after the next interval completes
my $interval = 300; # 5 min update interval
my $guardband = 15; # updates occur this many seconds after predicted gif completion
my $refresh = $interval + $guardband + $gifdone - time; # predict how long until next update
$refresh = $interval if $refresh <= $guardband;
my $expires = gmtime (time + $interval * 2 + $guardband);

1b1c276c Hamish Coleman
my $path = param('path');
804e829a Hamish Coleman
my $graph_period = param('graph') || 'day';
ebfd527a Hamish Coleman
print
"<meta http-equiv=\"expires\" content=\"$expires GMT\">\n",
"<meta http-equiv=\"refresh\" content=$refresh>\n";
1b1c276c Hamish Coleman
if ($path) {
804e829a Hamish Coleman
emit_graph_period_chooser($path,$graph_period);
ebfd527a Hamish Coleman
print "<br/>";
804e829a Hamish Coleman
emit_path_contents($path,$graph_period);
ebfd527a Hamish Coleman
}

print <<EOF;
</TD>
</TR>
</TABLE>

f7fe9beb Hamish Coleman
</TD>
</TR>
</TABLE>
EOF

print end_html;
11780c4c Hamish Coleman
}
5b3eb8e6 Hamish Coleman
1b1c276c Hamish Coleman
af3a7f97 Hamish Coleman
if (param('debug')) {
#print "<pre>\n", "$warnings", "</pre>\n";
print "<pre>\n";
ddad10fd Hamish Coleman
print HTML::Entities::encode(Dumper(\$db)),"\n";
af3a7f97 Hamish Coleman
print "</pre>";
5b3eb8e6 Hamish Coleman
}