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 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 ################################################################################
37 use Fatal qw(open close);
39 use Getopt::Long qw(:config no_ignore_case bundling pass_through);
41 my $DIR = "/var/lib/collectd";
42 my $HOST = "_UNDEFINED_";
45 "host-is=s" => \$HOST,
49 my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
51 my @tmp = `/bin/hostname`; chomp(@tmp);
52 $HOST = $tmp[0] if ( $HOST =~ /_UNDEFINED_/ );
53 my $IMG_DIR = "${HOST}.dir";
54 my $HTML = "${HOST}.html";
56 ################################################################################
61 # Fade a color's component to the white.
63 ################################################################################
67 return (($component + 255 * 5) / 6);
70 ################################################################################
75 # Fade a color to the white.
77 ################################################################################
85 my $component = (($color >> $shft) & 255);
86 $r |= (fade_component($component) << $shft);
92 ################################################################################
96 ################################################################################
97 system("rm -fR $IMG_DIR");
98 system("mkdir -p $IMG_DIR");
101 my $title="Rrd plot for $HOST";
104 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
105 "http://www.w3.org/TR/html4/loose.dtd">
108 <title>$title</title>
114 # list interesting rrd
116 my @list = `ls $DIR/*.rrd`; chomp(@list);
118 foreach my $rrd (sort @list){
119 my $bn = basename($rrd);
126 <A name="top"></A><H1>$title</H1>
130 foreach my $bn (@rrds){
131 my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
133 <A href="#$cleaned_bn">$bn</A>
141 # graph interesting rrd
142 foreach my $bn (@rrds){
145 my $rrd = "$DIR/${bn}.rrd";
146 my $cmd = "rrdtool info $rrd |grep 'ds\\[' |sed 's/^ds\\[//'"
147 ." |sed 's/\\].*//' |sort |uniq";
148 my @dss = `$cmd`; chomp(@dss);
154 foreach my $ds (@dss){
155 $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
156 ." DEF:${ds}_max=$rrd:$ds:MAX ";
162 foreach my $ds (@dss){
163 my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
164 my $faded_color = fade_color($color);
165 $defs .= sprintf(" AREA:${ds}_max#%06x ", $faded_color);
171 foreach my $ds (@dss){
172 my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
173 $defs .= sprintf(" LINE2:${ds}_avg#%06x:$ds"
174 ." GPRINT:${ds}_avg:AVERAGE:%%5.1lf%%sAvg"
175 ." GPRINT:${ds}_max:MAX:%%5.1lf%%sMax"
179 my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
181 <A name="$cleaned_bn"></A><H1>$bn</H1>
184 # graph various ranges
185 foreach my $span qw(1hour 1day 1week 1month){
186 my $png = "$IMG_DIR/${bn}-$span.png";
188 my $cmd = "rrdtool graph $png"
189 ." -t \"$bn $span\" --imgformat PNG --width 600 --height 100"
190 ." --start now-$span --end now --interlaced"
191 ." $defs >/dev/null 2>&1";
194 my $cleaned_png = $png; $cleaned_png =~ s/%/%25/g;
196 <P><IMG src="$cleaned_png" alt="${bn} $span"></P>
201 <A href="#top">[top]</A>