1 #!/usr/bin/perl
3 ################################################################################
4 #
5 # collectd2html.pl
6 #
7 # Description:
8 # Generate an html page with all rrd data gathered by collectd.
9 #
10 # Usage:
11 # collectd2html.pl
12 #
13 # When run on <host>, it generated <host>.html and <host>.dir, the latter
14 # containing all necessary images.
15 #
16 #
17 # Copyright 2006 Vincent Stehlé <vincent.stehle@free.fr>
18 #
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.
23 #
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.
28 #
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
32 #
33 ################################################################################
35 use warnings;
36 use strict;
37 use Fatal qw(open close);
38 use File::Basename;
39 use Getopt::Long qw(:config no_ignore_case bundling pass_through);
41 my $DIR = "/var/lib/collectd";
42 my $HOST = "_UNDEFINED_";
44 GetOptions (
45 "host-is=s" => \$HOST,
46 "data-dir=s" => \$DIR
47 );
49 my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
50 0xffff77, 0x55aaff);
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 ################################################################################
57 #
58 # fade_component
59 #
60 # Description:
61 # Fade a color's component to the white.
62 #
63 ################################################################################
64 sub fade_component($)
65 {
66 my($component) = @_;
67 return (($component + 255 * 5) / 6);
68 }
70 ################################################################################
71 #
72 # fade_color
73 #
74 # Description:
75 # Fade a color to the white.
76 #
77 ################################################################################
78 sub fade_color($)
79 {
80 my($color) = @_;
81 my $r = 0;
83 for my $i (0 .. 2){
84 my $shft = ($i * 8);
85 my $component = (($color >> $shft) & 255);
86 $r |= (fade_component($component) << $shft);
87 }
89 return $r;
90 }
92 ################################################################################
93 #
94 # main
95 #
96 ################################################################################
97 system("rm -fR $IMG_DIR");
98 system("mkdir -p $IMG_DIR");
99 local *OUT;
100 open(OUT, ">$HTML");
101 my $title="Rrd plot for $HOST";
103 print OUT <<END;
104 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
105 "http://www.w3.org/TR/html4/loose.dtd">
106 <html>
107 <head>
108 <title>$title</title>
109 </head>
110 <body>
111 <center>
112 END
114 # list interesting rrd
115 my @rrds;
116 my @list = `ls $DIR/*.rrd`; chomp(@list);
118 foreach my $rrd (sort @list){
119 my $bn = basename($rrd);
120 $bn =~ s/\.rrd$//;
121 push(@rrds, $bn);
122 }
124 # table of contents
125 print OUT <<END;
126 <A name="top"></A><H1>$title</H1>
127 <P>
128 END
130 foreach my $bn (@rrds){
131 my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
132 print OUT <<END;
133 <A href="#$cleaned_bn">$bn</A>
134 END
135 }
137 print OUT <<END;
138 </P>
139 END
141 # graph interesting rrd
142 foreach my $bn (@rrds){
143 print "$bn\n";
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);
150 # all DEF
151 my $i = 0;
152 my $defs = "";
154 foreach my $ds (@dss){
155 $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
156 ." DEF:${ds}_max=$rrd:$ds:MAX ";
157 }
159 # all AREA
160 $i = 0;
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);
166 }
168 # all LINE
169 $i = 0;
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"
176 , $color);
177 }
179 my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
180 print OUT <<END;
181 <A name="$cleaned_bn"></A><H1>$bn</H1>
182 END
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";
192 system($cmd);
194 my $cleaned_png = $png; $cleaned_png =~ s/%/%25/g;
195 print OUT <<END;
196 <P><IMG src="$cleaned_png" alt="${bn} $span"></P>
197 END
198 }
200 print OUT <<END;
201 <A href="#top">[top]</A>
202 END
203 }
205 print OUT <<END;
206 </center>
207 </body>
208 </html>
209 END
211 close(OUT);