1 #!/usr/bin/speedy -w -- -t100 -M1
4 use CGI::SpeedyCGI qw/:standard -no_xhtml/;
5 use CGI qw/:standard -no_xhtml/;
10 print(header(-status=>500),
18 our (@sections, %section_groups, %group_elems, %graphs);
20 #---------- initialisation code, run once - graphs setup ----------
24 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
25 our $SELF= '/home/ijackson/things/rrd-graphs';
27 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
29 sub graph_of_group ($$$$$) {
30 my ($section, $group, $elem, $basis, $args) = @_;
31 $basis->{Args}= $args;
32 $basis->{Slower}= 0 unless exists $basis->{Slower};
33 $basis->{TimeRanges} ||= \@timeranges;
34 $graphs{$section,$group,$elem}= $basis;
35 if (!exists $group_elems{$section,$group}) {
37 if (!exists $section_groups{$section}) {
39 push @sections, $section;
41 push @{ $section_groups{$section} }, $group;
43 push @{ $group_elems{$section,$group} }, $elem;
47 my ($section, $gname, $basis, $args) = @_;
48 graph_of_group($section, $gname,'', $basis, $args);
51 graph('General', 'Load and processes', { },
53 "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
54 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
55 qw(blocked running stopped paging sleeping zombies)),
56 "AREA:running#88f:running:STACK",
57 "AREA:blocked#8f8:disk wait:STACK",
58 "AREA:paging#f88:paging:STACK",
62 graph('General', 'Processes', { },
64 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
65 qw(blocked running stopped paging sleeping zombies)),
66 "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
67 "AREA:sleeping#ccc:sleeping:STACK",
68 "AREA:stopped#00f:stopped:STACK",
69 "AREA:zombies#ff0:zombie:STACK",
70 "AREA:busy#000:busy:STACK",
73 graph('General', 'CPU', { Units => '[%]' },
77 (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
79 "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
80 } qw(idle interrupt nice softirq steal system user wait)),
81 "CDEF:allintr=softirq,steal,+,interrupt,+",
82 "AREA:allintr#ff0:interrupt:STACK",
83 "AREA:system#88f:system:STACK",
84 "AREA:user#00f:user:STACK",
85 "AREA:nice#ccc:nice:STACK",
86 "AREA:wait#f00:wait:STACK",
89 graph('General', 'Memory', { },
91 (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
92 qw(free used cached)),
93 (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
94 qw(buffered free used cached)),
95 "CDEF:c_swap_used=0,swap_used,-",
96 "CDEF:c_swap_cached=0,swap_cached,-",
97 "CDEF:c_swap_free=0,swap_free,-",
98 "AREA:c_swap_used#000:used swap",
99 "AREA:c_swap_cached#888:\"cached\" swap:STACK",
100 # "AREA:c_swap_free#88f:free swap:STACK",
101 "AREA:mem_used#ff0:used memory",
102 "AREA:mem_buffered#00f:page cache:STACK",
103 "AREA:mem_cached#008:buffer cache:STACK",
104 "AREA:mem_free#ccc:unused memory:STACK",
107 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
110 ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
111 "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
112 "CDEF:mrx_$_=0,rx_$_,-")
113 } qw(octets packets errors)),
115 ("CDEF:${_}_kb=${_}_octets,1024,/",
116 "CDEF:${_}_errsx=${_}_errors,1000,*")
118 "AREA:tx_kb#080:kby",
119 "LINE:tx_packets#0f0:pkts",
120 "LINE:tx_errsx#000:errs",
121 "AREA:mrx_kb#008:kby",
122 "LINE:mrx_packets#00f:pkts",
123 "LINE:mrx_errsx#444:errs",
126 graph('General', 'Users', { },
128 "DEF:users=$R/users/users.rrd:users:AVERAGE",
129 "LINE:users#008:users"
132 foreach my $src (<$R/df/df-*.rrd>) {
134 $vol =~ s,\.rrd$,, or next;
137 graph('Disk space', $vol, {
142 (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
143 "AREA:used#000:used:STACK",
144 "AREA:free#88f:free:STACK",
150 if (!open NM, '<', "$SELF/data/news/name-map") {
151 die unless $!==&ENOENT;
155 next unless m/^[^\#]/;
156 m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
158 $news_name_map{$1,$_}= $3 foreach qw(in out);
160 $news_name_map{$1,$2}= $3;
167 foreach my $src (<$SELF/data/news/*.rrd>) {
169 $site =~ s,\.rrd$,, or next;
171 $site =~ s,_(in|out)$,,;
173 $site =~ s/^([-.0-9a-z]+)_//;
174 my $us= $1; # all very well but we ignore it
175 my $newsite= $news_name_map{$site,$inout};
176 $site= $newsite if defined $newsite;
177 next if $site eq '-';
178 push @{ $news_sources{$site}{$inout} }, $src;
183 foreach my $site (keys %news_sources) {
187 s/^[^.]*(?:chiark|greenend|news|nntp|peer|feed|in|out)[^.]*\.//;
190 foreach my $inout (keys %{ $news_sources{$site} }) {
191 push @news_graphs, [ "$sk $inout", $site, $inout ];
195 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
196 my ($sortkey, $site, $inout)= @$siteinfo;
197 my @sources= @{ $news_sources{$site}{$inout} };
199 my @vals= $inout eq 'out'
200 ? qw(missing deferred unwanted accepted rejected body_missing)
201 : qw(accepted refused rejected duplicate
202 accepted_size duplicate_size);
204 foreach my $val (@vals) {
205 my $def= "CDEF:$val=0";
206 foreach my $si (0..$#sources) {
207 my $src= $sources[$si];
208 my $tvar= "${val}_${si}";
209 push @defs, "DEF:$tvar=$src:$val:AVERAGE";
210 $def .= ",$tvar,ADDNAN";
213 if ($val =~ m/_size$/) {
214 push @defs, "CDEF:kb_$`=$val,1024,/";
217 graph_of_group("News", $site, $inout,
220 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
224 "AREA:accepted#00f:ok",
225 "AREA:body_missing#ff0:miss:STACK",
226 "AREA:rejected#f00:rej:STACK",
227 "AREA:unwanted#aaa:unw:STACK",
228 "AREA:deferred#ddd:defer:STACK",
232 "AREA:accepted#00f:ok:STACK",
233 "AREA:rejected#f00:rej:STACK",
234 "AREA:duplicate#000:dupe:STACK",
235 "AREA:refused#aaa:unw:STACK",
236 "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
237 "LINE:kb_duplicate#ff0:kb dupe",
238 "LINE:kb_accepted_smooth#008:~kb",
244 foreach my $physdiskrrd (<$R/disk-*/disk_octets.rrd>) {
245 $physdiskrrd =~ s,octets\.rrd$,, or die;
246 $physdiskrrd =~ m,-([^/]+)/disk_$, or die;
247 my $physdev= "/dev/$1";
248 if (!stat $physdev) {
249 die "$physdev $!" unless $!==&ENOENT;
252 die "$physdev ?" unless S_ISBLK((stat _)[2]);
253 $disk_rdev2rrd{(stat _)[6]}= $physdiskrrd;
259 my ($vg, $label, $factor, $rcolour, $wcolour) = @_;
262 $varname =~ s/[^0-9a-zA-Y]/ sprintf "Z%02x", ord($&) /ge;
266 Colour => { 'read' => $rcolour, 'write' => $wcolour },
269 foreach my $bo (qw(octets ops)) {
270 foreach my $rw (qw(read write)) {
271 $vginfo->{VarDefs}{$bo}{$rw}= [];
272 $vginfo->{Sumdef}{$bo}{$rw}= '0';
276 foreach my $lvpath (</dev/$vg/*>) {
277 my $lv= $lvpath; $lv =~ s,.*/,,;
279 die "$lvpath $!" unless $!==&ENOENT;
282 die "$lvpath ?" unless S_ISBLK((stat _)[2]);
283 my $rrd= $disk_rdev2rrd{(stat _)[6]};
284 next unless defined $rrd;
286 my $lvinfo= { Name => $lv };
287 push @{ $vginfo->{Lvs} }, $lvinfo;
289 foreach my $bo (qw(octets ops)) {
290 $lvinfo->{Defs}{$bo}=
292 (map { ("DEF:$_=${rrd}${bo}.rrd:$_:AVERAGE") } qw(read write)),
293 "CDEF:mwrite=0,write,-",
294 "AREA:read#00f:read",
295 "AREA:mwrite#f00:write"
298 foreach my $rw (qw(read write)) {
300 my $tvar= "lv_${rw}_${bo}_${varname}_${ix}";
301 push @{ $vginfo->{VarDefs}{$bo}{$rw} },
302 "DEF:$tvar=${rrd}${bo}.rrd:$rw:AVERAGE";
303 $vginfo->{Sumdef}{$bo}{$rw} .= ",$tvar,+";
307 foreach my $bo (qw(octets ops)) {
308 foreach my $rw (qw(read write)) {
310 push @$defs, @{ $vginfo->{VarDefs}{$bo}{$rw} };
311 push @$defs, "CDEF:${rw}_vg_${varname}=".
312 $vginfo->{Sumdef}{$bo}{$rw}.
313 sprintf(",%f,*", $rw eq 'write' ? -$factor : $factor);
314 $vginfo->{Defs}{$bo}{$rw}= $defs;
317 push @disk_vgs, $vginfo;
320 lvgraphs('vg-main', 'main', 1, qw(00f f00));
321 lvgraphs('vg-chiark-stripe', 'stripe', 0.5, qw(008 800));
323 foreach my $bo (qw(octets ops)) {
325 foreach my $rw (qw(read write)) {
327 foreach my $vginfo (@disk_vgs) {
328 push @a, @{ $vginfo->{Defs}{$bo}{$rw} };
329 push @a, "AREA:${rw}_vg_$vginfo->{Varname}#".
330 $vginfo->{Colour}{$rw}.
331 ":$vginfo->{Name} ".substr($rw,0,1).
336 graph_of_group('IO', 'IO', $bo, { Units => '[/s]' }, \@a);
339 foreach my $vginfo (@disk_vgs) {
340 foreach my $bo (qw(octets ops)) {
341 foreach my $lv (@{ $vginfo->{Lvs} }) {
342 graph_of_group('IO', "$vginfo->{Name} $lv->{Name}",
343 $bo, { Units => '[/s]' }, $lv->{Defs}{$bo});
348 push @{ $section_groups{General} }, {
351 UrlParams => "section=IO&sloth=SLOTH"
355 #---------- right, that was the initialisation ----------
357 our $self= url(-relative=>1);
359 if (param('debug')) {
360 print "Content-Type: text/plain\n\n";
369 my $var= $nav->{Variable};
370 $$var= param($nav->{Param});
371 $$var= $nav->{Default} if !defined $$var;
372 die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
373 push @navsettings, $nav;
381 Variable => \$section,
382 Default => $sections[0],
383 Values => [@sections],
384 Show => sub { return $_[0]; }
388 sub num_param ($$$$) {
389 my ($param,$def,$min,$max) = @_;
390 my $v= param($param);
391 return $def if !defined $v;
392 $v =~ m/^([1-9]\d{0,8})$/ or die;
394 die unless $v >= $min && $v <= $max;
398 our $group= param('graph');
400 our $elem= param('elem');
402 my $g= $graphs{$section,$group,$elem};
405 my $width= num_param('w',370,100,1600);
406 my $height= num_param('h',200,100,1600);
408 my $sloth= param('sloth');
409 die unless defined $sloth;
410 $sloth =~ m/^(\d+)$/ or die;
412 my $end= $g->{TimeRanges}[$sloth];
413 die unless defined $end;
415 my @args= @{ $g->{Args} };
416 s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
417 unshift @args, qw(--end now --start), "end-${end}s";
420 if (length $elem) { $title.= " $elem"; }
422 $title .= " $g->{Units}" if $g->{Units};
423 unshift @args, '-t', $title, '-w',$width, '-h',$height;
424 unshift @args, qw(-a PNG --full-size-mode);
426 my $cacheid= "$section!$group!$elem!$sloth!$width!$height!";
427 $cacheid .= unpack "H*", MD5->hash(join '\0', @args);
428 my $cachepath= "cache/$cacheid.png";
430 if (param('debug')) {
431 print((join "\n",@args),"\n"); exit 0;
434 #print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
435 exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
437 rrdtool graph "$p" --lazy "$@" >/dev/null
438 printf "Content-Type: image/png\n\n"
446 print header(), start_html($title);
448 foreach my $nav (@navsettings) {
450 print $nav->{Desc}, ": ";
452 my $current= $nav->{Variable}; $current= $$current;
453 foreach my $couldbe (@{ $nav->{Values} }) {
455 my $show= $nav->{Show}($couldbe);
456 if ($couldbe eq $current) {
457 print "<b>$show</b>";
461 foreach my $nav2 (@navsettings) {
462 my $current2= $nav2->{Variable}; $current2= $$current2;
463 $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
464 next if $current2 eq $nav2->{Default};
465 $u .= $delim2; $u .= "$nav2->{Param}=$current2";
468 print a({href=>$u}, $show);
472 $outerdelim= "<br>\n";
479 our $detail= param('detail');
480 if (defined $detail) {
481 my $elems= $group_elems{$section,$detail};
483 start_page("$detail - $section - graphs");
484 foreach my $tsloth (0..5) {
485 foreach my $elem (@$elems) {
486 my $g= $graphs{$section,$detail,$elem};
488 next if $tsloth >= @{ $g->{TimeRanges} };
489 my $imgurl= "$self?graph=$detail§ion=$section".
490 "&sloth=$tsloth&elem=$elem";
491 print a({href=>"$imgurl&w=780&h=800"},
492 img({src=>$imgurl, alt=>''}));
502 Desc => 'Time interval',
509 return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
513 if (param('debug')) {
515 print Dumper(\%graphs);
519 start_page("$section - graphs");
521 foreach my $group (@{ $section_groups{$section} }) {
522 my $ref_group= $group;
523 my $ref_section= $section;
524 my $ref_urlparams= "detail=$group§ion=$section";
526 $ref_group= $group->{Group};
527 $ref_section= $group->{Section};
528 $ref_urlparams= $group->{UrlParams};
529 $ref_urlparams =~ s/\bSLOTH\b/$sloth/;
531 print a({href=>"$self?$ref_urlparams"});
532 my $imgurl= "$self?graph=$ref_group§ion=$ref_section";
533 print "<span style=\"white-space:nowrap\">";
534 my $elems= $group_elems{$ref_section,$ref_group};
535 foreach my $elem (@$elems) {
536 my $g= $graphs{$ref_section,$ref_group,$elem};
537 print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),