chiark / gitweb /
works
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 25 Jun 2010 18:33:20 +0000 (19:33 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 25 Jun 2010 18:33:20 +0000 (19:33 +0100)
cgi [new file with mode: 0755]

diff --git a/cgi b/cgi
new file mode 100755 (executable)
index 0000000..e286069
--- /dev/null
+++ b/cgi
@@ -0,0 +1,121 @@
+#!/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 (@graphs, %graphs);
+
+sub graph ($$$) {
+    my ($gname, $basis, $args) = @_;
+    $basis->{Args}= $args;
+    $graphs{$gname}= $basis;
+    push @graphs, $gname;
+}
+
+graph('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('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('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",
+       ]);
+
+if (param('debug')) {
+    print "Content-Type: text/plain\n\n";
+}
+
+my $gname= param('graph');
+
+if ($gname) {
+    my $g= $graphs{$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 380 -h 200), @args;
+    die $!;
+}
+
+my $detail= param('detail');
+if ($detail) {
+    die unless $graphs{$detail};
+    print header(), start_html(), h1("$detail graphs");
+    foreach my $end (qw(3600 86400 604800 2419200)) {
+        print "<img src=\"$self?graph=$detail&end=$end\">\n";
+    }
+    print end_html();
+    exit 0;
+}
+
+if (param('debug')) {
+    use Data::Dumper;
+    print Dumper(\%graphs);
+    exit 0;
+}
+
+print header(), start_html();
+print h1('Graphs');
+
+foreach my $gname (@graphs) {
+    print "<a href=\"$self?detail=$gname\">"; #,h2($gname),"</a>";
+    print "<img src=\"$self?graph=$gname\"></a>\n";
+}
+