1 #!/usr/bin/speedy -w -- -t100 -M1
4 use CGI::SpeedyCGI qw/:standard -no_xhtml/;
5 use CGI qw/:standard -no_xhtml/;
9 print(header(-status=>500),
17 our (@sections, %section_groups, %group_elems, %graphs);
19 #---------- initialisation code, run once - graphs setup ----------
23 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
24 our $SELF= '/home/ijackson/things/rrd-graphs';
26 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
28 sub graph_of_group ($$$$$) {
29 my ($section, $group, $elem, $basis, $args) = @_;
30 $basis->{Args}= $args;
31 $basis->{Slower}= 0 unless exists $basis->{Slower};
32 $basis->{TimeRanges} ||= \@timeranges;
33 $graphs{$section,$group,$elem}= $basis;
34 if (!exists $group_elems{$section,$group}) {
36 if (!exists $section_groups{$section}) {
38 push @sections, $section;
40 push @{ $section_groups{$section} }, $group;
42 push @{ $group_elems{$section,$group} }, $elem;
46 my ($section, $gname, $basis, $args) = @_;
47 graph_of_group($section, $gname,'', $basis, $args);
50 graph('General', 'Load and processes', { },
52 "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
53 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
54 qw(blocked running stopped paging sleeping zombies)),
55 "AREA:running#88f:running:STACK",
56 "AREA:blocked#8f8:disk wait:STACK",
57 "AREA:paging#f88:paging:STACK",
61 graph('General', 'Processes', { },
63 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
64 qw(blocked running stopped paging sleeping zombies)),
65 "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
66 "AREA:sleeping#ccc:sleeping:STACK",
67 "AREA:stopped#00f:stopped:STACK",
68 "AREA:zombies#ff0:zombie:STACK",
69 "AREA:busy#000:busy:STACK",
72 graph('General', 'CPU', { Units => '[%]' },
76 (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
78 "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
79 } qw(idle interrupt nice softirq steal system user wait)),
80 "CDEF:allintr=softirq,steal,+,interrupt,+",
81 "AREA:allintr#ff0:interrupt:STACK",
82 "AREA:system#88f:system:STACK",
83 "AREA:user#00f:user:STACK",
84 "AREA:nice#ccc:nice:STACK",
85 "AREA:wait#f00:wait:STACK",
88 graph('General', 'Memory', { },
90 (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
91 qw(free used cached)),
92 (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
93 qw(buffered free used cached)),
94 "CDEF:c_swap_used=0,swap_used,-",
95 "CDEF:c_swap_cached=0,swap_cached,-",
96 "CDEF:c_swap_free=0,swap_free,-",
97 "AREA:c_swap_used#000:used swap",
98 "AREA:c_swap_cached#888:\"cached\" swap:STACK",
99 # "AREA:c_swap_free#88f:free swap:STACK",
100 "AREA:mem_used#ff0:used memory",
101 "AREA:mem_buffered#00f:page cache:STACK",
102 "AREA:mem_cached#008:buffer cache:STACK",
103 "AREA:mem_free#ccc:unused memory:STACK",
106 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
109 ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
110 "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
111 "CDEF:mrx_$_=0,rx_$_,-")
112 } qw(octets packets errors)),
114 ("CDEF:${_}_kb=${_}_octets,1024,/",
115 "CDEF:${_}_errsx=${_}_errors,1000,*")
117 "AREA:tx_kb#080:kby",
118 "LINE:tx_packets#0f0:pkts",
119 "LINE:tx_errsx#000:errs",
120 "AREA:mrx_kb#008:kby",
121 "LINE:mrx_packets#00f:pkts",
122 "LINE:mrx_errsx#444:errs",
125 graph('General', 'Users', { },
127 "DEF:users=$R/users/users.rrd:users:AVERAGE",
128 "LINE:users#008:users"
131 foreach my $src (<$R/df/df-*.rrd>) {
133 $vol =~ s,\.rrd$,, or next;
136 graph('Disk space', $vol, {
141 (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
142 "AREA:used#000:used:STACK",
143 "AREA:free#88f:free:STACK",
149 if (!open NM, '<', "$SELF/data/news/name-map") {
150 die unless $!==&ENOENT;
154 next unless m/^[^\#]/;
155 m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
157 $news_name_map{$1,$_}= $3 foreach qw(in out);
159 $news_name_map{$1,$2}= $3;
166 foreach my $src (<$SELF/data/news/*.rrd>) {
168 $site =~ s,\.rrd$,, or next;
170 $site =~ s,_(in|out)$,,;
172 $site =~ s/^([-.0-9a-z]+)_//;
173 my $us= $1; # all very well but we ignore it
174 my $newsite= $news_name_map{$site,$inout};
175 $site= $newsite if defined $newsite;
176 next if $site eq '-';
177 push @{ $news_sources{$site}{$inout} }, $src;
182 foreach my $site (keys %news_sources) {
186 s/^[^.]*(?:chiark|greenend|news|nntp|peer|feed|in|out)[^.]*\.//;
189 foreach my $inout (keys %{ $news_sources{$site} }) {
190 push @news_graphs, [ "$sk $inout", $site, $inout ];
194 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
195 my ($sortkey, $site, $inout)= @$siteinfo;
196 my @sources= @{ $news_sources{$site}{$inout} };
198 my @vals= $inout eq 'out'
199 ? qw(missing deferred unwanted accepted rejected body_missing)
200 : qw(accepted refused rejected duplicate
201 accepted_size duplicate_size);
203 foreach my $val (@vals) {
204 my $def= "CDEF:$val=0";
205 foreach my $si (0..$#sources) {
206 my $src= $sources[$si];
207 my $tvar= "${val}_${si}";
208 push @defs, "DEF:$tvar=$src:$val:AVERAGE";
209 $def .= ",$tvar,ADDNAN";
212 if ($val =~ m/_size$/) {
213 push @defs, "CDEF:kb_$`=$val,1024,/";
216 graph_of_group("News", $site, $inout,
219 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
223 "AREA:accepted#00f:ok",
224 "AREA:body_missing#ff0:miss:STACK",
225 "AREA:rejected#f00:rej:STACK",
226 "AREA:unwanted#aaa:unw:STACK",
227 "AREA:deferred#ddd:defer:STACK",
231 "AREA:accepted#00f:ok:STACK",
232 "AREA:rejected#f00:rej:STACK",
233 "AREA:duplicate#000:dupe:STACK",
234 "AREA:refused#aaa:unw:STACK",
235 "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
236 "LINE:kb_duplicate#ff0:kb dupe",
237 "LINE:kb_accepted_smooth#008:~kb",
243 foreach my $physdiskrrd (<$R/disk-*/disk_octets.rrd>) {
244 $physdiskrrd =~ s,octets\.rrd$,, or die;
245 $physdiskrrd =~ m,-([^/]+)/disk_$, or die;
246 my $physdev= "/dev/$1";
247 if (!stat $physdev) {
248 die "$physdev $!" unless $!==&ENOENT;
251 die "$physdev ?" unless S_ISBLK((stat _)[2]);
252 $disk_rdev2rrd{(stat _)[6]}= $physdiskrrd;
258 my ($vg, $label, $factor, $rcolour, $wcolour) = @_;
261 $varname =~ s/[^0-9a-zA-Y]/ sprintf "Z%02x", ord($&) /ge;
265 Colour => { 'read' => $rcolour, 'write' => $wcolour },
268 foreach my $bo (qw(octets ops)) {
269 foreach my $rw (qw(read write)) {
270 $vginfo->{VarDefs}{$bo}{$rw}= [];
271 $vginfo->{Sumdef}{$bo}{$rw}= '0';
275 foreach my $lvpath (</dev/$vg/*>) {
276 my $lv= $lvpath; $lv =~ s,.*/,,;
278 die "$lvpath $!" unless $!==&ENOENT;
281 die "$lvpath ?" unless S_ISBLK((stat _)[2]);
282 my $rrd= $disk_rdev2rrd{(stat _)[6]};
283 next unless defined $rrd;
285 my $lvinfo= { Name => $lv };
286 push @{ $vginfo->{Lvs} }, $lvinfo;
288 foreach my $bo (qw(octets ops)) {
289 $lvinfo->{Defs}{$bo}=
291 (map { ("DEF:$_=${rrd}${bo}.rrd:$_:AVERAGE") } qw(read write)),
292 "CDEF:mwrite=0,write,-",
293 "AREA:read#00f:read",
294 "AREA:mwrite#f00:write"
297 foreach my $rw (qw(read write)) {
299 my $tvar= "lv_${rw}_${bo}_${varname}_${ix}";
300 push @{ $vginfo->{VarDefs}{$bo}{$rw} },
301 "DEF:$tvar=${rrd}${bo}.rrd:$rw:AVERAGE";
302 $vginfo->{Sumdef}{$bo}{$rw} .= ",$tvar,+";
306 foreach my $bo (qw(octets ops)) {
307 foreach my $rw (qw(read write)) {
309 push @$defs, @{ $vginfo->{VarDefs}{$bo}{$rw} };
310 push @$defs, "CDEF:${rw}_vg_${varname}=".
311 $vginfo->{Sumdef}{$bo}{$rw}.
312 sprintf(",%f,*", $rw eq 'write' ? -$factor : $factor);
313 $vginfo->{Defs}{$bo}{$rw}= $defs;
316 push @disk_vgs, $vginfo;
319 lvgraphs('vg-main', 'main', 1, qw(00f f00));
320 lvgraphs('vg-chiark-stripe', 'stripe', 0.5, qw(008 800));
322 foreach my $bo (qw(octets ops)) {
324 foreach my $rw (qw(read write)) {
326 foreach my $vginfo (@disk_vgs) {
327 push @a, @{ $vginfo->{Defs}{$bo}{$rw} };
328 push @a, "AREA:${rw}_vg_$vginfo->{Varname}#".
329 $vginfo->{Colour}{$rw}.
330 ":$vginfo->{Name} ".substr($rw,0,1).
335 graph_of_group('IO', 'IO', $bo, { Units => '[/s]' }, \@a);
338 foreach my $vginfo (@disk_vgs) {
339 foreach my $bo (qw(octets ops)) {
340 foreach my $lv (@{ $vginfo->{Lvs} }) {
341 graph_of_group('IO', "$vginfo->{Name} $lv->{Name}",
342 $bo, { Units => '[/s]' }, $lv->{Defs}{$bo});
347 push @{ $section_groups{General} }, {
350 UrlParams => "section=IO&sloth=SLOTH"
354 #---------- right, that was the initialisation ----------
356 our $self= url(-relative=>1);
358 if (param('debug')) {
359 print "Content-Type: text/plain\n\n";
368 my $var= $nav->{Variable};
369 $$var= param($nav->{Param});
370 $$var= $nav->{Default} if !defined $$var;
371 die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
372 push @navsettings, $nav;
380 Variable => \$section,
381 Default => $sections[0],
382 Values => [@sections],
383 Show => sub { return $_[0]; }
387 sub num_param ($$$$) {
388 my ($param,$def,$min,$max) = @_;
389 my $v= param($param);
390 return $def if !defined $v;
391 $v =~ m/^([1-9]\d{0,8})$/ or die;
393 die unless $v >= $min && $v <= $max;
397 our $group= param('graph');
399 our $elem= param('elem');
401 my $g= $graphs{$section,$group,$elem};
404 my $width= num_param('w',370,100,1600);
405 my $height= num_param('h',200,100,1600);
407 my $sloth= param('sloth');
408 die unless defined $sloth;
409 $sloth =~ m/^(\d+)$/ or die;
411 my $end= $g->{TimeRanges}[$sloth];
412 die unless defined $end;
414 my $cacheid= "$section!$group!$elem!$sloth!$width!$height";
415 my $cachepath= "cache/$cacheid.png";
417 my @args= @{ $g->{Args} };
418 s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
419 unshift @args, qw(--end now --start), "end-${end}s";
422 if (length $elem) { $title.= " $elem"; }
424 $title .= " $g->{Units}" if $g->{Units};
425 unshift @args, '-t', $title, '-w',$width, '-h',$height;
426 unshift @args, qw(-a PNG --full-size-mode);
428 if (param('debug')) {
429 print((join "\n",@args),"\n"); exit 0;
432 #print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
433 exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
435 rrdtool graph "$p" --lazy "$@" >/dev/null
436 printf "Content-Type: image/png\n\n"
444 print header(), start_html($title);
446 foreach my $nav (@navsettings) {
448 print $nav->{Desc}, ": ";
450 my $current= $nav->{Variable}; $current= $$current;
451 foreach my $couldbe (@{ $nav->{Values} }) {
453 my $show= $nav->{Show}($couldbe);
454 if ($couldbe eq $current) {
455 print "<b>$show</b>";
459 foreach my $nav2 (@navsettings) {
460 my $current2= $nav2->{Variable}; $current2= $$current2;
461 $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
462 next if $current2 eq $nav2->{Default};
463 $u .= $delim2; $u .= "$nav2->{Param}=$current2";
466 print a({href=>$u}, $show);
470 $outerdelim= "<br>\n";
477 our $detail= param('detail');
478 if (defined $detail) {
479 my $elems= $group_elems{$section,$detail};
481 start_page("$detail - $section - graphs");
482 foreach my $tsloth (0..5) {
483 foreach my $elem (@$elems) {
484 my $g= $graphs{$section,$detail,$elem};
486 next if $tsloth >= @{ $g->{TimeRanges} };
487 my $imgurl= "$self?graph=$detail§ion=$section".
488 "&sloth=$tsloth&elem=$elem";
489 print a({href=>"$imgurl&w=780&h=800"},
490 img({src=>$imgurl, alt=>''}));
500 Desc => 'Time interval',
507 return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
511 if (param('debug')) {
513 print Dumper(\%graphs);
517 start_page("$section - graphs");
519 foreach my $group (@{ $section_groups{$section} }) {
520 my $ref_group= $group;
521 my $ref_section= $section;
522 my $ref_urlparams= "detail=$group§ion=$section";
524 $ref_group= $group->{Group};
525 $ref_section= $group->{Section};
526 $ref_urlparams= $group->{UrlParams};
527 $ref_urlparams =~ s/\bSLOTH\b/$sloth/;
529 print a({href=>"$self?$ref_urlparams"});
530 my $imgurl= "$self?graph=$ref_group§ion=$ref_section";
531 print "<span style=\"white-space:nowrap\">";
532 my $elems= $group_elems{$ref_section,$ref_group};
533 foreach my $elem (@$elems) {
534 my $g= $graphs{$ref_section,$ref_group,$elem};
535 print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),