chiark / gitweb /
slightly prettier news graphs
[rrd-graphs.git] / cgi
1 #!/usr/bin/perl -w
2
3 use strict qw(vars);
4 use CGI qw/:standard/;
5
6 sub fail ($) {
7     print(header(-status=>500),
8           start_html('Error'),
9           h1('Error'),
10           escapeHTML($_[0]),
11           end_html());
12     exit 0;
13 }
14
15 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
16 our $SELF= '/home/ijackson/things/rrd-graphs';
17
18 my $self= url(-relative=>1);
19
20 our (@sections, %sections, %graphs);
21
22 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1);
23
24 sub graph ($$$$) {
25     my ($section, $gname, $basis, $args) = @_;
26     $basis->{Args}= $args;
27     $basis->{Slower}= 0 unless exists $basis->{Slower};
28     $graphs{$section,$gname}= $basis;
29     if (!exists $sections{$section}) {
30         push @sections, $section;
31     }
32     push @{ $sections{$section} }, $gname;
33 }
34
35 graph('General', 'Load', { },
36       [
37        "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
38        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
39             qw(blocked running stopped paging sleeping zombies)),
40        "AREA:running#88f:running processes:STACK",
41        "AREA:blocked#8f8:blocked processes:STACK",
42        "AREA:paging#f88:paging processes:STACK",
43        "LINE:load#000:load",
44        ]);
45
46 graph('General', 'Processes', { },
47       [
48        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
49             qw(blocked running stopped paging sleeping zombies)),
50        "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
51        "AREA:sleeping#ccc:sleeping:STACK",
52        "AREA:stopped#00f:stopped:STACK",
53        "AREA:zombies#ff0:zombie:STACK",
54        "AREA:busy#000:busy:STACK",
55        ]);
56
57 graph('General', 'CPU', { Units => '[%]' },
58       [
59        (map {
60            my $thing= $_;
61            (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
62                 (0..7)),
63            "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
64        } qw(idle interrupt nice softirq steal system user wait)),
65        "AREA:system#00f:system:STACK",
66        "AREA:wait#f88:wait:STACK",
67        "AREA:nice#ccc:nice:STACK",
68        "AREA:user#080:user:STACK",
69        "AREA:softirq#f0f:softirq:STACK",
70        "AREA:interrupt#ff0:interrupt:STACK",
71        "AREA:steal#0ff:steal:STACK",
72        ]);
73
74 graph('General', 'Memory', { },
75       [ '-b',1024,
76        (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
77             qw(free used cached)),
78        (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
79             qw(buffered free used cached)),
80        "CDEF:c_swap_used=0,swap_used,-",
81        "CDEF:c_swap_cached=0,swap_cached,-",
82        "CDEF:c_swap_free=0,swap_free,-",
83        "AREA:c_swap_used#000:used swap",
84        "AREA:c_swap_cached#888:\"cached\" swap:STACK",
85 #       "AREA:c_swap_free#88f:free swap:STACK",
86        "AREA:mem_used#ff0:used memory",
87        "AREA:mem_buffered#00f:page cache:STACK",
88        "AREA:mem_cached#008:buffer cache:STACK",
89        "AREA:mem_free#ccc:unused memory:STACK",
90        ]);
91
92 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
93       [
94        (map {
95            ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
96             "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
97             "CDEF:mrx_$_=0,rx_$_,-")
98            } qw(octets packets errors)),
99        (map {
100            ("CDEF:${_}_kb=${_}_octets,1024,/",
101             "CDEF:${_}_errsx=${_}_errors,1000,*")
102            } qw(mrx tx)),
103        "AREA:tx_kb#080:kby",
104        "LINE:tx_packets#0f0:pkts",
105        "LINE:tx_errsx#000:errs",
106        "AREA:mrx_kb#008:kby",
107        "LINE:mrx_packets#00f:pkts",
108        "LINE:mrx_errsx#444:errs",
109       ]);
110
111 graph('General', 'Users', {  },
112       [
113        "DEF:users=$R/users/users.rrd:users:AVERAGE",
114        "LINE:users#008:users"
115        ]);
116
117 foreach my $src (<$R/df/df-*.rrd>) {
118     my $vol= $src;
119     $vol =~ s,.*/,,;
120     $vol =~ s,^df-,,;
121     $vol =~ s,\.rrd$,,;
122     graph('Disk space', $vol, {
123              Slower => 1,
124           },
125           [ '-A','-l',0,'-r',
126            qw(-b 1024 -l 0),
127            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
128            "AREA:used#000:used:STACK",
129            "AREA:free#88f:free:STACK",
130            ]);
131 }
132
133 foreach my $src (<$SELF/news-stats/*.rrd>) {
134     my $site= $src;
135     $site =~ s,.*/,,;
136     $site =~ s,\.rrd$,,;
137     $site =~ s,_(in|out)$,,;
138     my $inout= $1;
139     $site =~ s/^([-.0-9a-z]+)_//;
140     my $us= $1;
141     graph('News',
142           $inout eq 'out' ? "$us -> $site" : "$site -> $us",
143           {
144                 Slower => 1,
145             }, $inout eq 'out' ?
146           [
147            (map { "DEF:$_=$src:$_:AVERAGE" }
148                 qw(missing offered deferred unwanted accepted
149                    rejected body_missing)),
150            "AREA:accepted#00f:ok",
151            "AREA:body_missing#ff0:missing:STACK",
152            "AREA:rejected#f00:rej:STACK",
153            "AREA:unwanted#bbb:unw:STACK",
154            "AREA:deferred#eee:deferred:STACK",
155            "LINE:offered#080:",
156            ] :
157           [
158            (map { "DEF:$_=$src:$_:AVERAGE" }
159                 qw(accepted refused rejected duplicate)),
160            (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
161                    "CDEF:kb_$_=bytes_$_,1024,/")
162               } qw(accepted duplicate)),
163            "AREA:accepted#00f:ok:STACK",
164            "AREA:rejected#f00:rej:STACK",
165            "AREA:duplicate#000:dupe:STACK",
166            "AREA:refused#bbb:unw:STACK",
167            "LINE:kb_duplicate#ff0:kb dupe",
168            "LINE:kb_accepted#008:kb",
169            ]);
170 }
171
172 if (param('debug')) {
173     print "Content-Type: text/plain\n\n";
174 }
175
176 our @navsettings;
177
178 sub navsetting ($) {
179     my ($nav) = @_;
180     my $var= $nav->{Variable};
181     $$var= param($nav->{Param});
182     $$var= $nav->{Default} if !defined $$var;
183     die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
184     push @navsettings, $nav;
185 }
186
187 our $section;
188
189 navsetting({
190     Desc => 'Section',
191     Param => 'section',
192     Variable => \$section,
193     Default => $sections[0],
194     Values => [@sections],
195     Show => sub { return $_[0]; }
196 });
197
198
199 my $gname= param('graph');
200
201 sub num_param ($$$$) {
202     my ($param,$def,$min,$max) = @_;
203     my $v= param($param);
204     return $def if !defined $v;
205     $v =~ m/^([1-9]\d{0,8})$/ or die;
206     $v= $1;
207     die unless $v >= $min && $v <= $max;
208     return $v + 0;
209 }
210
211 if ($gname) {
212     my $g= $graphs{$section,$gname};
213     die unless $g;
214
215     my @args= @{ $g->{Args} };
216
217     my $width= num_param('w',370,100,1600);
218     my $height= num_param('h',200,100,1600);
219
220     my $end= param('end');
221     if (defined $end) {
222         $end =~ m/^(\d+)$/ or die;
223         unshift @args, qw(--end now --start), "end-${end}s";
224     }
225     if (param('debug')) {
226         print((join "\n",@args),"\n"); exit 0;
227     }
228     print "Content-Type: image/png\n\n";
229
230     my $title= $gname;
231     $title .= " $g->{Units}" if $g->{Units};
232     unshift @args, '-t', $title;
233     
234     exec (qw(rrdtool graph - -a PNG --full-size-mode),
235           '-w',$width, '-h',$height,
236           @args);
237     die $!;
238 }
239
240 sub start_page ($) {
241     my ($title) = @_;
242     print header(), start_html($title);
243     my $outerdelim= '';
244     foreach my $nav (@navsettings) {
245         print $outerdelim;
246         print $nav->{Desc}, ": ";
247         my $delim= '';
248         my $current= $nav->{Variable};  $current= $$current;
249         foreach my $couldbe (@{ $nav->{Values} }) {
250             print $delim;
251             my $show= $nav->{Show}($couldbe);
252             if ($couldbe eq $current) {
253                 print "<b>$show</b>";
254             } else {
255                 print "<a href=\"$self";
256                 my $delim2= '?';
257                 foreach my $nav2 (@navsettings) {
258                     my $current2= $nav2->{Variable};  $current2= $$current2;
259                     $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
260                     next if $current2 eq $nav2->{Default};
261                     print $delim2, "$nav2->{Param}=$current2";
262                     $delim2= '&';
263                 }
264                 print "\">$show</a>";
265             }
266             $delim= ' | ';
267         }
268         $outerdelim= "<br>\n";
269     }
270     print "\n";
271
272     print h1("$title");
273 }
274
275 my $detail= param('detail');
276 if ($detail) {
277     my $g= $graphs{$section,$detail};
278     die unless $g;
279     start_page("$detail graphs");
280     foreach my $end (@timeranges[$g->{Slower}..$g->{Slower}+3]) {
281         my $imgurl= "$self?graph=$detail&section=$section&end=$end";
282         print "<a href=\"$imgurl&w=780&h=800\"><img src=\"$imgurl\"></a>\n";
283     }
284     print end_html();
285     exit 0;
286 }
287
288 our $sloth;
289
290 navsetting({
291     Desc => 'Time interval',
292     Param => 'sloth',
293     Variable => \$sloth,
294     Default => 1,
295     Values => [0..2],
296     Show => sub {
297         my ($sl) = @_;
298         return ('Narrower', 'Normal', 'Wider')[$sl];
299     }
300 });
301
302 if (param('debug')) {
303     use Data::Dumper;
304     print Dumper(\%graphs);
305     exit 0;
306 }
307
308 start_page("$section graphs");
309
310 foreach my $gname (@{ $sections{$section} }) {
311     my $g= $graphs{$section,$gname};
312     print "<a href=\"$self?detail=$gname&section=$section\">";
313     my $end= $timeranges[$g->{Slower}+$sloth];
314     my $imgurl= "$self?graph=$gname&section=$section&end=$end";
315     print "<img src=\"$imgurl\"></a>\n";
316 }
317