-#!/usr/bin/perl -w
+#!/usr/bin/speedy -w -- -t100 -M1
use strict qw(vars);
-use CGI qw/:standard/;
+use CGI::SpeedyCGI qw/:standard -no_xhtml/;
+use CGI qw/:standard -no_xhtml/;
+use POSIX;
sub fail ($) {
print(header(-status=>500),
exit 0;
}
-our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
+our (@sections, %section_groups, %group_elems, %graphs);
-my $self= url(-relative=>1);
+#---------- initialisation code, run once - graphs setup ----------
-our (@sections, %sections, %graphs);
+BEGIN {
-our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1);
+our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
+our $SELF= '/home/ijackson/things/rrd-graphs';
-sub graph ($$$$) {
- my ($section, $gname, $basis, $args) = @_;
+our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
+
+sub graph_of_group ($$$$$) {
+ my ($section, $group, $elem, $basis, $args) = @_;
$basis->{Args}= $args;
$basis->{Slower}= 0 unless exists $basis->{Slower};
- $graphs{$section,$gname}= $basis;
- if (!exists $sections{$section}) {
- push @sections, $section;
+ $basis->{TimeRanges} ||= \@timeranges;
+ $graphs{$section,$group,$elem}= $basis;
+ if (!exists $group_elems{$section,$group}) {
+ # new group then
+ if (!exists $section_groups{$section}) {
+ # new section even
+ push @sections, $section;
+ }
+ push @{ $section_groups{$section} }, $group;
}
- push @{ $sections{$section} }, $gname;
+ push @{ $group_elems{$section,$group} }, $elem;
}
-graph('General', 'Load', { },
+sub graph ($$$$) {
+ my ($section, $gname, $basis, $args) = @_;
+ graph_of_group($section, $gname,'', $basis, $args);
+}
+
+graph('General', 'Load and processes', { },
[
"DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
(map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
qw(blocked running stopped paging sleeping zombies)),
- "AREA:running#88f:running processes:STACK",
- "AREA:blocked#8f8:blocked processes:STACK",
- "AREA:paging#f88:paging processes:STACK",
+ "AREA:running#88f:running:STACK",
+ "AREA:blocked#8f8:disk wait:STACK",
+ "AREA:paging#f88:paging:STACK",
"LINE:load#000:load",
]);
(0..7)),
"CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
} qw(idle interrupt nice softirq steal system user wait)),
- "AREA:system#00f:system:STACK",
- "AREA:wait#f88:wait:STACK",
+ "CDEF:allintr=softirq,steal,+,interrupt,+",
+ "AREA:allintr#ff0:interrupt:STACK",
+ "AREA:system#88f:system:STACK",
+ "AREA:user#00f:user:STACK",
"AREA:nice#ccc:nice:STACK",
- "AREA:user#080:user:STACK",
- "AREA:softirq#f0f:softirq:STACK",
- "AREA:interrupt#ff0:interrupt:STACK",
- "AREA:steal#0ff:steal:STACK",
+ "AREA:wait#f00:wait:STACK",
]);
graph('General', 'Memory', { },
foreach my $src (<$R/df/df-*.rrd>) {
my $vol= $src;
+ $vol =~ s,\.rrd$,, or next;
$vol =~ s,.*/,,;
$vol =~ s,^df-,,;
- $vol =~ s,\.rrd$,,;
graph('Disk space', $vol, {
Slower => 1,
},
]);
}
+our %news_name_map;
+
+if (!open NM, '<', "$SELF/data/news/name-map") {
+ die unless $!==&ENOENT;
+} else {
+ while (<NM>) {
+ s/^\s*//; s/\s+$//;
+ next unless m/^[^\#]/;
+ m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
+ if ($2 eq '*') {
+ $news_name_map{$1,$_}= $3 foreach qw(in out);
+ } else {
+ $news_name_map{$1,$2}= $3;
+ }
+ }
+}
+
+our @news_graphs;
+
+foreach my $src (<$SELF/data/news/*.rrd>) {
+ my $site= $src;
+ $site =~ s,\.rrd$,, or next;
+ $site =~ s,.*/,,;
+ $site =~ s,_(in|out)$,,;
+ my $inout= $1;
+ $site =~ s/^([-.0-9a-z]+)_//;
+ my $us= $1; # all very well but we ignore it
+ my $newsite= $news_name_map{$site,$inout};
+ $site= $newsite if defined $newsite;
+ next if $site eq '-';
+ #my $sk= join '.', reverse split /\./, $site;
+ my $sk= $site;
+ $sk .= " $&" if $sk =~ s/^[^.]*(?:news|nntp|peer)[^.]*\.//;
+ $sk .= " $inout";
+ push @news_graphs, [ $sk, $site, $inout, $src ];
+}
+
+foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
+ my ($sortkey, $site, $inout, $src)= @$siteinfo;
+ graph_of_group("News", $site, $inout,
+ {
+ Units => '[art/s]',
+ TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
+ }, $inout eq 'out' ?
+ [
+ (map { "DEF:$_=$src:$_:AVERAGE" }
+ qw(missing deferred unwanted accepted rejected body_missing)),
+ "AREA:accepted#00f:ok",
+ "AREA:body_missing#ff0:miss:STACK",
+ "AREA:rejected#f00:rej:STACK",
+ "AREA:unwanted#aaa:unw:STACK",
+ "AREA:deferred#ddd:defer:STACK",
+ ] :
+ [
+ (map { "DEF:$_=$src:$_:AVERAGE" }
+ qw(accepted refused rejected duplicate)),
+ (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
+ "CDEF:kb_$_=bytes_$_,1024,/")
+ } qw(accepted duplicate)),
+ "AREA:accepted#00f:ok:STACK",
+ "AREA:rejected#f00:rej:STACK",
+ "AREA:duplicate#000:dupe:STACK",
+ "AREA:refused#aaa:unw:STACK",
+ "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
+ "LINE:kb_duplicate#ff0:kb dupe",
+ "LINE:kb_accepted_smooth#008:~kb",
+ ]);
+}
+
+our %disk_rdev2rrd;
+
+foreach my $physdiskrrd (<$R/disk-*/disk_octets.rrd>) {
+ $physdiskrrd =~ s,octets\.rrd$,, or die;
+ $physdiskrrd =~ m,-([^/]+)/disk_$, or die;
+ my $physdev= "/dev/$1";
+ if (!stat $physdev) {
+ die "$physdev $!" unless $!==&ENOENT;
+ next;
+ }
+ die "$physdev ?" unless S_ISBLK((stat _)[2]);
+ $disk_rdev2rrd{(stat _)[6]}= $physdiskrrd;
+}
+
+our @disk_vgs;
+
+sub lvgraphs ($$) {
+ my ($vg, $factor) = @_;
+ my @lvs;
+ my $varname= $vg;
+ $varname =~ s/[^0-9a-zA-Y]/ sprintf "Z%02x", ord($&) /ge;
+ my $vginfo= {
+ Name => $vg,
+ Varname => $varname
+ };
+ foreach my $bo (qw(octets ops)) {
+ foreach my $rw (qw(read write)) {
+ $vginfo->{VarDefs}{$bo}{$rw}= [];
+ $vginfo->{Sumdef}{$bo}{$rw}= '0';
+ }
+ }
+ my $ix=0;
+ foreach my $lvpath (</dev/$vg/*>) {
+ my $lv= $lvpath; $lv =~ s,.*/,,;
+ if (!stat $lvpath) {
+ die "$lvpath $!" unless $!==&ENOENT;
+ next;
+ }
+ die "$lvpath ?" unless S_ISBLK((stat _)[2]);
+ my $rrd= $disk_rdev2rrd{(stat _)[6]};
+ next unless defined $rrd;
+
+ foreach my $bo (qw(octets ops)) {
+0 and
+ graph_of_group('IO', "$vg $lv", $bo, { Units => '[/s]' },
+ [
+ (map { ("DEF:$_=${rrd}${bo}.rrd:$_:AVERAGE") } qw(read write)),
+ "CDEF:mwrite=0,write,-",
+ "AREA:read#00f:read",
+ "AREA:mwrite#f00:write"
+ ]);
+ foreach my $rw (qw(read write)) {
+ $ix++;
+ my $tvar= "lv_${rw}_${bo}_${varname}_${ix}";
+ push @{ $vginfo->{VarDefs}{$bo}{$rw} },
+ "DEF:$tvar=${rrd}${bo}.rrd:$rw:AVERAGE";
+ $vginfo->{Sumdef}{$bo}{$rw} .= ",$tvar,+";
+ }
+ }
+ }
+ foreach my $bo (qw(octets ops)) {
+ my $defs= [];
+ foreach my $rw (qw(read write)) {
+ push @$defs, @{ $vginfo->{VarDefs}{$bo}{$rw} };
+ push @$defs, "CDEF:${rw}_vg_${varname}=".
+ $vginfo->{Sumdef}{$bo}{$rw}.
+ sprintf(",%f,*", $rw eq 'write' ? -$factor : $factor);
+ }
+ $vginfo->{Defs}{$bo}= $defs;
+ }
+ push @disk_vgs, $vginfo;
+}
+
+lvgraphs('vg-main', 1);
+lvgraphs('vg-chiark-stripe', 0.5);
+
+foreach my $bo (qw(octets ops)) {
+ my @a= ();
+ foreach my $rw (qw(read write)) {
+ my $stack= '';
+ foreach my $vginfo (@disk_vgs) {
+ push @a, @{ $vginfo->{Defs}{$bo} };
+ push @a, "AREA:${rw}_vg_$vginfo->{Varname}#0ff:$vginfo->{Name}"
+ .$stack;
+ $stack= ':STACK';
+ }
+ }
+ graph_of_group('IO', 'T', $bo, { Units => '[/s]' }, \@a);
+}
+
+}
+#---------- right, that was the initialisation ----------
+
+our $self= url(-relative=>1);
+
if (param('debug')) {
print "Content-Type: text/plain\n\n";
}
our @navsettings;
+@navsettings= ();
+
sub navsetting ($) {
my ($nav) = @_;
my $var= $nav->{Variable};
});
-my $gname= param('graph');
-
sub num_param ($$$$) {
my ($param,$def,$min,$max) = @_;
my $v= param($param);
return $v + 0;
}
-if ($gname) {
- my $g= $graphs{$section,$gname};
- die unless $g;
+our $group= param('graph');
- my @args= @{ $g->{Args} };
+our $elem= param('elem');
+if (defined $elem) {
+ my $g= $graphs{$section,$group,$elem};
+ die unless $g;
my $width= num_param('w',370,100,1600);
my $height= num_param('h',200,100,1600);
- my $end= param('end');
- if (defined $end) {
- $end =~ m/^(\d+)$/ or die;
- unshift @args, qw(--end now --start), "end-${end}s";
- }
+ my $sloth= param('sloth');
+ die unless defined $sloth;
+ $sloth =~ m/^(\d+)$/ or die;
+ $sloth= $1+0;
+ my $end= $g->{TimeRanges}[$sloth];
+ die unless defined $end;
+
+ my $cacheid= "$section!$group!$elem!$sloth!$width!$height";
+ my $cachepath= "cache/$cacheid.png";
+
+ my @args= @{ $g->{Args} };
+ s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
+ unshift @args, qw(--end now --start), "end-${end}s";
+
+ my $title= $group;
+ if (length $elem) { $title.= " $elem"; }
+
+ $title .= " $g->{Units}" if $g->{Units};
+ unshift @args, '-t', $title, '-w',$width, '-h',$height;
+ unshift @args, qw(-a PNG --full-size-mode);
+
if (param('debug')) {
print((join "\n",@args),"\n"); exit 0;
}
- print "Content-Type: image/png\n\n";
- my $title= $gname;
- $title .= " $g->{Units}" if $g->{Units};
- unshift @args, '-t', $title;
-
- exec (qw(rrdtool graph - -a PNG --full-size-mode),
- '-w',$width, '-h',$height,
- @args);
+#print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
+ exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
+ p="$1"; shift
+ rrdtool graph "$p" --lazy "$@" >/dev/null
+ printf "Content-Type: image/png\n\n"
+ exec cat "$p"
+END
die $!;
}
if ($couldbe eq $current) {
print "<b>$show</b>";
} else {
- print "<a href=\"$self";
+ my $u= $self;
my $delim2= '?';
foreach my $nav2 (@navsettings) {
my $current2= $nav2->{Variable}; $current2= $$current2;
$current2= $couldbe if $nav2->{Param} eq $nav->{Param};
next if $current2 eq $nav2->{Default};
- print $delim2, "$nav2->{Param}=$current2";
+ $u .= $delim2; $u .= "$nav2->{Param}=$current2";
$delim2= '&';
}
- print "\">$show</a>";
+ print a({href=>$u}, $show);
}
$delim= ' | ';
}
print h1("$title");
}
-my $detail= param('detail');
+our $detail= param('detail');
if ($detail) {
- my $g= $graphs{$section,$detail};
- die unless $g;
+ my $elems= $group_elems{$section,$detail};
+ die unless $elems;
start_page("$detail graphs");
- foreach my $end (@timeranges[$g->{Slower}..$g->{Slower}+3]) {
- my $imgurl= "$self?graph=$detail§ion=$section&end=$end";
- print "<a href=\"$imgurl&w=780&h=800\"><img src=\"$imgurl\"></a>\n";
+ foreach my $tsloth (0..5) {
+ foreach my $elem (@$elems) {
+ my $g= $graphs{$section,$detail,$elem};
+ die unless $g;
+ next if $tsloth >= @{ $g->{TimeRanges} };
+ my $imgurl= "$self?graph=$detail§ion=$section".
+ "&sloth=$tsloth&elem=$elem";
+ print a({href=>"$imgurl&w=780&h=800"},
+ img({src=>$imgurl, alt=>''}));
+ }
}
print end_html();
exit 0;
Param => 'sloth',
Variable => \$sloth,
Default => 1,
- Values => [0..2],
+ Values => [0..3],
Show => sub {
my ($sl) = @_;
- return ('Narrower', 'Normal', 'Wider')[$sl];
+ return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
}
});
start_page("$section graphs");
-foreach my $gname (@{ $sections{$section} }) {
- my $g= $graphs{$section,$gname};
- print "<a href=\"$self?detail=$gname§ion=$section\">";
- my $end= $timeranges[$g->{Slower}+$sloth];
- my $imgurl= "$self?graph=$gname§ion=$section&end=$end";
- print "<img src=\"$imgurl\"></a>\n";
+foreach my $group (@{ $section_groups{$section} }) {
+ print a({href=>"$self?detail=$group§ion=$section"});
+ my $imgurl= "$self?graph=$group§ion=$section";
+ print "<span style=\"white-space:nowrap\">";
+ my $elems= $group_elems{$section,$group};
+ foreach my $elem (@$elems) {
+ my $g= $graphs{$section,$group,$elem};
+ print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
+ alt=>''});
+ }
+ print "</span>";
+ print "</a>\n";
}