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, $title) = @_;
50 $basis->{Args}= $args;
51 $basis->{Slower}= 0 unless exists $basis->{Slower};
52 $basis->{TimeRanges} ||= \@timeranges;
53 if (!defined $title) {
55 if (length $elem) { $title.= " $elem"; }
57 $basis->{Title} = $title;
58 $graphs{$section,$group,$elem}= $basis;
59 if (!exists $group_elems{$section,$group}) {
61 if (!exists $section_groups{$section}) {
63 push @sections, $section;
65 push @{ $section_groups{$section} }, $group;
67 push @{ $group_elems{$section,$group} }, $elem;
71 my ($section, $gname, $basis, $args) = @_;
72 graph_of_group($section, $gname,'', $basis, $args);
75 graph('General', 'Load and processes', { },
77 "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
78 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
79 qw(blocked running stopped paging sleeping zombies)),
80 "AREA:running#88f:running:STACK",
81 "AREA:blocked#8f8:disk wait:STACK",
82 "AREA:paging#f88:paging:STACK",
86 graph('General', 'Processes', { },
88 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
89 qw(blocked running stopped paging sleeping zombies)),
90 "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
91 "AREA:sleeping#ccc:sleeping:STACK",
92 "AREA:stopped#00f:stopped:STACK",
93 "AREA:zombies#ff0:zombie:STACK",
94 "AREA:busy#000:busy:STACK",
97 graph('General', 'CPU', { Units => '[%]' },
101 (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
103 "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
104 } qw(idle interrupt nice softirq steal system user wait)),
105 "CDEF:allintr=softirq,steal,+,interrupt,+",
106 "AREA:allintr#ff0:interrupt:STACK",
107 "AREA:system#88f:system:STACK",
108 "AREA:user#00f:user:STACK",
109 "AREA:nice#ccc:nice:STACK",
110 "AREA:wait#f00:wait:STACK",
113 graph('General', 'Memory', { },
115 (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
116 qw(free used cached)),
117 (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
118 qw(buffered free used cached)),
119 "CDEF:c_swap_used=0,swap_used,-",
120 "CDEF:c_swap_cached=0,swap_cached,-",
121 "CDEF:c_swap_free=0,swap_free,-",
122 "AREA:c_swap_used#000:used swap",
123 "AREA:c_swap_cached#888:\"cached\" swap:STACK",
124 # "AREA:c_swap_free#88f:free swap:STACK",
125 "AREA:mem_used#ff0:used memory",
126 "AREA:mem_buffered#00f:page cache:STACK",
127 "AREA:mem_cached#008:buffer cache:STACK",
128 "AREA:mem_free#ccc:unused memory:STACK",
131 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
134 ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
135 "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
136 "CDEF:mrx_$_=0,rx_$_,-")
137 } qw(octets packets errors)),
139 ("CDEF:${_}_kb=${_}_octets,1024,/",
140 "CDEF:${_}_errsx=${_}_errors,1000,*")
142 "AREA:tx_kb#080:kby",
143 "LINE:tx_packets#0f0:pkts",
144 "LINE:tx_errsx#000:errs",
145 "AREA:mrx_kb#008:kby",
146 "LINE:mrx_packets#00f:pkts",
147 "LINE:mrx_errsx#444:errs",
150 graph('General', 'Users', { },
152 "DEF:users=$R/users/users.rrd:users:AVERAGE",
153 "LINE:users#008:users"
156 foreach my $src (<$R/df/df-*.rrd>) {
158 $vol =~ s,\.rrd$,, or next;
161 graph('Disk space', $vol, {
166 (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
167 "AREA:used#000:used:STACK",
168 "AREA:free#88f:free:STACK",
174 if (!open NM, '<', "$SELF/data/news/name-map") {
175 die unless $!==&ENOENT;
179 next unless m/^[^\#]/;
180 m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
182 $news_name_map{$1,$_}= $3 foreach qw(in out);
184 $news_name_map{$1,$2}= $3;
191 foreach my $src (<$SELF/data/news/*.rrd>) {
193 $site =~ s,\.rrd$,, or next;
195 $site =~ s,_(in|out)$,,;
197 $site =~ s/^([-.0-9a-z]+)_//;
198 my $us= $1; # all very well but we ignore it
199 my $newsite= $news_name_map{$site,$inout};
200 $site= $newsite if defined $newsite;
201 next if $site eq '-';
202 push @{ $news_sources{$site}{$inout} }, $src;
207 foreach my $site (keys %news_sources) {
209 $sk =~ s/^[.0-9]+$/~$&/;
212 s/^[^. ]*\b(?:chiark|greenend|news|newsfeed|nntp|peer|feeds?|feeder|in|out)\b[^.]*\.//;
215 print STDERR "$site => $sk\n";
216 foreach my $inout (keys %{ $news_sources{$site} }) {
218 $skio =~ s/ / [$inout]/;
219 push @news_graphs, [ $skio, $site, $inout ];
223 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
224 my ($sortkey, $site, $inout)= @$siteinfo;
225 my @sources= @{ $news_sources{$site}{$inout} };
227 my @vals= $inout eq 'out'
228 ? qw(missing deferred unwanted accepted rejected body_missing)
229 : qw(accepted refused rejected duplicate
230 accepted_size duplicate_size);
232 foreach my $val (@vals) {
233 my $def= "CDEF:$val=0";
234 foreach my $si (0..$#sources) {
235 my $src= $sources[$si];
236 my $tvar= "${val}_${si}";
237 push @defs, "DEF:$tvar=$src:$val:AVERAGE";
238 $def .= ",$tvar,ADDNAN";
241 if ($val =~ m/_size$/) {
242 push @defs, "CDEF:kb_$`=$val,1024,/";
245 my $group = $sortkey;
247 $group = $site unless length $group;
248 graph_of_group("News", $group, "$site $inout",
251 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
255 "AREA:accepted#00f:ok",
256 "AREA:body_missing#ff0:miss:STACK",
257 "AREA:rejected#f00:rej:STACK",
258 "AREA:unwanted#aaa:unw:STACK",
259 "AREA:deferred#ddd:defer:STACK",
263 "AREA:accepted#00f:ok:STACK",
264 "AREA:rejected#f00:rej:STACK",
265 "AREA:duplicate#000:dupe:STACK",
266 "AREA:refused#aaa:unw:STACK",
267 "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
268 "LINE:kb_duplicate#ff0:kb dupe",
269 "LINE:kb_accepted_smooth#008:~kb",
276 foreach my $physdiskrrd (<$R/disk-*/disk_octets.rrd>) {
277 $physdiskrrd =~ s,octets\.rrd$,, or die;
278 $physdiskrrd =~ m,-([^/]+)/disk_$, or die;
279 my $physdev= "/dev/$1";
280 if (!stat $physdev) {
281 die "$physdev $!" unless $!==&ENOENT;
284 die "$physdev ?" unless S_ISBLK((stat _)[2]);
285 $disk_rdev2rrd{(stat _)[6]}= $physdiskrrd;
291 my ($vg, $label, $factor, $rcolour, $wcolour) = @_;
294 $varname =~ s/[^0-9a-zA-Y]/ sprintf "Z%02x", ord($&) /ge;
298 Colour => { 'read' => $rcolour, 'write' => $wcolour },
301 foreach my $bo (qw(octets ops)) {
302 foreach my $rw (qw(read write)) {
303 $vginfo->{VarDefs}{$bo}{$rw}= [];
304 $vginfo->{Sumdef}{$bo}{$rw}= '0';
308 foreach my $lvpath (</dev/$vg/*>) {
309 my $lv= $lvpath; $lv =~ s,.*/,,;
311 die "$lvpath $!" unless $!==&ENOENT;
314 die "$lvpath ?" unless S_ISBLK((stat _)[2]);
315 my $rrd= $disk_rdev2rrd{(stat _)[6]};
316 next unless defined $rrd;
318 my $lvinfo= { Name => $lv };
319 push @{ $vginfo->{Lvs} }, $lvinfo;
321 foreach my $bo (qw(octets ops)) {
322 $lvinfo->{Defs}{$bo}=
324 (map { ("DEF:$_=${rrd}${bo}.rrd:$_:AVERAGE") } qw(read write)),
325 "CDEF:mwrite=0,write,-",
326 "AREA:read#00f:read",
327 "AREA:mwrite#f00:write"
330 foreach my $rw (qw(read write)) {
332 my $tvar= "lv_${rw}_${bo}_${varname}_${ix}";
333 push @{ $vginfo->{VarDefs}{$bo}{$rw} },
334 "DEF:$tvar=${rrd}${bo}.rrd:$rw:AVERAGE";
335 $vginfo->{Sumdef}{$bo}{$rw} .= ",$tvar,+";
339 foreach my $bo (qw(octets ops)) {
340 foreach my $rw (qw(read write)) {
342 push @$defs, @{ $vginfo->{VarDefs}{$bo}{$rw} };
343 push @$defs, "CDEF:${rw}_vg_${varname}=".
344 $vginfo->{Sumdef}{$bo}{$rw}.
345 sprintf(",%f,*", $rw eq 'write' ? -$factor : $factor);
346 $vginfo->{Defs}{$bo}{$rw}= $defs;
349 push @disk_vgs, $vginfo;
352 lvgraphs('vg-main', 'main', 1, qw(00f f00));
353 lvgraphs('vg-chiark-stripe', 'stripe', 0.5, qw(008 800));
355 foreach my $bo (qw(octets ops)) {
357 foreach my $rw (qw(read write)) {
359 foreach my $vginfo (@disk_vgs) {
360 push @a, @{ $vginfo->{Defs}{$bo}{$rw} };
361 push @a, "AREA:${rw}_vg_$vginfo->{Varname}#".
362 $vginfo->{Colour}{$rw}.
363 ":$vginfo->{Name} ".substr($rw,0,1).
368 graph_of_group('IO', 'IO', $bo, { Units => '[/s]' }, \@a);
371 foreach my $vginfo (@disk_vgs) {
372 foreach my $bo (qw(octets ops)) {
373 foreach my $lv (@{ $vginfo->{Lvs} }) {
374 graph_of_group('IO', "$vginfo->{Name} $lv->{Name}",
375 $bo, { Units => '[/s]' }, $lv->{Defs}{$bo});
380 push @{ $section_groups{General} }, {
383 UrlParams => "section=IO&sloth=SLOTH"
387 #---------- right, that was the initialisation ----------
389 our $self= url(-relative=>1);
391 if (param('debug')) {
392 print "Content-Type: text/plain\n\n";
395 sub source_tarball ($$) {
396 my ($spitoutfn) = @_;
399 if (path_info() =~ m/\.tar\.gz$/) {
400 print "Content-Type: application/octet-stream\n\n";
402 exec '/bin/sh','-c','
405 git-ls-files -z --others --exclude-from=.gitignore;
406 if test -d .git; then find .git -print0; fi
408 cpio -Hustar -o --quiet -0 -R 1000:1000 || \
409 cpio -Hustar -o --quiet -0
421 my $var= $nav->{Variable};
422 $$var= param($nav->{Param});
423 $$var= $nav->{Default} if !defined $$var;
424 die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
425 push @navsettings, $nav;
433 Variable => \$section,
434 Default => $sections[0],
435 Values => [@sections],
436 Show => sub { return $_[0]; }
440 sub num_param ($$$$) {
441 my ($param,$def,$min,$max) = @_;
442 my $v= param($param);
443 return $def if !defined $v;
444 $v =~ m/^([1-9]\d{0,8})$/ or die;
446 die unless $v >= $min && $v <= $max;
450 our $group= param('graph');
455 our $elem= param('elem');
457 my $g= $graphs{$section,$group,$elem};
460 my $width= num_param('w',$defwidth,100,1600);
461 my $height= num_param('h',$defheight,100,1600);
463 my $sloth= param('sloth');
464 die unless defined $sloth;
465 $sloth =~ m/^(\d+)$/ or die;
467 my $end= $g->{TimeRanges}[$sloth];
468 die unless defined $end;
470 my @args= @{ $g->{Args} };
471 s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
472 unshift @args, qw(--end now --start), "end-${end}s";
474 my $title = $g->{Title};
475 $title .= " $g->{Units}" if $g->{Units};
476 unshift @args, '-t', $title, '-w',$width, '-h',$height;
477 unshift @args, qw(-a PNG --full-size-mode);
479 my $cacheid= "$section!$group!$elem!$sloth!$width!$height!";
480 $cacheid .= unpack "H*", MD5->hash(join '\0', @args);
481 my $cachepath= "cache/$cacheid.png";
483 if (param('debug')) {
484 print((join "\n",@args),"\n"); exit 0;
487 #print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
488 exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
490 rrdtool graph "$p" --lazy "$@" >/dev/null
491 printf "Content-Type: image/png\n\n"
499 print header(), start_html($title);
501 foreach my $nav (@navsettings) {
503 print $nav->{Desc}, ": ";
505 my $current= $nav->{Variable}; $current= $$current;
506 foreach my $couldbe (@{ $nav->{Values} }) {
508 my $show= $nav->{Show}($couldbe);
509 if ($couldbe eq $current) {
510 print "<b>$show</b>";
514 foreach my $nav2 (@navsettings) {
515 my $current2= $nav2->{Variable}; $current2= $$current2;
516 $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
517 next if $current2 eq $nav2->{Default};
518 $u .= $delim2; $u .= "$nav2->{Param}=$current2";
521 print a({href=>$u}, $show);
525 $outerdelim= "<br>\n";
532 our $detail= param('detail');
533 if (defined $detail) {
534 my $elems= $group_elems{$section,$detail};
536 start_page("$detail - $section - graphs");
537 foreach my $tsloth (0..5) {
538 foreach my $elem (@$elems) {
539 my $g= $graphs{$section,$detail,$elem};
541 next if $tsloth >= @{ $g->{TimeRanges} };
542 my $imgurl= "$self?graph=$detail§ion=$section".
543 "&sloth=$tsloth&elem=$elem";
544 print a({href=>"$imgurl&w=780&h=800"},
545 img({src=>$imgurl, alt=>'',
546 width=>$defwidth, height=>$defheight}));
556 Desc => 'Time interval',
563 return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
567 if (param('debug')) {
569 print Dumper(\%graphs);
573 start_page("$section - graphs");
575 foreach my $group (@{ $section_groups{$section} }) {
576 my $ref_group= $group;
577 my $ref_section= $section;
578 my $ref_urlparams= "detail=$group§ion=$section";
580 $ref_group= $group->{Group};
581 $ref_section= $group->{Section};
582 $ref_urlparams= $group->{UrlParams};
583 $ref_urlparams =~ s/\bSLOTH\b/$sloth/;
585 print a({href=>"$self?$ref_urlparams"});
586 my $imgurl= "$self?graph=$ref_group§ion=$ref_section";
587 print "<span style=\"white-space:nowrap\">";
588 my $elems= $group_elems{$ref_section,$ref_group};
589 foreach my $elem (@$elems) {
590 my $g= $graphs{$ref_section,$ref_group,$elem};
591 print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
592 alt=>'', width=>$defwidth, height=>$defheight});
599 'http://www.chiark.greenend.org.uk/ucgi/~ijackson/git/rrd-graphs/';
601 <hr>Generated by "<a href="$homeurl">rrd-graphs</a>".
602 Copyright 2010,2012 Ian Jackson. There is <strong>NO WARRANTY</strong>.
603 Available under the Affero Public General Licence, version 3 or any
604 later version. You may download the
605 <a href="$self/rrd-graphs.tar.gz">source code</a>
606 for the version currently running here.