Added `collectd2html.pl' by Vincent Stehlé
[collectd.git] / contrib / collectd2html.pl
1 #!/usr/bin/perl
2
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 ################################################################################
34
35 use warnings;
36 use strict;
37 use Fatal qw(open close);
38 use File::Basename;
39
40 my $DIR = "/var/lib/collectd";
41 my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
42         0xffff77, 0x55aaff);
43 my @tmp = `/bin/hostname`; chomp(@tmp);
44 my $HOST = $tmp[0];
45 my $IMG_DIR = "${HOST}.dir";
46 my $HTML = "${HOST}.html";
47
48 ################################################################################
49 #
50 # fade_component
51 #
52 # Description:
53 #   Fade a color's component to the white.
54 #
55 ################################################################################
56 sub fade_component($)
57 {
58         my($component) = @_;
59         return (($component + 255 * 5) / 6);
60 }
61
62 ################################################################################
63 #
64 # fade_color
65 #
66 # Description:
67 #   Fade a color to the white.
68 #
69 ################################################################################
70 sub fade_color($)
71 {
72         my($color) = @_;
73         my $r = 0;
74
75         for my $i (0 .. 2){
76                 my $shft = ($i * 8);
77                 my $component = (($color >> $shft) & 255);
78                 $r |= (fade_component($component) << $shft);
79         }
80
81         return $r;
82 }
83
84 ################################################################################
85 #
86 # main
87 #
88 ################################################################################
89 system("rm -fR $IMG_DIR");
90 system("mkdir -p $IMG_DIR");
91 local *OUT;
92 open(OUT, ">$HTML");
93 my $title="Rrd plot for $HOST";
94
95 print OUT <<END;
96 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
97    "http://www.w3.org/TR/html4/loose.dtd">
98 <html>
99 <head>
100 <title>$title</title>
101 </head>
102 <body>
103 <center>
104 END
105
106 # list interesting rrd
107 my @rrds;
108 my @list = `ls $DIR/*.rrd`; chomp(@list);
109
110 foreach my $rrd (sort @list){
111         my $bn = basename($rrd);
112         $bn =~ s/\.rrd$//;
113         push(@rrds, $bn);
114 }
115
116 # table of contents
117 print OUT <<END;
118 <A name="top"></A><H1>$title</H1>
119 <P>
120 END
121
122 foreach my $bn (@rrds){
123         my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
124         print OUT <<END;
125 <A href="#$cleaned_bn">$bn</A>
126 END
127 }
128
129 print OUT <<END;
130 </P>
131 END
132
133 # graph interesting rrd
134 foreach my $bn (@rrds){
135         print "$bn\n";
136
137         my $rrd = "$DIR/${bn}.rrd";
138         my $cmd = "rrdtool info $rrd |grep 'ds\\[' |sed 's/^ds\\[//'" 
139                 ." |sed 's/\\].*//' |sort |uniq";
140         my @dss = `$cmd`; chomp(@dss);
141
142         # all DEF
143         my $i = 0;
144         my $defs = "";
145
146         foreach my $ds (@dss){
147                 $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
148                         ." DEF:${ds}_max=$rrd:$ds:MAX ";
149         }
150
151         # all AREA
152         $i = 0;
153
154         foreach my $ds (@dss){
155                 my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
156                 my $faded_color = fade_color($color);
157                 $defs .= sprintf(" AREA:${ds}_max#%06x ", $faded_color);
158         }
159
160         # all LINE      
161         $i = 0;
162
163         foreach my $ds (@dss){
164                 my $color = $COLORS[$i % scalar(@COLORS)]; $i++;
165                 $defs .= sprintf(" LINE2:${ds}_avg#%06x:$ds"
166                         ." GPRINT:${ds}_avg:AVERAGE:%%5.1lf%%sAvg"
167                         ." GPRINT:${ds}_max:MAX:%%5.1lf%%sMax"
168                         , $color);
169         }
170
171         my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
172         print OUT <<END;
173 <A name="$cleaned_bn"></A><H1>$bn</H1>
174 END
175
176         # graph various ranges
177         foreach my $span qw(1hour 1day 1week 1month){
178                 my $png = "$IMG_DIR/${bn}-$span.png";
179
180                 my $cmd = "rrdtool graph $png"
181                         ." -t \"$bn $span\" --imgformat PNG --width 600 --height 100"
182                         ." --start now-$span --end now --interlaced"
183                         ." $defs >/dev/null 2>&1";
184                 system($cmd);
185
186                 my $cleaned_png = $png; $cleaned_png =~ s/%/%25/g;
187                 print OUT <<END;
188 <P><IMG src="$cleaned_png" alt="${bn} $span"></P>
189 END
190         }
191
192         print OUT <<END;
193 <A href="#top">[top]</A>
194 END
195 }
196
197 print OUT <<END;
198 </center>
199 </body>
200 </html>
201 END
202
203 close(OUT);