chiark / gitweb /
show memory
[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       [
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 foreach my $src (<$R/df/df-*.rrd>) {
92     my $vol= $src;
93     $vol =~ s,.*/,,;
94     $vol =~ s,^df-,,;
95     $vol =~ s,\.rrd$,,;
96     graph('Disk space', $vol, {
97              Slower => 1,
98           },
99           [
100            qw(-b 1024 -l 0),
101            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
102            "AREA:used#000:used:STACK",
103            "AREA:free#88f:free:STACK",
104            ]);
105 }
106
107 if (param('debug')) {
108     print "Content-Type: text/plain\n\n";
109 }
110
111 our @navsettings;
112
113 sub navsetting ($) {
114     my ($nav) = @_;
115     my $var= $nav->{Variable};
116     $$var= param($nav->{Param});
117     $$var= $nav->{Default} if !defined $$var;
118     die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
119     push @navsettings, $nav;
120 }
121
122 our $section;
123
124 navsetting({
125     Desc => 'Section',
126     Param => 'section',
127     Variable => \$section,
128     Default => $sections[0],
129     Values => [@sections],
130     Show => sub { return $_[0]; }
131 });
132
133
134 my $gname= param('graph');
135
136 if ($gname) {
137     my $g= $graphs{$section,$gname};
138     die unless $g;
139
140     my @args= @{ $g->{Args} };
141
142     my $end= param('end');
143     if (defined $end) {
144         $end =~ m/^(\d+)$/ or die;
145         unshift @args, qw(--end now --start), "end-${end}s";
146     }
147     if (param('debug')) {
148         print((join "\n",@args),"\n"); exit 0;
149     }
150     print "Content-Type: image/png\n\n";
151
152     my $title= $gname;
153     $title .= " $g->{Units}" if $g->{Units};
154     unshift @args, '-t', $title;
155     
156     exec qw(rrdtool graph - -a PNG --full-size-mode -w 370 -h 200), @args;
157     die $!;
158 }
159
160 sub start_page ($) {
161     my ($title) = @_;
162     print header(), start_html($title);
163     my $outerdelim= '';
164     foreach my $nav (@navsettings) {
165         print $outerdelim;
166         print $nav->{Desc}, ": ";
167         my $delim= '';
168         my $current= $nav->{Variable};  $current= $$current;
169         foreach my $couldbe (@{ $nav->{Values} }) {
170             print $delim;
171             my $show= $nav->{Show}($couldbe);
172             if ($couldbe eq $current) {
173                 print "<b>$show</b>";
174             } else {
175                 print "<a href=\"$self";
176                 my $delim2= '?';
177                 foreach my $nav2 (@navsettings) {
178                     my $current2= $nav2->{Variable};  $current2= $$current2;
179                     $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
180                     next if $current2 eq $nav2->{Default};
181                     print $delim2, "$nav2->{Param}=$current2";
182                     $delim2= '&';
183                 }
184                 print "\">$show</a>";
185             }
186             $delim= ' | ';
187         }
188         $outerdelim= "<br>\n";
189     }
190     print "\n";
191
192     print h1("$title");
193 }
194
195 my $detail= param('detail');
196 if ($detail) {
197     my $g= $graphs{$section,$detail};
198     die unless $g;
199     start_page("$detail graphs");
200     foreach my $end (@timeranges[$g->{Slower}..$g->{Slower}+3]) {
201         print "<img src=\"$self?graph=$detail&section=$section&end=$end\">\n";
202     }
203     print end_html();
204     exit 0;
205 }
206
207 our $sloth;
208
209 navsetting({
210     Desc => 'Time interval',
211     Param => 'sloth',
212     Variable => \$sloth,
213     Default => 1,
214     Values => [0..2],
215     Show => sub {
216         my ($sl) = @_;
217         return ('Narrower', 'Normal', 'Wider')[$sl];
218     }
219 });
220
221 if (param('debug')) {
222     use Data::Dumper;
223     print Dumper(\%graphs);
224     exit 0;
225 }
226
227 start_page("$section graphs");
228
229 foreach my $gname (@{ $sections{$section} }) {
230     my $g= $graphs{$section,$gname};
231     print "<a href=\"$self?detail=$gname&section=$section\">";
232     my $end= $timeranges[$g->{Slower}+$sloth];
233     my $imgurl= "$self?graph=$gname&section=$section&end=$end";
234     print "<img src=\"$imgurl\"></a>\n";
235 }
236