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