chiark / gitweb /
smoother and better news graphs
[rrd-graphs.git] / cgi
1 #!/usr/bin/perl -w
2
3 use strict qw(vars);
4 use CGI qw/:standard -no_xhtml/;
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, 366);
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     $basis->{TimeRanges} ||= \@timeranges;
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 and processes', { },
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:STACK",
52        "AREA:blocked#8f8:disk wait:STACK",
53        "AREA:paging#f88:paging: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        "CDEF:allintr=softirq,steal,+,interrupt,+",
77        "AREA:system#88f:system:STACK",
78        "AREA:allintr#ff0:interrupt:STACK",
79        "AREA:user#00f:user:STACK",
80        "AREA:nice#ccc:nice:STACK",
81        "AREA:wait#f00:wait:STACK",
82        ]);
83
84 graph('General', 'Memory', { },
85       [ '-b',1024,
86        (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
87             qw(free used cached)),
88        (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
89             qw(buffered free used cached)),
90        "CDEF:c_swap_used=0,swap_used,-",
91        "CDEF:c_swap_cached=0,swap_cached,-",
92        "CDEF:c_swap_free=0,swap_free,-",
93        "AREA:c_swap_used#000:used swap",
94        "AREA:c_swap_cached#888:\"cached\" swap:STACK",
95 #       "AREA:c_swap_free#88f:free swap:STACK",
96        "AREA:mem_used#ff0:used memory",
97        "AREA:mem_buffered#00f:page cache:STACK",
98        "AREA:mem_cached#008:buffer cache:STACK",
99        "AREA:mem_free#ccc:unused memory:STACK",
100        ]);
101
102 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
103       [
104        (map {
105            ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
106             "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
107             "CDEF:mrx_$_=0,rx_$_,-")
108            } qw(octets packets errors)),
109        (map {
110            ("CDEF:${_}_kb=${_}_octets,1024,/",
111             "CDEF:${_}_errsx=${_}_errors,1000,*")
112            } qw(mrx tx)),
113        "AREA:tx_kb#080:kby",
114        "LINE:tx_packets#0f0:pkts",
115        "LINE:tx_errsx#000:errs",
116        "AREA:mrx_kb#008:kby",
117        "LINE:mrx_packets#00f:pkts",
118        "LINE:mrx_errsx#444:errs",
119       ]);
120
121 graph('General', 'Users', {  },
122       [
123        "DEF:users=$R/users/users.rrd:users:AVERAGE",
124        "LINE:users#008:users"
125        ]);
126
127 foreach my $src (<$R/df/df-*.rrd>) {
128     my $vol= $src;
129     $vol =~ s,\.rrd$,, or next;
130     $vol =~ s,.*/,,;
131     $vol =~ s,^df-,,;
132     graph('Disk space', $vol, {
133              Slower => 1,
134           },
135           [ '-A','-l',0,'-r',
136            qw(-b 1024 -l 0),
137            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
138            "AREA:used#000:used:STACK",
139            "AREA:free#88f:free:STACK",
140            ]);
141 }
142
143 our %news_name_map;
144
145 if (!open NM, '<', "$SELF/data/news/name-map") {
146     die unless $!==&ENOENT;
147 } else {
148     while (<NM>) {
149         s/^\s*//; s/\s+$//;
150         next unless m/^[^\#]/;
151         m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
152         if ($2 eq '*') {
153             $news_name_map{$1,$_}= $3 foreach qw(in out);
154         } else {
155             $news_name_map{$1,$2}= $3;
156         }
157     }
158 }
159
160 sub news_name_sortkey {
161     return join '.', reverse split /\./, $_[0];
162 }
163
164 foreach my $src (sort { news_name_sortkey($a) cmp news_name_sortkey($b) }
165                  <$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     graph_of_group("News", $site, $inout,
177           {
178                 Units => '[art/s]',
179                 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
180             }, $inout eq 'out' ?
181           [
182            (map { "DEF:$_=$src:$_:AVERAGE" }
183                 qw(missing deferred unwanted accepted rejected body_missing)),
184            "AREA:accepted#00f:ok",
185            "AREA:body_missing#ff0:miss:STACK",
186            "AREA:rejected#f00:rej:STACK",
187            "AREA:unwanted#aaa:unw:STACK",
188            "AREA:deferred#ddd:defer:STACK",
189            ] :
190           [
191            (map { "DEF:$_=$src:$_:AVERAGE" }
192                 qw(accepted refused rejected duplicate)),
193            (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
194                    "CDEF:kb_$_=bytes_$_,1024,/")
195               } qw(accepted duplicate)),
196            "AREA:accepted#00f:ok:STACK",
197            "AREA:rejected#f00:rej:STACK",
198            "AREA:duplicate#000:dupe:STACK",
199            "AREA:refused#aaa:unw:STACK",
200            "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
201            "LINE:kb_duplicate#ff0:kb dupe",
202            "LINE:kb_accepted_smooth#008:~kb",
203            ]);
204 }
205
206 if (param('debug')) {
207     print "Content-Type: text/plain\n\n";
208 }
209
210 our @navsettings;
211
212 sub navsetting ($) {
213     my ($nav) = @_;
214     my $var= $nav->{Variable};
215     $$var= param($nav->{Param});
216     $$var= $nav->{Default} if !defined $$var;
217     die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
218     push @navsettings, $nav;
219 }
220
221 our $section;
222
223 navsetting({
224     Desc => 'Section',
225     Param => 'section',
226     Variable => \$section,
227     Default => $sections[0],
228     Values => [@sections],
229     Show => sub { return $_[0]; }
230 });
231
232
233 sub num_param ($$$$) {
234     my ($param,$def,$min,$max) = @_;
235     my $v= param($param);
236     return $def if !defined $v;
237     $v =~ m/^([1-9]\d{0,8})$/ or die;
238     $v= $1;
239     die unless $v >= $min && $v <= $max;
240     return $v + 0;
241 }
242
243 my $group= param('graph');
244
245 my $elem= param('elem');
246 if (defined $elem) {
247     my $g= $graphs{$section,$group,$elem};
248     die unless $g;
249
250     my $width= num_param('w',370,100,1600);
251     my $height= num_param('h',200,100,1600);
252
253     my $sloth= param('sloth');
254     die unless defined $sloth;
255     $sloth =~ m/^(\d+)$/ or die;
256     my $end= $g->{TimeRanges}[$sloth];
257     die unless defined $end;
258
259     my @args= @{ $g->{Args} };
260     s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
261     unshift @args, qw(--end now --start), "end-${end}s";
262     
263     if (param('debug')) {
264         print((join "\n",@args),"\n"); exit 0;
265     }
266     print "Content-Type: image/png\n\n";
267
268     my $title= $group;
269     if (length $elem) { $title.= " $elem"; }
270
271     $title .= " $g->{Units}" if $g->{Units};
272     unshift @args, '-t', $title;
273
274     exec (qw(rrdtool graph - -a PNG --full-size-mode),
275           '-w',$width, '-h',$height,
276           @args);
277     die $!;
278 }
279
280 sub start_page ($) {
281     my ($title) = @_;
282     print header(), start_html($title);
283     my $outerdelim= '';
284     foreach my $nav (@navsettings) {
285         print $outerdelim;
286         print $nav->{Desc}, ": ";
287         my $delim= '';
288         my $current= $nav->{Variable};  $current= $$current;
289         foreach my $couldbe (@{ $nav->{Values} }) {
290             print $delim;
291             my $show= $nav->{Show}($couldbe);
292             if ($couldbe eq $current) {
293                 print "<b>$show</b>";
294             } else {
295                 my $u= $self;
296                 my $delim2= '?';
297                 foreach my $nav2 (@navsettings) {
298                     my $current2= $nav2->{Variable};  $current2= $$current2;
299                     $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
300                     next if $current2 eq $nav2->{Default};
301                     $u .= $delim2;  $u .= "$nav2->{Param}=$current2";
302                     $delim2= '&';
303                 }
304                 print a({href=>$u}, $show);
305             }
306             $delim= ' | ';
307         }
308         $outerdelim= "<br>\n";
309     }
310     print "\n";
311
312     print h1("$title");
313 }
314
315 my $detail= param('detail');
316 if ($detail) {
317     my $elems= $group_elems{$section,$detail};
318     die unless $elems;
319     start_page("$detail graphs");
320     foreach my $xsloth (0..5) {
321         foreach my $elem (@$elems) {
322             my $g= $graphs{$section,$detail,$elem};
323             die unless $g;
324             my $tsloth= $xsloth + $g->{Slower};
325             my $imgurl= "$self?graph=$detail&section=$section".
326                 "&sloth=$tsloth&elem=$elem";
327             print a({href=>"$imgurl&w=780&h=800"},
328                     img({src=>$imgurl, alt=>''}));
329         }
330     }
331     print end_html();
332     exit 0;
333 }
334
335 our $sloth;
336
337 navsetting({
338     Desc => 'Time interval',
339     Param => 'sloth',
340     Variable => \$sloth,
341     Default => 1,
342     Values => [0..3],
343     Show => sub {
344         my ($sl) = @_;
345         return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
346     }
347 });
348
349 if (param('debug')) {
350     use Data::Dumper;
351     print Dumper(\%graphs);
352     exit 0;
353 }
354
355 start_page("$section graphs");
356
357 foreach my $group (@{ $section_groups{$section} }) {
358     print a({href=>"$self?detail=$group&section=$section"});
359     my $imgurl= "$self?graph=$group&section=$section";
360     print "<span style=\"white-space:nowrap\">";
361     my $elems= $group_elems{$section,$group};
362     foreach my $elem (@$elems) {
363         my $g= $graphs{$section,$group,$elem};
364         print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
365                    alt=>''});
366     }
367     print "</span>";
368     print "</a>\n";
369 }
370