chiark / gitweb /
different ranges
[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 (@sections, %sections, %graphs);
20
21 sub graph ($$$$) {
22     my ($section, $gname, $basis, $args) = @_;
23     $basis->{Args}= $args;
24     $basis->{DefTimeRange} ||= 3600;
25     $basis->{MinTimeRange} ||= 3600;
26     $basis->{MaxTimeRange} ||= 28*86400;
27     $graphs{$section,$gname}= $basis;
28     if (!exists $sections{$section}) {
29         push @sections, $section;
30     }
31     push @{ $sections{$section} }, $gname;
32 }
33
34 graph('General', 'Load', { },
35       [
36        "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
37        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
38             qw(blocked running stopped paging sleeping zombies)),
39        "AREA:running#88f:running processes:STACK",
40        "AREA:blocked#8f8:blocked processes:STACK",
41        "AREA:paging#f88:paging processes:STACK",
42        "LINE:load#000:load",
43        ]);
44
45 graph('General', 'Processes', { },
46       [
47        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
48             qw(blocked running stopped paging sleeping zombies)),
49        "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
50        "AREA:sleeping#ccc:sleeping:STACK",
51        "AREA:stopped#f00:stopped:STACK",
52        "AREA:zombies#0f0:zombie:STACK",
53        "AREA:busy#f00:busy:STACK",
54        ]);
55
56 graph('General', 'CPU', { Units => '[%]' },
57       [
58        (map {
59            my $thing= $_;
60            (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
61                 (0..7)),
62            "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
63        } qw(idle interrupt nice softirq steal system user wait)),
64        "AREA:system#00f:system:STACK",
65        "AREA:wait#f88:wait:STACK",
66        "AREA:nice#ccc:nice:STACK",
67        "AREA:user#080:user:STACK",
68        "AREA:softirq#f0f:softirq:STACK",
69        "AREA:interrupt#ff0:interrupt:STACK",
70        "AREA:steal#0ff:steal:STACK",
71        ]);
72
73 foreach my $src (<$R/df/df-*.rrd>) {
74     my $vol= $src;
75     $vol =~ s,.*/,,;
76     $vol =~ s,^df-,,;
77     $vol =~ s,\.rrd$,,;
78     graph('Disk space', $vol, {
79              MinTimeRange => 86400,
80              MaxTimeRange => (13*7+1)*86400,
81              DefTimeRange => 7*86400
82           },
83           [
84            qw(-b 1024 -l 0),
85            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
86            "AREA:used#000:used:STACK",
87            "AREA:free#88f:free:STACK",
88            ]);
89 }
90
91 if (param('debug')) {
92     print "Content-Type: text/plain\n\n";
93 }
94
95 my $gname= param('graph');
96 my $section= param('section');
97 $section ||= $sections[0];
98 die unless $sections{$section};
99
100 if ($gname) {
101     my $g= $graphs{$section,$gname};
102     die unless $g;
103
104     my @args= @{ $g->{Args} };
105
106     my $end= param('end');
107     if (defined $end) {
108         $end =~ m/^(\d+)$/ or die;
109         unshift @args, qw(--end now --start), "end-${end}s";
110     }
111     if (param('debug')) {
112         print((join "\n",@args),"\n"); exit 0;
113     }
114     print "Content-Type: image/png\n\n";
115
116     my $title= $gname;
117     $title .= " $g->{Units}" if $g->{Units};
118     unshift @args, '-t', $title;
119     
120     exec qw(rrdtool graph - -a PNG --full-size-mode -w 370 -h 200), @args;
121     die $!;
122 }
123
124 sub start_page ($) {
125     my ($title) = @_;
126     print header(), start_html($title);
127     my $delim= '';
128     foreach my $s2 (@sections) {
129         print $delim;
130         if ($s2 eq $section) {
131             print "<b>$section</b>";
132         } else {
133             print "<a href=\"$self";
134             if ($s2 ne $sections[0]) { print "?section=$s2"; }
135             print "\">$s2</a>";
136         }
137         $delim= ' | ';
138     }
139     print h1("$title");
140 }
141
142 my $detail= param('detail');
143 if ($detail) {
144     my $g= $graphs{$section,$detail};
145     die unless $g;
146     start_page("$detail graphs");
147     foreach my $end (qw(300 3600 86400 604800 2419200 7948800)) {
148         next if $end < $g->{MinTimeRange};
149         next if $end > $g->{MaxTimeRange};
150         print "<img src=\"$self?graph=$detail&section=$section&end=$end\">\n";
151     }
152     print end_html();
153     exit 0;
154 }
155
156 if (param('debug')) {
157     use Data::Dumper;
158     print Dumper(\%graphs);
159     exit 0;
160 }
161
162 start_page("$section graphs");
163
164 foreach my $gname (@{ $sections{$section} }) {
165     my $g= $graphs{$section,$gname};
166     print "<a href=\"$self?detail=$gname&section=$section\">"; #,h2($gname),"</a>";
167     print "<img src=\"$self?graph=$gname&section=$section&end=$g->{DefTimeRange}\"></a>\n";
168 }
169