index da143d700b3d65f41f51a84106264ebb3845cc2d..fe4e2bdb02663cf7777cf24553b443baf543faac 100644 (file)
--- a/contrib/collectd2html.pl
+++ b/contrib/collectd2html.pl
#
# Copyright 2006 Vincent Stehlé <vincent.stehle@free.fr>
#
+# Patch to configure the data directory and hostname by Eddy Petrisor
+# <eddy.petrisor@gmail.com>.
+#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
use File::Basename;
use Getopt::Long qw(:config no_ignore_case bundling pass_through);
-my $DIR = "/var/lib/collectd";
-my $HOST = "_UNDEFINED_";
+my $DIR = "/var/lib/collectd";
+my $HOST = undef;
+my $IMG_FMT = "PNG";
+my $RECURSIVE = 1;
GetOptions (
- "host-is=s" => \$HOST,
- "data-dir=s" => \$DIR
+ "host=s" => \$HOST,
+ "data-dir=s" => \$DIR,
+ "image-format=s" => \$IMG_FMT,
+ "recursive" => \$RECURSIVE
);
+if (($DIR !~ m/\/rrd\/?$/) && (-d "$DIR/rrd")) {
+ $DIR .= "/rrd";
+}
+
+if (defined($HOST) && ($DIR !~ m/\/$HOST\/?$/) && (-d "$DIR/$HOST")) {
+ $DIR .= "/$HOST";
+}
+
my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
0xffff77, 0x55aaff);
-my @tmp = `/bin/hostname`; chomp(@tmp);
-$HOST = $tmp[0] if ( $HOST =~ /_UNDEFINED_/ );
+my @tmp = `/bin/hostname -f`; chomp(@tmp);
+$HOST = $tmp[0] if (! defined $HOST);
+my $svg_p = ($IMG_FMT eq "SVG");
+my $IMG_SFX = $svg_p ? ".svg" : ".png";
my $IMG_DIR = "${HOST}.dir";
-my $HTML = "${HOST}.html";
+my $HTML = "${HOST}.xhtml";
################################################################################
#
my $title="Rrd plot for $HOST";
print OUT <<END;
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
-<html>
+<!DOCTYPE html PUBLIC
+ "-//W3C//DTD XHTML 1.1//EN"
+ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
<head>
+<style type="text/css" media="screen">
+.graph { text-align: center; }
+object.graph { width: 670; height: 179; }
+</style>
<title>$title</title>
+<meta http-equiv="Content-Type"
+ content="application/xhtml+xml; charset=us-ascii" />
</head>
<body>
-<center>
END
# list interesting rrd
my @rrds;
-my @list = `ls $DIR/*.rrd`; chomp(@list);
+my @list;
-foreach my $rrd (sort @list){
- my $bn = basename($rrd);
- $bn =~ s/\.rrd$//;
- push(@rrds, $bn);
+if ($RECURSIVE) {
+ @list = `find $DIR -type f -name '*.rrd'`;
+}
+else {
+ @list = `ls $DIR/*.rrd`;
+}
+chomp(@list);
+
+@list = sort @list;
+foreach my $rrd (@list){
+ $rrd =~ m/^$DIR\/(.*)\.rrd$/;
+ push(@rrds, $1);
}
# table of contents
print OUT <<END;
-<A name="top"></A><H1>$title</H1>
-<P>
+<h1><a id="top">$title</a></h1>
+<p>
END
foreach my $bn (@rrds){
- my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
+ my $cleaned_bn = $bn;
+ $cleaned_bn =~ tr/%\//__/;
print OUT <<END;
-<A href="#$cleaned_bn">$bn</A>
+<a href="#$cleaned_bn">$bn</a>
END
}
print OUT <<END;
-</P>
+</p>
END
# graph interesting rrd
-foreach my $bn (@rrds){
+for (my $i = 0; $i < scalar(@rrds); ++$i) {
+ my $bn = $rrds[$i];
print "$bn\n";
- my $rrd = "$DIR/${bn}.rrd";
+ my $rrd = $list[$i];
my $cmd = "rrdtool info $rrd |grep 'ds\\[' |sed 's/^ds\\[//'"
." |sed 's/\\].*//' |sort |uniq";
my @dss = `$cmd`; chomp(@dss);
# all DEF
- my $i = 0;
+ my $j = 0;
my $defs = "";
foreach my $ds (@dss){
}
# all AREA
- $i = 0;
+ $j = 0;
foreach my $ds (@dss){
- my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
+ my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
my $faded_color = fade_color($color);
$defs .= sprintf(" AREA:${ds}_max#%06x ", $faded_color);
}
# all LINE
- $i = 0;
+ $j = 0;
foreach my $ds (@dss){
- my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
+ my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
$defs .= sprintf(" LINE2:${ds}_avg#%06x:$ds"
." GPRINT:${ds}_avg:AVERAGE:%%5.1lf%%sAvg"
." GPRINT:${ds}_max:MAX:%%5.1lf%%sMax"
, $color);
}
- my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
+ my $cleaned_bn = $bn;
+ $cleaned_bn =~ tr/%\//__/;
print OUT <<END;
-<A name="$cleaned_bn"></A><H1>$bn</H1>
+<h2><a id="$cleaned_bn">$bn</a></h2>
END
# graph various ranges
foreach my $span qw(1hour 1day 1week 1month){
- my $png = "$IMG_DIR/${bn}-$span.png";
+ system("mkdir -p $IMG_DIR/" . dirname($bn));
+ my $img = "$IMG_DIR/${bn}-$span$IMG_SFX";
- my $cmd = "rrdtool graph $png"
- ." -t \"$bn $span\" --imgformat PNG --width 600 --height 100"
+ my $cmd = "rrdtool graph $img"
+ ." -t \"$bn $span\" --imgformat $IMG_FMT --width 600 --height 100"
." --start now-$span --end now --interlaced"
." $defs >/dev/null 2>&1";
system($cmd);
- my $cleaned_png = $png; $cleaned_png =~ s/%/%25/g;
- print OUT <<END;
-<P><IMG src="$cleaned_png" alt="${bn} $span"></P>
+ my $cleaned_img = $img; $cleaned_img =~ s/%/%25/g;
+ if (! $svg_p) {
+ print OUT <<END;
+<p class="graph"><img src="$cleaned_img" alt="${bn} $span" /></p>
+END
+ } else {
+ print OUT <<END;
+<p class="graph"><object data="$cleaned_img" type="image/svg+xml">
+ ${bn} $span</object></p>
END
+ }
}
print OUT <<END;
-<A href="#top">[top]</A>
+<p><a href="#top">[top]</a></p>
END
}
print OUT <<END;
-</center>
+<hr />
+<p>
+ <a href="http://validator.w3.org/check?uri=referer"><img
+ src="http://www.w3.org/Icons/valid-xhtml10"
+ alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
+</p>
</body>
</html>
END