#!/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); our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1); sub graph ($$$$) { my ($section, $gname, $basis, $args) = @_; $basis->{Args}= $args; $basis->{Slower}= 0 unless exists $basis->{Slower}; $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", ]); graph('General', 'Memory', { }, [ (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" } qw(free used cached)), (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" } qw(buffered free used cached)), "CDEF:c_swap_used=0,swap_used,-", "CDEF:c_swap_cached=0,swap_cached,-", "CDEF:c_swap_free=0,swap_free,-", "AREA:c_swap_used#000:used swap", "AREA:c_swap_cached#888:\"cached\" swap:STACK", # "AREA:c_swap_free#88f:free swap:STACK", "AREA:mem_used#ff0:used memory", "AREA:mem_buffered#00f:page cache:STACK", "AREA:mem_cached#008:buffer cache:STACK", "AREA:mem_free#ccc:unused memory:STACK", ]); foreach my $src (<$R/df/df-*.rrd>) { my $vol= $src; $vol =~ s,.*/,,; $vol =~ s,^df-,,; $vol =~ s,\.rrd$,,; graph('Disk space', $vol, { Slower => 1, }, [ '-A','-l',0,'-r', 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"; } our @navsettings; sub navsetting ($) { my ($nav) = @_; my $var= $nav->{Variable}; $$var= param($nav->{Param}); $$var= $nav->{Default} if !defined $$var; die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} }; push @navsettings, $nav; } our $section; navsetting({ Desc => 'Section', Param => 'section', Variable => \$section, Default => $sections[0], Values => [@sections], Show => sub { return $_[0]; } }); my $gname= param('graph'); sub num_param ($$$$) { my ($param,$def,$min,$max) = @_; my $v= param($param); return $def if !defined $v; $v =~ m/^([1-9]\d{0,8})$/ or die; $v= $1; die unless $v >= $min && $v <= $max; return $v + 0; } if ($gname) { my $g= $graphs{$section,$gname}; die unless $g; my @args= @{ $g->{Args} }; 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"; } 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); die $!; } sub start_page ($) { my ($title) = @_; print header(), start_html($title); my $outerdelim= ''; foreach my $nav (@navsettings) { print $outerdelim; print $nav->{Desc}, ": "; my $delim= ''; my $current= $nav->{Variable}; $current= $$current; foreach my $couldbe (@{ $nav->{Values} }) { print $delim; my $show= $nav->{Show}($couldbe); if ($couldbe eq $current) { print "$show"; } else { print "{Variable}; $current2= $$current2; $current2= $couldbe if $nav2->{Param} eq $nav->{Param}; next if $current2 eq $nav2->{Default}; print $delim2, "$nav2->{Param}=$current2"; $delim2= '&'; } print "\">$show"; } $delim= ' | '; } $outerdelim= "
\n"; } print "\n"; print h1("$title"); } my $detail= param('detail'); if ($detail) { my $g= $graphs{$section,$detail}; die unless $g; start_page("$detail graphs"); foreach my $end (@timeranges[$g->{Slower}..$g->{Slower}+3]) { my $imgurl= "$self?graph=$detail§ion=$section&end=$end"; print "\n"; } print end_html(); exit 0; } our $sloth; navsetting({ Desc => 'Time interval', Param => 'sloth', Variable => \$sloth, Default => 1, Values => [0..2], Show => sub { my ($sl) = @_; return ('Narrower', 'Normal', 'Wider')[$sl]; } }); 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 ""; my $end= $timeranges[$g->{Slower}+$sloth]; my $imgurl= "$self?graph=$gname§ion=$section&end=$end"; print "\n"; }