1 #!/usr/bin/speedy -w -- -t100 -M1
3 # Main CGI program's logic; must be run inside a lock.
5 # rrd-graphs/cgi - part of rrd-graphs, a tool for online graphs
6 # Copyright 2010, 2012 Ian Jackson
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 use CGI::SpeedyCGI qw/:standard -no_xhtml/;
24 use CGI qw/:standard -no_xhtml/;
29 print(header(-status=>500),
37 our (@sections, %section_groups, %group_elems, %graphs);
39 #---------- initialisation code, run once - graphs setup ----------
43 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
44 our $SELF= '/home/ijackson/things/rrd-graphs';
46 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
48 sub graph_of_group ($$$$$) {
49 my ($section, $group, $elem, $basis, $args) = @_;
50 $basis->{Args}= $args;
51 $basis->{Slower}= 0 unless exists $basis->{Slower};
52 $basis->{TimeRanges} ||= \@timeranges;
53 $graphs{$section,$group,$elem}= $basis;
54 if (!exists $group_elems{$section,$group}) {
56 if (!exists $section_groups{$section}) {
58 push @sections, $section;
60 push @{ $section_groups{$section} }, $group;
62 push @{ $group_elems{$section,$group} }, $elem;
66 my ($section, $gname, $basis, $args) = @_;
67 graph_of_group($section, $gname,'', $basis, $args);
70 graph('General', 'Load and processes', { },
72 "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
73 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
74 qw(blocked running stopped paging sleeping zombies)),
75 "AREA:running#88f:running:STACK",
76 "AREA:blocked#8f8:disk wait:STACK",
77 "AREA:paging#f88:paging:STACK",
81 graph('General', 'Processes', { },
83 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
84 qw(blocked running stopped paging sleeping zombies)),
85 "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
86 "AREA:sleeping#ccc:sleeping:STACK",
87 "AREA:stopped#00f:stopped:STACK",
88 "AREA:zombies#ff0:zombie:STACK",
89 "AREA:busy#000:busy:STACK",
92 graph('General', 'CPU', { Units => '[%]' },
96 (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
98 "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
99 } qw(idle interrupt nice softirq steal system user wait)),
100 "CDEF:allintr=softirq,steal,+,interrupt,+",
101 "AREA:allintr#ff0:interrupt:STACK",
102 "AREA:system#88f:system:STACK",
103 "AREA:user#00f:user:STACK",
104 "AREA:nice#ccc:nice:STACK",
105 "AREA:wait#f00:wait:STACK",
108 graph('General', 'Memory', { },
110 (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
111 qw(free used cached)),
112 (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
113 qw(buffered free used cached)),
114 "CDEF:c_swap_used=0,swap_used,-",
115 "CDEF:c_swap_cached=0,swap_cached,-",
116 "CDEF:c_swap_free=0,swap_free,-",
117 "AREA:c_swap_used#000:used swap",
118 "AREA:c_swap_cached#888:\"cached\" swap:STACK",
119 # "AREA:c_swap_free#88f:free swap:STACK",
120 "AREA:mem_used#ff0:used memory",
121 "AREA:mem_buffered#00f:page cache:STACK",
122 "AREA:mem_cached#008:buffer cache:STACK",
123 "AREA:mem_free#ccc:unused memory:STACK",
126 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
129 ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
130 "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
131 "CDEF:mrx_$_=0,rx_$_,-")
132 } qw(octets packets errors)),
134 ("CDEF:${_}_kb=${_}_octets,1024,/",
135 "CDEF:${_}_errsx=${_}_errors,1000,*")
137 "AREA:tx_kb#080:kby",
138 "LINE:tx_packets#0f0:pkts",
139 "LINE:tx_errsx#000:errs",
140 "AREA:mrx_kb#008:kby",
141 "LINE:mrx_packets#00f:pkts",
142 "LINE:mrx_errsx#444:errs",
145 graph('General', 'Users', { },
147 "DEF:users=$R/users/users.rrd:users:AVERAGE",
148 "LINE:users#008:users"
151 foreach my $src (<$R/df/df-*.rrd>) {
153 $vol =~ s,\.rrd$,, or next;
156 graph('Disk space', $vol, {
161 (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
162 "AREA:used#000:used:STACK",
163 "AREA:free#88f:free:STACK",
169 if (!open NM, '<', "$SELF/data/news/name-map") {
170 die unless $!==&ENOENT;
174 next unless m/^[^\#]/;
175 m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
177 $news_name_map{$1,$_}= $3 foreach qw(in out);
179 $news_name_map{$1,$2}= $3;
186 foreach my $src (<$SELF/data/news/*.rrd>) {
188 $site =~ s,\.rrd$,, or next;
190 $site =~ s,_(in|out)$,,;
192 $site =~ s/^([-.0-9a-z]+)_//;
193 my $us= $1; # all very well but we ignore it
194 my $newsite= $news_name_map{$site,$inout};
195 $site= $newsite if defined $newsite;
196 next if $site eq '-';
197 push @{ $news_sources{$site}{$inout} }, $src;
202 foreach my $site (keys %news_sources) {
206 s/^[^. ]*\b(?:chiark|greenend|news|nntp|peer|feed|in|out)\b[^.]*\.//;
209 foreach my $inout (keys %{ $news_sources{$site} }) {
210 push @news_graphs, [ "$sk $inout", $site, $inout ];
214 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
215 my ($sortkey, $site, $inout)= @$siteinfo;
216 my @sources= @{ $news_sources{$site}{$inout} };
218 my @vals= $inout eq 'out'
219 ? qw(missing deferred unwanted accepted rejected body_missing)
220 : qw(accepted refused rejected duplicate
221 accepted_size duplicate_size);
223 foreach my $val (@vals) {
224 my $def= "CDEF:$val=0";
225 foreach my $si (0..$#sources) {
226 my $src= $sources[$si];
227 my $tvar= "${val}_${si}";
228 push @defs, "DEF:$tvar=$src:$val:AVERAGE";
229 $def .= ",$tvar,ADDNAN";
232 if ($val =~ m/_size$/) {
233 push @defs, "CDEF:kb_$`=$val,1024,/";
236 graph_of_group("News", $site, $inout,
239 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
243 "AREA:accepted#00f:ok",
244 "AREA:body_missing#ff0:miss:STACK",
245 "AREA:rejected#f00:rej:STACK",
246 "AREA:unwanted#aaa:unw:STACK",
247 "AREA:deferred#ddd:defer:STACK",
251 "AREA:accepted#00f:ok:STACK",
252 "AREA:rejected#f00:rej:STACK",
253 "AREA:duplicate#000:dupe:STACK",
254 "AREA:refused#aaa:unw:STACK",
255 "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
256 "LINE:kb_duplicate#ff0:kb dupe",
257 "LINE:kb_accepted_smooth#008:~kb",
263 foreach my $physdiskrrd (<$R/disk-*/disk_octets.rrd>) {
264 $physdiskrrd =~ s,octets\.rrd$,, or die;
265 $physdiskrrd =~ m,-([^/]+)/disk_$, or die;
266 my $physdev= "/dev/$1";
267 if (!stat $physdev) {
268 die "$physdev $!" unless $!==&ENOENT;
271 die "$physdev ?" unless S_ISBLK((stat _)[2]);
272 $disk_rdev2rrd{(stat _)[6]}= $physdiskrrd;
278 my ($vg, $label, $factor, $rcolour, $wcolour) = @_;
281 $varname =~ s/[^0-9a-zA-Y]/ sprintf "Z%02x", ord($&) /ge;
285 Colour => { 'read' => $rcolour, 'write' => $wcolour },
288 foreach my $bo (qw(octets ops)) {
289 foreach my $rw (qw(read write)) {
290 $vginfo->{VarDefs}{$bo}{$rw}= [];
291 $vginfo->{Sumdef}{$bo}{$rw}= '0';
295 foreach my $lvpath (</dev/$vg/*>) {
296 my $lv= $lvpath; $lv =~ s,.*/,,;
298 die "$lvpath $!" unless $!==&ENOENT;
301 die "$lvpath ?" unless S_ISBLK((stat _)[2]);
302 my $rrd= $disk_rdev2rrd{(stat _)[6]};
303 next unless defined $rrd;
305 my $lvinfo= { Name => $lv };
306 push @{ $vginfo->{Lvs} }, $lvinfo;
308 foreach my $bo (qw(octets ops)) {
309 $lvinfo->{Defs}{$bo}=
311 (map { ("DEF:$_=${rrd}${bo}.rrd:$_:AVERAGE") } qw(read write)),
312 "CDEF:mwrite=0,write,-",
313 "AREA:read#00f:read",
314 "AREA:mwrite#f00:write"
317 foreach my $rw (qw(read write)) {
319 my $tvar= "lv_${rw}_${bo}_${varname}_${ix}";
320 push @{ $vginfo->{VarDefs}{$bo}{$rw} },
321 "DEF:$tvar=${rrd}${bo}.rrd:$rw:AVERAGE";
322 $vginfo->{Sumdef}{$bo}{$rw} .= ",$tvar,+";
326 foreach my $bo (qw(octets ops)) {
327 foreach my $rw (qw(read write)) {
329 push @$defs, @{ $vginfo->{VarDefs}{$bo}{$rw} };
330 push @$defs, "CDEF:${rw}_vg_${varname}=".
331 $vginfo->{Sumdef}{$bo}{$rw}.
332 sprintf(",%f,*", $rw eq 'write' ? -$factor : $factor);
333 $vginfo->{Defs}{$bo}{$rw}= $defs;
336 push @disk_vgs, $vginfo;
339 lvgraphs('vg-main', 'main', 1, qw(00f f00));
340 lvgraphs('vg-chiark-stripe', 'stripe', 0.5, qw(008 800));
342 foreach my $bo (qw(octets ops)) {
344 foreach my $rw (qw(read write)) {
346 foreach my $vginfo (@disk_vgs) {
347 push @a, @{ $vginfo->{Defs}{$bo}{$rw} };
348 push @a, "AREA:${rw}_vg_$vginfo->{Varname}#".
349 $vginfo->{Colour}{$rw}.
350 ":$vginfo->{Name} ".substr($rw,0,1).
355 graph_of_group('IO', 'IO', $bo, { Units => '[/s]' }, \@a);
358 foreach my $vginfo (@disk_vgs) {
359 foreach my $bo (qw(octets ops)) {
360 foreach my $lv (@{ $vginfo->{Lvs} }) {
361 graph_of_group('IO', "$vginfo->{Name} $lv->{Name}",
362 $bo, { Units => '[/s]' }, $lv->{Defs}{$bo});
367 push @{ $section_groups{General} }, {
370 UrlParams => "section=IO&sloth=SLOTH"
374 #---------- right, that was the initialisation ----------
376 our $self= url(-relative=>1);
378 if (param('debug')) {
379 print "Content-Type: text/plain\n\n";
382 sub source_tarball ($$) {
383 my ($spitoutfn) = @_;
386 if (path_info() =~ m/\.tar\.gz$/) {
387 print "Content-Type: application/octet-stream\n\n";
389 exec '/bin/sh','-c','
392 git-ls-files -z --others --exclude-from=.gitignore;
393 if test -d .git; then find .git -print0; fi
395 cpio -Hustar -o --quiet -0 -R 1000:1000 || \
396 cpio -Hustar -o --quiet -0
408 my $var= $nav->{Variable};
409 $$var= param($nav->{Param});
410 $$var= $nav->{Default} if !defined $$var;
411 die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
412 push @navsettings, $nav;
420 Variable => \$section,
421 Default => $sections[0],
422 Values => [@sections],
423 Show => sub { return $_[0]; }
427 sub num_param ($$$$) {
428 my ($param,$def,$min,$max) = @_;
429 my $v= param($param);
430 return $def if !defined $v;
431 $v =~ m/^([1-9]\d{0,8})$/ or die;
433 die unless $v >= $min && $v <= $max;
437 our $group= param('graph');
442 our $elem= param('elem');
444 my $g= $graphs{$section,$group,$elem};
447 my $width= num_param('w',$defwidth,100,1600);
448 my $height= num_param('h',$defheight,100,1600);
450 my $sloth= param('sloth');
451 die unless defined $sloth;
452 $sloth =~ m/^(\d+)$/ or die;
454 my $end= $g->{TimeRanges}[$sloth];
455 die unless defined $end;
457 my @args= @{ $g->{Args} };
458 s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
459 unshift @args, qw(--end now --start), "end-${end}s";
462 if (length $elem) { $title.= " $elem"; }
464 $title .= " $g->{Units}" if $g->{Units};
465 unshift @args, '-t', $title, '-w',$width, '-h',$height;
466 unshift @args, qw(-a PNG --full-size-mode);
468 my $cacheid= "$section!$group!$elem!$sloth!$width!$height!";
469 $cacheid .= unpack "H*", MD5->hash(join '\0', @args);
470 my $cachepath= "cache/$cacheid.png";
472 if (param('debug')) {
473 print((join "\n",@args),"\n"); exit 0;
476 #print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
477 exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
479 rrdtool graph "$p" --lazy "$@" >/dev/null
480 printf "Content-Type: image/png\n\n"
488 print header(), start_html($title);
490 foreach my $nav (@navsettings) {
492 print $nav->{Desc}, ": ";
494 my $current= $nav->{Variable}; $current= $$current;
495 foreach my $couldbe (@{ $nav->{Values} }) {
497 my $show= $nav->{Show}($couldbe);
498 if ($couldbe eq $current) {
499 print "<b>$show</b>";
503 foreach my $nav2 (@navsettings) {
504 my $current2= $nav2->{Variable}; $current2= $$current2;
505 $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
506 next if $current2 eq $nav2->{Default};
507 $u .= $delim2; $u .= "$nav2->{Param}=$current2";
510 print a({href=>$u}, $show);
514 $outerdelim= "<br>\n";
521 our $detail= param('detail');
522 if (defined $detail) {
523 my $elems= $group_elems{$section,$detail};
525 start_page("$detail - $section - graphs");
526 foreach my $tsloth (0..5) {
527 foreach my $elem (@$elems) {
528 my $g= $graphs{$section,$detail,$elem};
530 next if $tsloth >= @{ $g->{TimeRanges} };
531 my $imgurl= "$self?graph=$detail§ion=$section".
532 "&sloth=$tsloth&elem=$elem";
533 print a({href=>"$imgurl&w=780&h=800"},
534 img({src=>$imgurl, alt=>'',
535 width=>$defwidth, height=>$defheight}));
545 Desc => 'Time interval',
552 return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
556 if (param('debug')) {
558 print Dumper(\%graphs);
562 start_page("$section - graphs");
564 foreach my $group (@{ $section_groups{$section} }) {
565 my $ref_group= $group;
566 my $ref_section= $section;
567 my $ref_urlparams= "detail=$group§ion=$section";
569 $ref_group= $group->{Group};
570 $ref_section= $group->{Section};
571 $ref_urlparams= $group->{UrlParams};
572 $ref_urlparams =~ s/\bSLOTH\b/$sloth/;
574 print a({href=>"$self?$ref_urlparams"});
575 my $imgurl= "$self?graph=$ref_group§ion=$ref_section";
576 print "<span style=\"white-space:nowrap\">";
577 my $elems= $group_elems{$ref_section,$ref_group};
578 foreach my $elem (@$elems) {
579 my $g= $graphs{$ref_section,$ref_group,$elem};
580 print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
581 alt=>'', width=>$defwidth, height=>$defheight});
588 'http://www.chiark.greenend.org.uk/ucgi/~ijackson/git/rrd-graphs/';
590 <hr>Generated by "<a href="$homeurl">rrd-graphs</a>".
591 Copyright 2010,2012 Ian Jackson. There is <strong>NO WARRANTY</strong>.
592 Available under the Affero Public General Licence, version 3 or any
593 later version. You may download the
594 <a href="$self/rrd-graphs.tar.gz">source code</a>
595 for the version currently running here.