collectd2html.pl: Produce XHTML output.
[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 my $RECURSIVE = 1;
48
49 GetOptions (
50     "host=s"         => \$HOST,
51     "data-dir=s"     => \$DIR,
52     "image-format=s" => \$IMG_FMT,
53     "recursive"      => \$RECURSIVE
54 );
55
56 if (($DIR !~ m/\/rrd\/?$/) && (-d "$DIR/rrd")) {
57         $DIR .= "/rrd";
58 }
59
60 if (defined($HOST) && ($DIR !~ m/\/$HOST\/?$/) && (-d "$DIR/$HOST")) {
61         $DIR .= "/$HOST";
62 }
63
64 my @COLORS = (0xff7777, 0x7777ff, 0x55ff55, 0xffcc77, 0xff77ff, 0x77ffff,
65         0xffff77, 0x55aaff);
66 my @tmp = `/bin/hostname -f`; chomp(@tmp);
67 $HOST = $tmp[0] if (! defined $HOST);
68 my $svg_p = ($IMG_FMT eq "SVG");
69 my $IMG_SFX = $svg_p ? ".svg" : ".png";
70 my $IMG_DIR = "${HOST}.dir";
71 my $HTML = "${HOST}.html";
72
73 ################################################################################
74 #
75 # fade_component
76 #
77 # Description:
78 #   Fade a color's component to the white.
79 #
80 ################################################################################
81 sub fade_component($)
82 {
83         my($component) = @_;
84         return (($component + 255 * 5) / 6);
85 }
86
87 ################################################################################
88 #
89 # fade_color
90 #
91 # Description:
92 #   Fade a color to the white.
93 #
94 ################################################################################
95 sub fade_color($)
96 {
97         my($color) = @_;
98         my $r = 0;
99
100         for my $i (0 .. 2){
101                 my $shft = ($i * 8);
102                 my $component = (($color >> $shft) & 255);
103                 $r |= (fade_component($component) << $shft);
104         }
105
106         return $r;
107 }
108
109 ################################################################################
110 #
111 # main
112 #
113 ################################################################################
114 system("rm -fR $IMG_DIR");
115 system("mkdir -p $IMG_DIR");
116 local *OUT;
117 open(OUT, ">$HTML");
118 my $title="Rrd plot for $HOST";
119
120 print OUT <<END;
121 <!DOCTYPE html PUBLIC
122   "-//W3C//DTD XHTML 1.1//EN"
123   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
124 <html xmlns="http://www.w3.org/1999/xhtml">
125 <head>
126 <style type="text/css" media="screen">
127 body { text-align: center; }
128 </style>
129 <title>$title</title>
130 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
131 </head>
132 <body>
133 END
134
135 # list interesting rrd
136 my @rrds;
137 my @list;
138
139 if ($RECURSIVE) {
140         @list = `find $DIR -type f -name '*.rrd'`;
141 }
142 else {
143         @list = `ls $DIR/*.rrd`;
144 }
145 chomp(@list);
146
147 @list = sort @list;
148 foreach my $rrd (@list){
149         $rrd =~ m/^$DIR\/(.*)\.rrd$/;
150         push(@rrds, $1);
151 }
152
153 # table of contents
154 print OUT <<END;
155 <h1><a id="top">$title</a></h1>
156 <p>
157 END
158
159 foreach my $bn (@rrds){
160         my $cleaned_bn = $bn;
161         $cleaned_bn =~ tr/%\//__/;
162         print OUT <<END;
163 <a href="#$cleaned_bn">$bn</a>
164 END
165 }
166
167 print OUT <<END;
168 </p>
169 END
170
171 # graph interesting rrd
172 for (my $i = 0; $i < scalar(@rrds); ++$i) {
173         my $bn = $rrds[$i];
174         print "$bn\n";
175
176         my $rrd = $list[$i];
177         my $cmd = "rrdtool info $rrd |grep 'ds\\[' |sed 's/^ds\\[//'" 
178                 ." |sed 's/\\].*//' |sort |uniq";
179         my @dss = `$cmd`; chomp(@dss);
180
181         # all DEF
182         my $j = 0;
183         my $defs = "";
184
185         foreach my $ds (@dss){
186                 $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
187                         ." DEF:${ds}_max=$rrd:$ds:MAX ";
188         }
189
190         # all AREA
191         $j = 0;
192
193         foreach my $ds (@dss){
194                 my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
195                 my $faded_color = fade_color($color);
196                 $defs .= sprintf(" AREA:${ds}_max#%06x ", $faded_color);
197         }
198
199         # all LINE      
200         $j = 0;
201
202         foreach my $ds (@dss){
203                 my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
204                 $defs .= sprintf(" LINE2:${ds}_avg#%06x:$ds"
205                         ." GPRINT:${ds}_avg:AVERAGE:%%5.1lf%%sAvg"
206                         ." GPRINT:${ds}_max:MAX:%%5.1lf%%sMax"
207                         , $color);
208         }
209
210         my $cleaned_bn = $bn;
211         $cleaned_bn =~ tr/%\//__/;
212         print OUT <<END;
213 <h1><a id="$cleaned_bn">$bn</a></h1>
214 END
215
216         # graph various ranges
217         foreach my $span qw(1hour 1day 1week 1month){
218                 system("mkdir -p $IMG_DIR/" . dirname($bn));
219                 my $img = "$IMG_DIR/${bn}-$span$IMG_SFX";
220
221                 my $cmd = "rrdtool graph $img"
222                         ." -t \"$bn $span\" --imgformat $IMG_FMT --width 600 --height 100"
223                         ." --start now-$span --end now --interlaced"
224                         ." $defs >/dev/null 2>&1";
225                 system($cmd);
226
227                 my $cleaned_img = $img; $cleaned_img =~ s/%/%25/g;
228                 if (! $svg_p) {
229                         print OUT <<END;
230 <p><img src="$cleaned_img" alt="${bn} $span" /></p>
231 END
232                 } else {
233                         print OUT <<END;
234 <p><object data="$cleaned_img" type="image/svg+xml"
235            width="670" height="179">
236   ${bn} $span</object></p>
237 END
238                 }
239         }
240
241         print OUT <<END;
242 <p><a href="#top">[top]</a></p>
243 END
244 }
245
246 print OUT <<END;
247 </body>
248 </html>
249 END
250
251 close(OUT);