chiark / gitweb /
1d29b1d91a7e64fb6f6ce079b899e7305dc79249
[rrd-graphs.git] / cgi
1 #!/usr/bin/speedy -w -- -t100 -M1
2
3 use strict qw(vars);
4 use CGI::SpeedyCGI qw/:standard -no_xhtml/;
5 use CGI qw/:standard -no_xhtml/;
6 use POSIX;
7 use MD5;
8
9 sub fail ($) {
10     print(header(-status=>500),
11           start_html('Error'),
12           h1('Error'),
13           escapeHTML($_[0]),
14           end_html());
15     exit 0;
16 }
17
18 our (@sections, %section_groups, %group_elems, %graphs);
19
20 #---------- initialisation code, run once - graphs setup ----------
21
22 BEGIN {
23
24 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
25 our $SELF= '/home/ijackson/things/rrd-graphs';
26
27 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
28
29 sub graph_of_group ($$$$$) {
30     my ($section, $group, $elem, $basis, $args) = @_;
31     $basis->{Args}= $args;
32     $basis->{Slower}= 0 unless exists $basis->{Slower};
33     $basis->{TimeRanges} ||= \@timeranges;
34     $graphs{$section,$group,$elem}= $basis;
35     if (!exists $group_elems{$section,$group}) {
36         # new group then
37         if (!exists $section_groups{$section}) {
38             # new section even
39             push @sections, $section;
40         }
41         push @{ $section_groups{$section} }, $group;
42     }
43     push @{ $group_elems{$section,$group} }, $elem;
44 }
45
46 sub graph ($$$$) {
47     my ($section, $gname, $basis, $args) = @_;
48     graph_of_group($section, $gname,'', $basis, $args);
49 }
50
51 graph('General', 'Load and processes', { },
52       [
53        "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
54        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
55             qw(blocked running stopped paging sleeping zombies)),
56        "AREA:running#88f:running:STACK",
57        "AREA:blocked#8f8:disk wait:STACK",
58        "AREA:paging#f88:paging:STACK",
59        "LINE:load#000:load",
60        ]);
61
62 graph('General', 'Processes', { },
63       [
64        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
65             qw(blocked running stopped paging sleeping zombies)),
66        "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
67        "AREA:sleeping#ccc:sleeping:STACK",
68        "AREA:stopped#00f:stopped:STACK",
69        "AREA:zombies#ff0:zombie:STACK",
70        "AREA:busy#000:busy:STACK",
71        ]);
72
73 graph('General', 'CPU', { Units => '[%]' },
74       [
75        (map {
76            my $thing= $_;
77            (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
78                 (0..7)),
79            "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
80        } qw(idle interrupt nice softirq steal system user wait)),
81        "CDEF:allintr=softirq,steal,+,interrupt,+",
82        "AREA:allintr#ff0:interrupt:STACK",
83        "AREA:system#88f:system:STACK",
84        "AREA:user#00f:user:STACK",
85        "AREA:nice#ccc:nice:STACK",
86        "AREA:wait#f00:wait:STACK",
87        ]);
88
89 graph('General', 'Memory', { },
90       [ '-b',1024,
91        (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
92             qw(free used cached)),
93        (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
94             qw(buffered free used cached)),
95        "CDEF:c_swap_used=0,swap_used,-",
96        "CDEF:c_swap_cached=0,swap_cached,-",
97        "CDEF:c_swap_free=0,swap_free,-",
98        "AREA:c_swap_used#000:used swap",
99        "AREA:c_swap_cached#888:\"cached\" swap:STACK",
100 #       "AREA:c_swap_free#88f:free swap:STACK",
101        "AREA:mem_used#ff0:used memory",
102        "AREA:mem_buffered#00f:page cache:STACK",
103        "AREA:mem_cached#008:buffer cache:STACK",
104        "AREA:mem_free#ccc:unused memory:STACK",
105        ]);
106
107 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
108       [
109        (map {
110            ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
111             "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
112             "CDEF:mrx_$_=0,rx_$_,-")
113            } qw(octets packets errors)),
114        (map {
115            ("CDEF:${_}_kb=${_}_octets,1024,/",
116             "CDEF:${_}_errsx=${_}_errors,1000,*")
117            } qw(mrx tx)),
118        "AREA:tx_kb#080:kby",
119        "LINE:tx_packets#0f0:pkts",
120        "LINE:tx_errsx#000:errs",
121        "AREA:mrx_kb#008:kby",
122        "LINE:mrx_packets#00f:pkts",
123        "LINE:mrx_errsx#444:errs",
124       ]);
125
126 graph('General', 'Users', {  },
127       [
128        "DEF:users=$R/users/users.rrd:users:AVERAGE",
129        "LINE:users#008:users"
130        ]);
131
132 foreach my $src (<$R/df/df-*.rrd>) {
133     my $vol= $src;
134     $vol =~ s,\.rrd$,, or next;
135     $vol =~ s,.*/,,;
136     $vol =~ s,^df-,,;
137     graph('Disk space', $vol, {
138              Slower => 1,
139           },
140           [ '-A','-l',0,'-r',
141            qw(-b 1024 -l 0),
142            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
143            "AREA:used#000:used:STACK",
144            "AREA:free#88f:free:STACK",
145            ]);
146 }
147
148 our %news_name_map;
149
150 if (!open NM, '<', "$SELF/data/news/name-map") {
151     die unless $!==&ENOENT;
152 } else {
153     while (<NM>) {
154         s/^\s*//; s/\s+$//;
155         next unless m/^[^\#]/;
156         m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
157         if ($2 eq '*') {
158             $news_name_map{$1,$_}= $3 foreach qw(in out);
159         } else {
160             $news_name_map{$1,$2}= $3;
161         }
162     }
163 }
164
165 our %news_sources;
166
167 foreach my $src (<$SELF/data/news/*.rrd>) {
168     my $site= $src;
169     $site =~ s,\.rrd$,, or next;
170     $site =~ s,.*/,,;
171     $site =~ s,_(in|out)$,,;
172     my $inout= $1;
173     $site =~ s/^([-.0-9a-z]+)_//;
174     my $us= $1; # all very well but we ignore it
175     my $newsite= $news_name_map{$site,$inout};
176     $site= $newsite if defined $newsite;
177     next if $site eq '-';
178     push @{ $news_sources{$site}{$inout} }, $src;
179 }
180
181 our @news_graphs;
182
183 foreach my $site (keys %news_sources) {
184     my $sk= $site;
185     for (;;) {
186         last unless $sk =~
187             s/^[^.]*(?:chiark|greenend|news|nntp|peer|feed|in|out)[^.]*\.//;
188         $sk .= " $&";
189     }
190     foreach my $inout (keys %{ $news_sources{$site} }) {
191         push @news_graphs, [ "$sk $inout", $site, $inout ];
192     }
193 }
194
195 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
196     my ($sortkey, $site, $inout)= @$siteinfo;
197     my @sources= @{ $news_sources{$site}{$inout} };
198
199     my @vals= $inout eq 'out'
200         ? qw(missing deferred unwanted accepted rejected body_missing)
201         : qw(accepted refused rejected duplicate
202              accepted_size duplicate_size);
203     my @defs;
204     foreach my $val (@vals) {
205         my $def= "CDEF:$val=0";
206         foreach my $si (0..$#sources) {
207             my $src= $sources[$si];
208             my $tvar= "${val}_${si}";
209             push @defs, "DEF:$tvar=$src:$val:AVERAGE";
210             $def .= ",$tvar,ADDNAN";
211         }
212         push @defs, $def;
213         if ($val =~ m/_size$/) {
214             push @defs, "CDEF:kb_$`=$val,1024,/";
215         }
216     }
217     graph_of_group("News", $site, $inout,
218           {
219                 Units => '[art/s]',
220                 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
221             }, $inout eq 'out' ?
222           [
223            @defs,
224            "AREA:accepted#00f:ok",
225            "AREA:body_missing#ff0:miss:STACK",
226            "AREA:rejected#f00:rej:STACK",
227            "AREA:unwanted#aaa:unw:STACK",
228            "AREA:deferred#ddd:defer:STACK",
229            ] :
230           [
231            @defs,
232            "AREA:accepted#00f:ok:STACK",
233            "AREA:rejected#f00:rej:STACK",
234            "AREA:duplicate#000:dupe:STACK",
235            "AREA:refused#aaa:unw:STACK",
236            "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
237            "LINE:kb_duplicate#ff0:kb dupe",
238            "LINE:kb_accepted_smooth#008:~kb",
239            ]);
240 }
241
242 our %disk_rdev2rrd;
243
244 foreach my $physdiskrrd (<$R/disk-*/disk_octets.rrd>) {
245     $physdiskrrd =~ s,octets\.rrd$,, or die;
246     $physdiskrrd =~ m,-([^/]+)/disk_$, or die;
247     my $physdev= "/dev/$1";
248     if (!stat $physdev) {
249         die "$physdev $!" unless $!==&ENOENT;
250         next;
251     }
252     die "$physdev ?" unless S_ISBLK((stat _)[2]);
253     $disk_rdev2rrd{(stat _)[6]}= $physdiskrrd;
254 }
255
256 our @disk_vgs;
257
258 sub lvgraphs {
259     my ($vg, $label, $factor, $rcolour, $wcolour) = @_;
260     my @lvs;
261     my $varname= $vg;
262     $varname =~ s/[^0-9a-zA-Y]/ sprintf "Z%02x", ord($&) /ge;
263     my $vginfo= {
264         Name => $label,
265         Varname => $varname,
266         Colour => { 'read' => $rcolour, 'write' => $wcolour },
267         Lvs => []
268     };
269     foreach my $bo (qw(octets ops)) {
270         foreach my $rw (qw(read write)) {
271             $vginfo->{VarDefs}{$bo}{$rw}= [];
272             $vginfo->{Sumdef}{$bo}{$rw}= '0';
273         }
274     }
275     my $ix=0;
276     foreach my $lvpath (</dev/$vg/*>) {
277         my $lv= $lvpath; $lv =~ s,.*/,,;
278         if (!stat $lvpath) {
279             die "$lvpath $!" unless $!==&ENOENT;
280             next;
281         }
282         die "$lvpath ?" unless S_ISBLK((stat _)[2]);
283         my $rrd= $disk_rdev2rrd{(stat _)[6]};
284         next unless defined $rrd;
285
286         my $lvinfo= { Name => $lv };
287         push @{ $vginfo->{Lvs} }, $lvinfo;
288
289         foreach my $bo (qw(octets ops)) {
290             $lvinfo->{Defs}{$bo}=
291               [
292                (map { ("DEF:$_=${rrd}${bo}.rrd:$_:AVERAGE") } qw(read write)),
293                "CDEF:mwrite=0,write,-",
294                "AREA:read#00f:read",
295                "AREA:mwrite#f00:write"
296                ];
297
298             foreach my $rw (qw(read write)) {
299                 $ix++;
300                 my $tvar= "lv_${rw}_${bo}_${varname}_${ix}";
301                 push @{ $vginfo->{VarDefs}{$bo}{$rw} },
302                     "DEF:$tvar=${rrd}${bo}.rrd:$rw:AVERAGE";
303                 $vginfo->{Sumdef}{$bo}{$rw} .= ",$tvar,+";
304             }
305         }
306     }
307     foreach my $bo (qw(octets ops)) {
308         foreach my $rw (qw(read write)) {
309             my $defs= [];
310             push @$defs, @{ $vginfo->{VarDefs}{$bo}{$rw} };
311             push @$defs, "CDEF:${rw}_vg_${varname}=".
312                 $vginfo->{Sumdef}{$bo}{$rw}.
313                 sprintf(",%f,*", $rw eq 'write' ? -$factor : $factor);
314             $vginfo->{Defs}{$bo}{$rw}= $defs;
315         }
316     }
317     push @disk_vgs, $vginfo;
318 }
319
320 lvgraphs('vg-main',          'main',     1, qw(00f f00));
321 lvgraphs('vg-chiark-stripe', 'stripe', 0.5, qw(008 800));
322
323 foreach my $bo (qw(octets ops)) {
324     my @a= ();
325     foreach my $rw (qw(read write)) {
326         my $stack= '';
327         foreach my $vginfo (@disk_vgs) {
328             push @a, @{ $vginfo->{Defs}{$bo}{$rw} };
329             push @a, "AREA:${rw}_vg_$vginfo->{Varname}#".
330                 $vginfo->{Colour}{$rw}.
331                 ":$vginfo->{Name} ".substr($rw,0,1).
332                 $stack;
333             $stack= ':STACK';
334         }
335     }
336     graph_of_group('IO', 'IO', $bo, { Units => '[/s]' }, \@a);
337 }
338
339 foreach my $vginfo (@disk_vgs) {
340     foreach my $bo (qw(octets ops)) {
341         foreach my $lv (@{ $vginfo->{Lvs} }) {
342             graph_of_group('IO', "$vginfo->{Name} $lv->{Name}",
343                            $bo, { Units => '[/s]' }, $lv->{Defs}{$bo});
344         }
345     }
346 }
347
348 push @{ $section_groups{General} }, {
349     Section => 'IO',
350     Group => 'IO',
351     UrlParams => "section=IO&sloth=SLOTH"
352 };
353
354 }
355 #---------- right, that was the initialisation ----------
356
357 our $self= url(-relative=>1);
358
359 if (param('debug')) {
360     print "Content-Type: text/plain\n\n";
361 }
362
363 our @navsettings;
364
365 @navsettings= ();
366
367 sub navsetting ($) {
368     my ($nav) = @_;
369     my $var= $nav->{Variable};
370     $$var= param($nav->{Param});
371     $$var= $nav->{Default} if !defined $$var;
372     die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
373     push @navsettings, $nav;
374 }
375
376 our $section;
377
378 navsetting({
379     Desc => 'Section',
380     Param => 'section',
381     Variable => \$section,
382     Default => $sections[0],
383     Values => [@sections],
384     Show => sub { return $_[0]; }
385 });
386
387
388 sub num_param ($$$$) {
389     my ($param,$def,$min,$max) = @_;
390     my $v= param($param);
391     return $def if !defined $v;
392     $v =~ m/^([1-9]\d{0,8})$/ or die;
393     $v= $1;
394     die unless $v >= $min && $v <= $max;
395     return $v + 0;
396 }
397
398 our $group= param('graph');
399
400 our $elem= param('elem');
401 if (defined $elem) {
402     my $g= $graphs{$section,$group,$elem};
403     die unless $g;
404
405     my $width= num_param('w',370,100,1600);
406     my $height= num_param('h',200,100,1600);
407
408     my $sloth= param('sloth');
409     die unless defined $sloth;
410     $sloth =~ m/^(\d+)$/ or die;
411     $sloth= $1+0;
412     my $end= $g->{TimeRanges}[$sloth];
413     die unless defined $end;
414
415     my @args= @{ $g->{Args} };
416     s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
417     unshift @args, qw(--end now --start), "end-${end}s";
418     
419     my $title= $group;
420     if (length $elem) { $title.= " $elem"; }
421
422     $title .= " $g->{Units}" if $g->{Units};
423     unshift @args, '-t', $title, '-w',$width, '-h',$height;
424     unshift @args, qw(-a PNG --full-size-mode);
425
426     my $cacheid= "$section!$group!$elem!$sloth!$width!$height!";
427     $cacheid .= unpack "H*", MD5->hash(join '\0', @args);
428     my $cachepath= "cache/$cacheid.png";
429
430     if (param('debug')) {
431         print((join "\n",@args),"\n"); exit 0;
432     }
433
434 #print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
435     exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
436         p="$1"; shift
437         rrdtool graph "$p" --lazy "$@" >/dev/null
438         printf "Content-Type: image/png\n\n"
439         exec cat "$p"
440 END
441     die $!;
442 }
443
444 sub start_page ($) {
445     my ($title) = @_;
446     print header(), start_html($title);
447     my $outerdelim= '';
448     foreach my $nav (@navsettings) {
449         print $outerdelim;
450         print $nav->{Desc}, ": ";
451         my $delim= '';
452         my $current= $nav->{Variable};  $current= $$current;
453         foreach my $couldbe (@{ $nav->{Values} }) {
454             print $delim;
455             my $show= $nav->{Show}($couldbe);
456             if ($couldbe eq $current) {
457                 print "<b>$show</b>";
458             } else {
459                 my $u= $self;
460                 my $delim2= '?';
461                 foreach my $nav2 (@navsettings) {
462                     my $current2= $nav2->{Variable};  $current2= $$current2;
463                     $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
464                     next if $current2 eq $nav2->{Default};
465                     $u .= $delim2;  $u .= "$nav2->{Param}=$current2";
466                     $delim2= '&';
467                 }
468                 print a({href=>$u}, $show);
469             }
470             $delim= ' | ';
471         }
472         $outerdelim= "<br>\n";
473     }
474     print "\n";
475
476     print h1("$title");
477 }
478
479 our $detail= param('detail');
480 if (defined $detail) {
481     my $elems= $group_elems{$section,$detail};
482     die unless $elems;
483     start_page("$detail - $section - graphs");
484     foreach my $tsloth (0..5) {
485         foreach my $elem (@$elems) {
486             my $g= $graphs{$section,$detail,$elem};
487             die unless $g;
488             next if $tsloth >= @{ $g->{TimeRanges} };
489             my $imgurl= "$self?graph=$detail&section=$section".
490                 "&sloth=$tsloth&elem=$elem";
491             print a({href=>"$imgurl&w=780&h=800"},
492                     img({src=>$imgurl, alt=>''}));
493         }
494     }
495     print end_html();
496     exit 0;
497 }
498
499 our $sloth;
500
501 navsetting({
502     Desc => 'Time interval',
503     Param => 'sloth',
504     Variable => \$sloth,
505     Default => 1,
506     Values => [0..3],
507     Show => sub {
508         my ($sl) = @_;
509         return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
510     }
511 });
512
513 if (param('debug')) {
514     use Data::Dumper;
515     print Dumper(\%graphs);
516     exit 0;
517 }
518
519 start_page("$section - graphs");
520
521 foreach my $group (@{ $section_groups{$section} }) {
522     my $ref_group= $group;
523     my $ref_section= $section;
524     my $ref_urlparams= "detail=$group&section=$section";
525     if (ref $group) {
526         $ref_group= $group->{Group};
527         $ref_section= $group->{Section};
528         $ref_urlparams= $group->{UrlParams};
529         $ref_urlparams =~ s/\bSLOTH\b/$sloth/;
530     }
531     print a({href=>"$self?$ref_urlparams"});
532     my $imgurl= "$self?graph=$ref_group&section=$ref_section";
533     print "<span style=\"white-space:nowrap\">";
534     my $elems= $group_elems{$ref_section,$ref_group};
535     foreach my $elem (@$elems) {
536         my $g= $graphs{$ref_section,$ref_group,$elem};
537         print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
538                    alt=>''});
539     }
540     print "</span>";
541     print "</a>\n";
542 }
543