chiark / gitweb /
new invoke script
[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
7 sub fail ($) {
8     print(header(-status=>500),
9           start_html('Error'),
10           h1('Error'),
11           escapeHTML($_[0]),
12           end_html());
13     exit 0;
14 }
15
16 our (@sections, %section_groups, %group_elems, %graphs);
17
18 #---------- initialisation code, run once - graphs setup ----------
19
20 BEGIN {
21
22 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
23 our $SELF= '/home/ijackson/things/rrd-graphs';
24
25 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
26
27 sub graph_of_group ($$$$$) {
28     my ($section, $group, $elem, $basis, $args) = @_;
29     $basis->{Args}= $args;
30     $basis->{Slower}= 0 unless exists $basis->{Slower};
31     $basis->{TimeRanges} ||= \@timeranges;
32     $graphs{$section,$group,$elem}= $basis;
33     if (!exists $group_elems{$section,$group}) {
34         # new group then
35         if (!exists $section_groups{$section}) {
36             # new section even
37             push @sections, $section;
38         }
39         push @{ $section_groups{$section} }, $group;
40     }
41     push @{ $group_elems{$section,$group} }, $elem;
42 }
43
44 sub graph ($$$$) {
45     my ($section, $gname, $basis, $args) = @_;
46     graph_of_group($section, $gname,'', $basis, $args);
47 }
48
49 graph('General', 'Load and processes', { },
50       [
51        "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
52        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
53             qw(blocked running stopped paging sleeping zombies)),
54        "AREA:running#88f:running:STACK",
55        "AREA:blocked#8f8:disk wait:STACK",
56        "AREA:paging#f88:paging:STACK",
57        "LINE:load#000:load",
58        ]);
59
60 graph('General', 'Processes', { },
61       [
62        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
63             qw(blocked running stopped paging sleeping zombies)),
64        "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
65        "AREA:sleeping#ccc:sleeping:STACK",
66        "AREA:stopped#00f:stopped:STACK",
67        "AREA:zombies#ff0:zombie:STACK",
68        "AREA:busy#000:busy:STACK",
69        ]);
70
71 graph('General', 'CPU', { Units => '[%]' },
72       [
73        (map {
74            my $thing= $_;
75            (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
76                 (0..7)),
77            "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
78        } qw(idle interrupt nice softirq steal system user wait)),
79        "CDEF:allintr=softirq,steal,+,interrupt,+",
80        "AREA:allintr#ff0:interrupt:STACK",
81        "AREA:system#88f:system:STACK",
82        "AREA:user#00f:user:STACK",
83        "AREA:nice#ccc:nice:STACK",
84        "AREA:wait#f00:wait:STACK",
85        ]);
86
87 graph('General', 'Memory', { },
88       [ '-b',1024,
89        (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
90             qw(free used cached)),
91        (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
92             qw(buffered free used cached)),
93        "CDEF:c_swap_used=0,swap_used,-",
94        "CDEF:c_swap_cached=0,swap_cached,-",
95        "CDEF:c_swap_free=0,swap_free,-",
96        "AREA:c_swap_used#000:used swap",
97        "AREA:c_swap_cached#888:\"cached\" swap:STACK",
98 #       "AREA:c_swap_free#88f:free swap:STACK",
99        "AREA:mem_used#ff0:used memory",
100        "AREA:mem_buffered#00f:page cache:STACK",
101        "AREA:mem_cached#008:buffer cache:STACK",
102        "AREA:mem_free#ccc:unused memory:STACK",
103        ]);
104
105 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
106       [
107        (map {
108            ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
109             "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
110             "CDEF:mrx_$_=0,rx_$_,-")
111            } qw(octets packets errors)),
112        (map {
113            ("CDEF:${_}_kb=${_}_octets,1024,/",
114             "CDEF:${_}_errsx=${_}_errors,1000,*")
115            } qw(mrx tx)),
116        "AREA:tx_kb#080:kby",
117        "LINE:tx_packets#0f0:pkts",
118        "LINE:tx_errsx#000:errs",
119        "AREA:mrx_kb#008:kby",
120        "LINE:mrx_packets#00f:pkts",
121        "LINE:mrx_errsx#444:errs",
122       ]);
123
124 graph('General', 'Users', {  },
125       [
126        "DEF:users=$R/users/users.rrd:users:AVERAGE",
127        "LINE:users#008:users"
128        ]);
129
130 foreach my $src (<$R/df/df-*.rrd>) {
131     my $vol= $src;
132     $vol =~ s,\.rrd$,, or next;
133     $vol =~ s,.*/,,;
134     $vol =~ s,^df-,,;
135     graph('Disk space', $vol, {
136              Slower => 1,
137           },
138           [ '-A','-l',0,'-r',
139            qw(-b 1024 -l 0),
140            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
141            "AREA:used#000:used:STACK",
142            "AREA:free#88f:free:STACK",
143            ]);
144 }
145
146 our %news_name_map;
147
148 if (!open NM, '<', "$SELF/data/news/name-map") {
149     die unless $!==&ENOENT;
150 } else {
151     while (<NM>) {
152         s/^\s*//; s/\s+$//;
153         next unless m/^[^\#]/;
154         m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
155         if ($2 eq '*') {
156             $news_name_map{$1,$_}= $3 foreach qw(in out);
157         } else {
158             $news_name_map{$1,$2}= $3;
159         }
160     }
161 }
162
163 our @news_graphs;
164
165 foreach my $src (<$SELF/data/news/*.rrd>) {
166     my $site= $src;
167     $site =~ s,\.rrd$,, or next;
168     $site =~ s,.*/,,;
169     $site =~ s,_(in|out)$,,;
170     my $inout= $1;
171     $site =~ s/^([-.0-9a-z]+)_//;
172     my $us= $1; # all very well but we ignore it
173     my $newsite= $news_name_map{$site,$inout};
174     $site= $newsite if defined $newsite;
175     next if $site eq '-';
176     #my $sk= join '.', reverse split /\./, $site;
177     my $sk= $site;
178     $sk .= " $&" if $sk =~ s/^[^.]*(?:news|nntp|peer)[^.]*\.//;
179     $sk .= " $inout";
180     push @news_graphs, [ $sk, $site, $inout, $src ];
181 }
182
183 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
184     my ($sortkey, $site, $inout, $src)= @$siteinfo;
185     graph_of_group("News", $site, $inout,
186           {
187                 Units => '[art/s]',
188                 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
189             }, $inout eq 'out' ?
190           [
191            (map { "DEF:$_=$src:$_:AVERAGE" }
192                 qw(missing deferred unwanted accepted rejected body_missing)),
193            "AREA:accepted#00f:ok",
194            "AREA:body_missing#ff0:miss:STACK",
195            "AREA:rejected#f00:rej:STACK",
196            "AREA:unwanted#aaa:unw:STACK",
197            "AREA:deferred#ddd:defer:STACK",
198            ] :
199           [
200            (map { "DEF:$_=$src:$_:AVERAGE" }
201                 qw(accepted refused rejected duplicate)),
202            (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
203                    "CDEF:kb_$_=bytes_$_,1024,/")
204               } qw(accepted duplicate)),
205            "AREA:accepted#00f:ok:STACK",
206            "AREA:rejected#f00:rej:STACK",
207            "AREA:duplicate#000:dupe:STACK",
208            "AREA:refused#aaa:unw:STACK",
209            "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
210            "LINE:kb_duplicate#ff0:kb dupe",
211            "LINE:kb_accepted_smooth#008:~kb",
212            ]);
213 }
214
215 }
216
217 #---------- right, that was the initialisation ----------
218
219 our $self= url(-relative=>1);
220
221 if (param('debug')) {
222     print "Content-Type: text/plain\n\n";
223 }
224
225 our @navsettings;
226
227 @navsettings= ();
228
229 sub navsetting ($) {
230     my ($nav) = @_;
231     my $var= $nav->{Variable};
232     $$var= param($nav->{Param});
233     $$var= $nav->{Default} if !defined $$var;
234     die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
235     push @navsettings, $nav;
236 }
237
238 our $section;
239
240 navsetting({
241     Desc => 'Section',
242     Param => 'section',
243     Variable => \$section,
244     Default => $sections[0],
245     Values => [@sections],
246     Show => sub { return $_[0]; }
247 });
248
249
250 sub num_param ($$$$) {
251     my ($param,$def,$min,$max) = @_;
252     my $v= param($param);
253     return $def if !defined $v;
254     $v =~ m/^([1-9]\d{0,8})$/ or die;
255     $v= $1;
256     die unless $v >= $min && $v <= $max;
257     return $v + 0;
258 }
259
260 our $group= param('graph');
261
262 our $elem= param('elem');
263 if (defined $elem) {
264     my $g= $graphs{$section,$group,$elem};
265     die unless $g;
266
267     my $width= num_param('w',370,100,1600);
268     my $height= num_param('h',200,100,1600);
269
270     my $sloth= param('sloth');
271     die unless defined $sloth;
272     $sloth =~ m/^(\d+)$/ or die;
273     $sloth= $1+0;
274     my $end= $g->{TimeRanges}[$sloth];
275     die unless defined $end;
276
277     my $cacheid= "$section!$group!$elem!$sloth!$width!$height";
278     my $cachepath= "cache/$cacheid.png";
279
280     my @args= @{ $g->{Args} };
281     s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
282     unshift @args, qw(--end now --start), "end-${end}s";
283     
284     my $title= $group;
285     if (length $elem) { $title.= " $elem"; }
286
287     $title .= " $g->{Units}" if $g->{Units};
288     unshift @args, '-t', $title, '-w',$width, '-h',$height;
289     unshift @args, qw(-a PNG --full-size-mode);
290
291     if (param('debug')) {
292         print((join "\n",@args),"\n"); exit 0;
293     }
294
295 #print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
296     exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
297         p="$1"; shift
298         rrdtool graph "$p" --lazy "$@" >/dev/null
299         printf "Content-Type: image/png\n\n"
300         exec cat "$p"
301 END
302     die $!;
303 }
304
305 sub start_page ($) {
306     my ($title) = @_;
307     print header(), start_html($title);
308     my $outerdelim= '';
309     foreach my $nav (@navsettings) {
310         print $outerdelim;
311         print $nav->{Desc}, ": ";
312         my $delim= '';
313         my $current= $nav->{Variable};  $current= $$current;
314         foreach my $couldbe (@{ $nav->{Values} }) {
315             print $delim;
316             my $show= $nav->{Show}($couldbe);
317             if ($couldbe eq $current) {
318                 print "<b>$show</b>";
319             } else {
320                 my $u= $self;
321                 my $delim2= '?';
322                 foreach my $nav2 (@navsettings) {
323                     my $current2= $nav2->{Variable};  $current2= $$current2;
324                     $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
325                     next if $current2 eq $nav2->{Default};
326                     $u .= $delim2;  $u .= "$nav2->{Param}=$current2";
327                     $delim2= '&';
328                 }
329                 print a({href=>$u}, $show);
330             }
331             $delim= ' | ';
332         }
333         $outerdelim= "<br>\n";
334     }
335     print "\n";
336
337     print h1("$title");
338 }
339
340 our $detail= param('detail');
341 if ($detail) {
342     my $elems= $group_elems{$section,$detail};
343     die unless $elems;
344     start_page("$detail graphs");
345     foreach my $tsloth (0..5) {
346         foreach my $elem (@$elems) {
347             my $g= $graphs{$section,$detail,$elem};
348             die unless $g;
349             next if $tsloth >= @{ $g->{TimeRanges} };
350             my $imgurl= "$self?graph=$detail&section=$section".
351                 "&sloth=$tsloth&elem=$elem";
352             print a({href=>"$imgurl&w=780&h=800"},
353                     img({src=>$imgurl, alt=>''}));
354         }
355     }
356     print end_html();
357     exit 0;
358 }
359
360 our $sloth;
361
362 navsetting({
363     Desc => 'Time interval',
364     Param => 'sloth',
365     Variable => \$sloth,
366     Default => 1,
367     Values => [0..3],
368     Show => sub {
369         my ($sl) = @_;
370         return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
371     }
372 });
373
374 if (param('debug')) {
375     use Data::Dumper;
376     print Dumper(\%graphs);
377     exit 0;
378 }
379
380 start_page("$section graphs");
381
382 foreach my $group (@{ $section_groups{$section} }) {
383     print a({href=>"$self?detail=$group&section=$section"});
384     my $imgurl= "$self?graph=$group&section=$section";
385     print "<span style=\"white-space:nowrap\">";
386     my $elems= $group_elems{$section,$group};
387     foreach my $elem (@$elems) {
388         my $g= $graphs{$section,$group,$elem};
389         print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
390                    alt=>''});
391     }
392     print "</span>";
393     print "</a>\n";
394 }
395