chiark / gitweb /
network; correct base on memory; users
[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 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1);
22
23 sub graph ($$$$) {
24     my ($section, $gname, $basis, $args) = @_;
25     $basis->{Args}= $args;
26     $basis->{Slower}= 0 unless exists $basis->{Slower};
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 graph('General', 'Memory', { },
74       [ '-b',1024,
75        (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
76             qw(free used cached)),
77        (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
78             qw(buffered free used cached)),
79        "CDEF:c_swap_used=0,swap_used,-",
80        "CDEF:c_swap_cached=0,swap_cached,-",
81        "CDEF:c_swap_free=0,swap_free,-",
82        "AREA:c_swap_used#000:used swap",
83        "AREA:c_swap_cached#888:\"cached\" swap:STACK",
84 #       "AREA:c_swap_free#88f:free swap:STACK",
85        "AREA:mem_used#ff0:used memory",
86        "AREA:mem_buffered#00f:page cache:STACK",
87        "AREA:mem_cached#008:buffer cache:STACK",
88        "AREA:mem_free#ccc:unused memory:STACK",
89        ]);
90
91 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
92       [
93        (map {
94            ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
95             "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
96             "CDEF:mrx_$_=0,rx_$_,-")
97            } qw(octets packets errors)),
98        (map {
99            ("CDEF:${_}_kb=${_}_octets,1024,/",
100             "CDEF:${_}_errsx=${_}_errors,1000,*")
101            } qw(mrx tx)),
102        "AREA:tx_kb#080:kby",
103        "LINE:tx_packets#0f0:pkts",
104        "LINE:tx_errsx#000:errs",
105        "AREA:mrx_kb#008:kby",
106        "LINE:mrx_packets#00f:pkts",
107        "LINE:mrx_errsx#444:errs",
108       ]);
109
110 graph('General', 'Users', {  },
111       [
112        "DEF:users=$R/users/users.rrd:users:AVERAGE",
113        "LINE:users#008:users"
114        ]);
115
116 foreach my $src (<$R/df/df-*.rrd>) {
117     my $vol= $src;
118     $vol =~ s,.*/,,;
119     $vol =~ s,^df-,,;
120     $vol =~ s,\.rrd$,,;
121     graph('Disk space', $vol, {
122              Slower => 1,
123           },
124           [ '-A','-l',0,'-r',
125            qw(-b 1024 -l 0),
126            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
127            "AREA:used#000:used:STACK",
128            "AREA:free#88f:free:STACK",
129            ]);
130 }
131
132 if (param('debug')) {
133     print "Content-Type: text/plain\n\n";
134 }
135
136 our @navsettings;
137
138 sub navsetting ($) {
139     my ($nav) = @_;
140     my $var= $nav->{Variable};
141     $$var= param($nav->{Param});
142     $$var= $nav->{Default} if !defined $$var;
143     die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
144     push @navsettings, $nav;
145 }
146
147 our $section;
148
149 navsetting({
150     Desc => 'Section',
151     Param => 'section',
152     Variable => \$section,
153     Default => $sections[0],
154     Values => [@sections],
155     Show => sub { return $_[0]; }
156 });
157
158
159 my $gname= param('graph');
160
161 sub num_param ($$$$) {
162     my ($param,$def,$min,$max) = @_;
163     my $v= param($param);
164     return $def if !defined $v;
165     $v =~ m/^([1-9]\d{0,8})$/ or die;
166     $v= $1;
167     die unless $v >= $min && $v <= $max;
168     return $v + 0;
169 }
170
171 if ($gname) {
172     my $g= $graphs{$section,$gname};
173     die unless $g;
174
175     my @args= @{ $g->{Args} };
176
177     my $width= num_param('w',370,100,1600);
178     my $height= num_param('h',200,100,1600);
179
180     my $end= param('end');
181     if (defined $end) {
182         $end =~ m/^(\d+)$/ or die;
183         unshift @args, qw(--end now --start), "end-${end}s";
184     }
185     if (param('debug')) {
186         print((join "\n",@args),"\n"); exit 0;
187     }
188     print "Content-Type: image/png\n\n";
189
190     my $title= $gname;
191     $title .= " $g->{Units}" if $g->{Units};
192     unshift @args, '-t', $title;
193     
194     exec (qw(rrdtool graph - -a PNG --full-size-mode),
195           '-w',$width, '-h',$height,
196           @args);
197     die $!;
198 }
199
200 sub start_page ($) {
201     my ($title) = @_;
202     print header(), start_html($title);
203     my $outerdelim= '';
204     foreach my $nav (@navsettings) {
205         print $outerdelim;
206         print $nav->{Desc}, ": ";
207         my $delim= '';
208         my $current= $nav->{Variable};  $current= $$current;
209         foreach my $couldbe (@{ $nav->{Values} }) {
210             print $delim;
211             my $show= $nav->{Show}($couldbe);
212             if ($couldbe eq $current) {
213                 print "<b>$show</b>";
214             } else {
215                 print "<a href=\"$self";
216                 my $delim2= '?';
217                 foreach my $nav2 (@navsettings) {
218                     my $current2= $nav2->{Variable};  $current2= $$current2;
219                     $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
220                     next if $current2 eq $nav2->{Default};
221                     print $delim2, "$nav2->{Param}=$current2";
222                     $delim2= '&';
223                 }
224                 print "\">$show</a>";
225             }
226             $delim= ' | ';
227         }
228         $outerdelim= "<br>\n";
229     }
230     print "\n";
231
232     print h1("$title");
233 }
234
235 my $detail= param('detail');
236 if ($detail) {
237     my $g= $graphs{$section,$detail};
238     die unless $g;
239     start_page("$detail graphs");
240     foreach my $end (@timeranges[$g->{Slower}..$g->{Slower}+3]) {
241         my $imgurl= "$self?graph=$detail&section=$section&end=$end";
242         print "<a href=\"$imgurl&w=780&h=800\"><img src=\"$imgurl\"></a>\n";
243     }
244     print end_html();
245     exit 0;
246 }
247
248 our $sloth;
249
250 navsetting({
251     Desc => 'Time interval',
252     Param => 'sloth',
253     Variable => \$sloth,
254     Default => 1,
255     Values => [0..2],
256     Show => sub {
257         my ($sl) = @_;
258         return ('Narrower', 'Normal', 'Wider')[$sl];
259     }
260 });
261
262 if (param('debug')) {
263     use Data::Dumper;
264     print Dumper(\%graphs);
265     exit 0;
266 }
267
268 start_page("$section graphs");
269
270 foreach my $gname (@{ $sections{$section} }) {
271     my $g= $graphs{$section,$gname};
272     print "<a href=\"$self?detail=$gname&section=$section\">";
273     my $end= $timeranges[$g->{Slower}+$sloth];
274     my $imgurl= "$self?graph=$gname&section=$section&end=$end";
275     print "<img src=\"$imgurl\"></a>\n";
276 }
277