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