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";
50 "data-dir=s" => \$DIR,
51 "image-format=s" => \$IMG_FMT
54 my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
56 my @tmp = `/bin/hostname`; chomp(@tmp);
57 $HOST = $tmp[0] if (! defined $HOST);
58 my $svg_p = ($IMG_FMT eq "SVG");
59 my $IMG_SFX = $svg_p ? ".svg" : ".png";
60 my $IMG_DIR = "${HOST}.dir";
61 my $HTML = "${HOST}.html";
63 ################################################################################
68 # Fade a color's component to the white.
70 ################################################################################
74 return (($component + 255 * 5) / 6);
77 ################################################################################
82 # Fade a color to the white.
84 ################################################################################
92 my $component = (($color >> $shft) & 255);
93 $r |= (fade_component($component) << $shft);
99 ################################################################################
103 ################################################################################
104 system("rm -fR $IMG_DIR");
105 system("mkdir -p $IMG_DIR");
108 my $title="Rrd plot for $HOST";
111 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
112 "http://www.w3.org/TR/html4/loose.dtd">
115 <title>$title</title>
121 # list interesting rrd
123 my @list = `ls $DIR/*.rrd`; chomp(@list);
125 foreach my $rrd (sort @list){
126 my $bn = basename($rrd);
133 <A name="top"></A><H1>$title</H1>
137 foreach my $bn (@rrds){
138 my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
140 <A href="#$cleaned_bn">$bn</A>
148 # graph interesting rrd
149 foreach my $bn (@rrds){
152 my $rrd = "$DIR/${bn}.rrd";
153 my $cmd = "rrdtool info $rrd |grep 'ds\\[' |sed 's/^ds\\[//'"
154 ." |sed 's/\\].*//' |sort |uniq";
155 my @dss = `$cmd`; chomp(@dss);
161 foreach my $ds (@dss){
162 $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
163 ." DEF:${ds}_max=$rrd:$ds:MAX ";
169 foreach my $ds (@dss){
170 my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
171 my $faded_color = fade_color($color);
172 $defs .= sprintf(" AREA:${ds}_max#%06x ", $faded_color);
178 foreach my $ds (@dss){
179 my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
180 $defs .= sprintf(" LINE2:${ds}_avg#%06x:$ds"
181 ." GPRINT:${ds}_avg:AVERAGE:%%5.1lf%%sAvg"
182 ." GPRINT:${ds}_max:MAX:%%5.1lf%%sMax"
186 my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
188 <A name="$cleaned_bn"></A><H1>$bn</H1>
191 # graph various ranges
192 foreach my $span qw(1hour 1day 1week 1month){
193 my $img = "$IMG_DIR/${bn}-$span$IMG_SFX";
195 my $cmd = "rrdtool graph $img"
196 ." -t \"$bn $span\" --imgformat $IMG_FMT --width 600 --height 100"
197 ." --start now-$span --end now --interlaced"
198 ." $defs >/dev/null 2>&1";
201 my $cleaned_img = $img; $cleaned_img =~ s/%/%25/g;
204 <P><IMG src="$cleaned_img" alt="${bn} $span"></P>
208 <P><object data="$cleaned_img" type="image/svg+xml"
209 width="670" height="179">
210 ${bn} $span</object></P>
216 <A href="#top">[top]</A>