chiark / gitweb /
works
[rrd-graphs.git] / cgi
1 #!/usr/bin/perl -w
2
3 use strict qw(vars);
4 use CGI qw/:standard/;
5
6 sub fail ($) {
7     print(header(-status=>500),
8           start_html('Error'),
9           h1('Error'),
10           escapeHTML($_[0]),
11           end_html());
12     exit 0;
13 }
14
15 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
16
17 my $self= url(-relative=>1);
18
19 our (@graphs, %graphs);
20
21 sub graph ($$$) {
22     my ($gname, $basis, $args) = @_;
23     $basis->{Args}= $args;
24     $graphs{$gname}= $basis;
25     push @graphs, $gname;
26 }
27
28 graph('Load', { },
29       [
30        "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
31        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
32             qw(blocked running stopped paging sleeping zombies)),
33        "AREA:running#88f:running processes:STACK",
34        "AREA:blocked#8f8:blocked processes:STACK",
35        "AREA:paging#f88:paging processes:STACK",
36        "LINE:load#000:load",
37        ]);
38
39 graph('Processes', { },
40       [
41        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
42             qw(blocked running stopped paging sleeping zombies)),
43        "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
44        "AREA:sleeping#ccc:sleeping:STACK",
45        "AREA:stopped#f00:stopped:STACK",
46        "AREA:zombies#0f0:zombie:STACK",
47        "AREA:busy#f00:busy:STACK",
48        ]);
49
50 graph('CPU', { Units => '[%]' },
51       [
52        (map {
53            my $thing= $_;
54            (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
55                 (0..7)),
56            "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
57        } qw(idle interrupt nice softirq steal system user wait)),
58        "AREA:system#00f:system:STACK",
59        "AREA:wait#f88:wait:STACK",
60        "AREA:nice#ccc:nice:STACK",
61        "AREA:user#080:user:STACK",
62        "AREA:softirq#f0f:softirq:STACK",
63        "AREA:interrupt#ff0:interrupt:STACK",
64        "AREA:steal#0ff:steal:STACK",
65        ]);
66
67 if (param('debug')) {
68     print "Content-Type: text/plain\n\n";
69 }
70
71 my $gname= param('graph');
72
73 if ($gname) {
74     my $g= $graphs{$gname};
75     die unless $g;
76
77     my @args= @{ $g->{Args} };
78
79     my $end= param('end');
80     if (defined $end) {
81         $end =~ m/^(\d+)$/ or die;
82         unshift @args, qw(--end now --start), "end-${end}s";
83     }
84     if (param('debug')) {
85         print((join "\n",@args),"\n"); exit 0;
86     }
87     print "Content-Type: image/png\n\n";
88
89     my $title= $gname;
90     $title .= " $g->{Units}" if $g->{Units};
91     unshift @args, '-t', $title;
92     
93     exec qw(rrdtool graph - -a PNG --full-size-mode -w 380 -h 200), @args;
94     die $!;
95 }
96
97 my $detail= param('detail');
98 if ($detail) {
99     die unless $graphs{$detail};
100     print header(), start_html(), h1("$detail graphs");
101     foreach my $end (qw(3600 86400 604800 2419200)) {
102         print "<img src=\"$self?graph=$detail&end=$end\">\n";
103     }
104     print end_html();
105     exit 0;
106 }
107
108 if (param('debug')) {
109     use Data::Dumper;
110     print Dumper(\%graphs);
111     exit 0;
112 }
113
114 print header(), start_html();
115 print h1('Graphs');
116
117 foreach my $gname (@graphs) {
118     print "<a href=\"$self?detail=$gname\">"; #,h2($gname),"</a>";
119     print "<img src=\"$self?graph=$gname\"></a>\n";
120 }
121