collectd2html.pl: Allow for --imgformat to be passed to rrdtool.
[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 # Patch to configure the data directory and hostname by Eddy Petrisor
20 # <eddy.petrisor@gmail.com>.
21 #
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.
26 #
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.
31 #
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
35 #
36 ################################################################################
37
38 use warnings;
39 use strict;
40 use Fatal qw(open close);
41 use File::Basename;
42 use Getopt::Long qw(:config no_ignore_case bundling pass_through);
43
44 my $DIR  = "/var/lib/collectd";
45 my $HOST = undef;
46 my $IMG_FMT = "PNG";
47
48 GetOptions (
49     "host=s"     => \$HOST,
50     "data-dir=s" => \$DIR,
51     "image-format=s" => \$IMG_FMT
52 );
53
54 my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
55         0xffff77, 0x55aaff);
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";
62
63 ################################################################################
64 #
65 # fade_component
66 #
67 # Description:
68 #   Fade a color's component to the white.
69 #
70 ################################################################################
71 sub fade_component($)
72 {
73         my($component) = @_;
74         return (($component + 255 * 5) / 6);
75 }
76
77 ################################################################################
78 #
79 # fade_color
80 #
81 # Description:
82 #   Fade a color to the white.
83 #
84 ################################################################################
85 sub fade_color($)
86 {
87         my($color) = @_;
88         my $r = 0;
89
90         for my $i (0 .. 2){
91                 my $shft = ($i * 8);
92                 my $component = (($color >> $shft) & 255);
93                 $r |= (fade_component($component) << $shft);
94         }
95
96         return $r;
97 }
98
99 ################################################################################
100 #
101 # main
102 #
103 ################################################################################
104 system("rm -fR $IMG_DIR");
105 system("mkdir -p $IMG_DIR");
106 local *OUT;
107 open(OUT, ">$HTML");
108 my $title="Rrd plot for $HOST";
109
110 print OUT <<END;
111 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
112    "http://www.w3.org/TR/html4/loose.dtd">
113 <html>
114 <head>
115 <title>$title</title>
116 </head>
117 <body>
118 <center>
119 END
120
121 # list interesting rrd
122 my @rrds;
123 my @list = `ls $DIR/*.rrd`; chomp(@list);
124
125 foreach my $rrd (sort @list){
126         my $bn = basename($rrd);
127         $bn =~ s/\.rrd$//;
128         push(@rrds, $bn);
129 }
130
131 # table of contents
132 print OUT <<END;
133 <A name="top"></A><H1>$title</H1>
134 <P>
135 END
136
137 foreach my $bn (@rrds){
138         my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
139         print OUT <<END;
140 <A href="#$cleaned_bn">$bn</A>
141 END
142 }
143
144 print OUT <<END;
145 </P>
146 END
147
148 # graph interesting rrd
149 foreach my $bn (@rrds){
150         print "$bn\n";
151
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);
156
157         # all DEF
158         my $i = 0;
159         my $defs = "";
160
161         foreach my $ds (@dss){
162                 $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
163                         ." DEF:${ds}_max=$rrd:$ds:MAX ";
164         }
165
166         # all AREA
167         $i = 0;
168
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);
173         }
174
175         # all LINE      
176         $i = 0;
177
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"
183                         , $color);
184         }
185
186         my $cleaned_bn = $bn; $cleaned_bn =~ s/%/_/g;
187         print OUT <<END;
188 <A name="$cleaned_bn"></A><H1>$bn</H1>
189 END
190
191         # graph various ranges
192         foreach my $span qw(1hour 1day 1week 1month){
193                 my $img = "$IMG_DIR/${bn}-$span$IMG_SFX";
194
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";
199                 system($cmd);
200
201                 my $cleaned_img = $img; $cleaned_img =~ s/%/%25/g;
202                 if (! $svg_p) {
203                         print OUT <<END;
204 <P><IMG src="$cleaned_img" alt="${bn} $span"></P>
205 END
206                 } else {
207                         print OUT <<END;
208 <P><object data="$cleaned_img" type="image/svg+xml"
209            width="670" height="179">
210   ${bn} $span</object></P>
211 END
212                 }
213         }
214
215         print OUT <<END;
216 <A href="#top">[top]</A>
217 END
218 }
219
220 print OUT <<END;
221 </center>
222 </body>
223 </html>
224 END
225
226 close(OUT);