3 ################################################################################
8 # Generate an html page with all rrd data gathered by collectd.
13 # When run on <host>, it generated <host>.html and <host>.dir, the latter
14 # containing all necessary images.
17 # Copyright 2006 Vincent Stehlé <vincent.stehle@free.fr>
19 # Patch to configure the data directory and hostname by Eddy Petrisor
20 # <eddy.petrisor@gmail.com>.
22 # This program is free software; you can redistribute it and/or modify
23 # it under the terms of the GNU General Public License as published by
24 # the Free Software Foundation; either version 2 of the License, or
25 # (at your option) any later version.
27 # This program is distributed in the hope that it will be useful,
28 # but WITHOUT ANY WARRANTY; without even the implied warranty of
29 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 # GNU General Public License for more details.
32 # You should have received a copy of the GNU General Public License
33 # along with this program; if not, write to the Free Software
34 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 ################################################################################
40 use Fatal qw(open close);
42 use Getopt::Long qw(:config no_ignore_case bundling pass_through);
44 my $DIR = "/var/lib/collectd";
51 "data-dir=s" => \$DIR,
52 "image-format=s" => \$IMG_FMT,
53 "recursive" => \$RECURSIVE
56 if (($DIR !~ m/\/rrd\/?$/) && (-d "$DIR/rrd")) {
60 if (defined($HOST) && ($DIR !~ m/\/$HOST\/?$/) && (-d "$DIR/$HOST")) {
64 my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
66 my @tmp = `/bin/hostname -f`; chomp(@tmp);
67 $HOST = $tmp[0] if (! defined $HOST);
68 my $svg_p = ($IMG_FMT eq "SVG");
69 my $IMG_SFX = $svg_p ? ".svg" : ".png";
70 my $IMG_DIR = "${HOST}.dir";
71 my $HTML = "${HOST}.xhtml";
73 ################################################################################
78 # Fade a color's component to the white.
80 ################################################################################
84 return (($component + 255 * 5) / 6);
87 ################################################################################
92 # Fade a color to the white.
94 ################################################################################
102 my $component = (($color >> $shft) & 255);
103 $r |= (fade_component($component) << $shft);
109 ################################################################################
113 ################################################################################
114 system("rm -fR $IMG_DIR");
115 system("mkdir -p $IMG_DIR");
118 my $title="Rrd plot for $HOST";
121 <!DOCTYPE html PUBLIC
122 "-//W3C//DTD XHTML 1.1//EN"
123 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
124 <html xmlns="http://www.w3.org/1999/xhtml">
126 <style type="text/css" media="screen">
127 .graph { text-align: center; }
128 object.graph { width: 670; height: 179; }
130 <title>$title</title>
131 <meta http-equiv="Content-Type"
132 content="application/xhtml+xml; charset=us-ascii" />
137 # list interesting rrd
142 @list = `find $DIR -type f -name '*.rrd'`;
145 @list = `ls $DIR/*.rrd`;
150 foreach my $rrd (@list){
151 $rrd =~ m/^$DIR\/(.*)\.rrd$/;
157 <h1><a id="top">$title</a></h1>
161 foreach my $bn (@rrds){
162 my $cleaned_bn = $bn;
163 $cleaned_bn =~ tr/%\//__/;
165 <a href="#$cleaned_bn">$bn</a>
173 # graph interesting rrd
174 for (my $i = 0; $i < scalar(@rrds); ++$i) {
179 my $cmd = "rrdtool info $rrd |grep 'ds\\[' |sed 's/^ds\\[//'"
180 ." |sed 's/\\].*//' |sort |uniq";
181 my @dss = `$cmd`; chomp(@dss);
187 foreach my $ds (@dss){
188 $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
189 ." DEF:${ds}_max=$rrd:$ds:MAX ";
195 foreach my $ds (@dss){
196 my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
197 my $faded_color = fade_color($color);
198 $defs .= sprintf(" AREA:${ds}_max#%06x ", $faded_color);
204 foreach my $ds (@dss){
205 my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
206 $defs .= sprintf(" LINE2:${ds}_avg#%06x:$ds"
207 ." GPRINT:${ds}_avg:AVERAGE:%%5.1lf%%sAvg"
208 ." GPRINT:${ds}_max:MAX:%%5.1lf%%sMax"
212 my $cleaned_bn = $bn;
213 $cleaned_bn =~ tr/%\//__/;
215 <h2><a id="$cleaned_bn">$bn</a></h2>
218 # graph various ranges
219 foreach my $span qw(1hour 1day 1week 1month){
220 system("mkdir -p $IMG_DIR/" . dirname($bn));
221 my $img = "$IMG_DIR/${bn}-$span$IMG_SFX";
223 my $cmd = "rrdtool graph $img"
224 ." -t \"$bn $span\" --imgformat $IMG_FMT --width 600 --height 100"
225 ." --start now-$span --end now --interlaced"
226 ." $defs >/dev/null 2>&1";
229 my $cleaned_img = $img; $cleaned_img =~ s/%/%25/g;
232 <p class="graph"><img src="$cleaned_img" alt="${bn} $span" /></p>
236 <p class="graph"><object data="$cleaned_img" type="image/svg+xml">
237 ${bn} $span</object></p>
243 <p><a href="#top">[top]</a></p>
250 <a href="http://validator.w3.org/check?uri=referer"><img
251 src="http://www.w3.org/Icons/valid-xhtml10"
252 alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>