#!/usr/bin/perl -w
use strict qw(vars);
use CGI qw/:standard/;
sub fail ($) {
print(header(-status=>500),
start_html('Error'),
h1('Error'),
escapeHTML($_[0]),
end_html());
exit 0;
}
our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
my $self= url(-relative=>1);
our (@sections, %sections, %graphs);
sub graph ($$$$) {
my ($section, $gname, $basis, $args) = @_;
$basis->{Args}= $args;
$basis->{DefTimeRange} ||= 3600;
$basis->{MinTimeRange} ||= 3600;
$basis->{MaxTimeRange} ||= 28*86400;
$graphs{$section,$gname}= $basis;
if (!exists $sections{$section}) {
push @sections, $section;
}
push @{ $sections{$section} }, $gname;
}
graph('General', 'Load', { },
[
"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",
"LINE:load#000:load",
]);
graph('General', 'Processes', { },
[
(map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
qw(blocked running stopped paging sleeping zombies)),
"CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
"AREA:sleeping#ccc:sleeping:STACK",
"AREA:stopped#f00:stopped:STACK",
"AREA:zombies#0f0:zombie:STACK",
"AREA:busy#f00:busy:STACK",
]);
graph('General', 'CPU', { Units => '[%]' },
[
(map {
my $thing= $_;
(map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
(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",
"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",
]);
foreach my $src (<$R/df/df-*.rrd>) {
my $vol= $src;
$vol =~ s,.*/,,;
$vol =~ s,^df-,,;
$vol =~ s,\.rrd$,,;
graph('Disk space', $vol, {
MinTimeRange => 86400,
MaxTimeRange => (13*7+1)*86400,
DefTimeRange => 7*86400
},
[
qw(-b 1024 -l 0),
(map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
"AREA:used#000:used:STACK",
"AREA:free#88f:free:STACK",
]);
}
if (param('debug')) {
print "Content-Type: text/plain\n\n";
}
my $gname= param('graph');
my $section= param('section');
$section ||= $sections[0];
die unless $sections{$section};
if ($gname) {
my $g= $graphs{$section,$gname};
die unless $g;
my @args= @{ $g->{Args} };
my $end= param('end');
if (defined $end) {
$end =~ m/^(\d+)$/ or die;
unshift @args, qw(--end now --start), "end-${end}s";
}
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 370 -h 200), @args;
die $!;
}
sub start_page ($) {
my ($title) = @_;
print header(), start_html($title);
my $delim= '';
foreach my $s2 (@sections) {
print $delim;
if ($s2 eq $section) {
print "$section";
} else {
print "$s2";
}
$delim= ' | ';
}
print h1("$title");
}
my $detail= param('detail');
if ($detail) {
my $g= $graphs{$section,$detail};
die unless $g;
start_page("$detail graphs");
foreach my $end (qw(300 3600 86400 604800 2419200 7948800)) {
next if $end < $g->{MinTimeRange};
next if $end > $g->{MaxTimeRange};
print "\n";
}
print end_html();
exit 0;
}
if (param('debug')) {
use Data::Dumper;
print Dumper(\%graphs);
exit 0;
}
start_page("$section graphs");
foreach my $gname (@{ $sections{$section} }) {
my $g= $graphs{$section,$gname};
print ""; #,h2($gname),"";
print "{DefTimeRange}\">\n";
}