7 print(header(-status=>500),
15 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
16 our $SELF= '/home/ijackson/things/rrd-graphs';
18 my $self= url(-relative=>1);
20 our (@sections, %section_groups, %group_elems, %graphs);
22 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
24 sub graph_of_group ($$$$$) {
25 my ($section, $group, $elem, $basis, $args) = @_;
26 $basis->{Args}= $args;
27 $basis->{Slower}= 0 unless exists $basis->{Slower};
29 $graphs{$section,$group,$elem}= $basis;
30 if (!exists $group_elems{$section,$group}) {
32 if (!exists $section_groups{$section}) {
34 push @sections, $section;
36 push @{ $section_groups{$section} }, $group;
38 push @{ $group_elems{$section,$group} }, $elem;
42 my ($section, $gname, $basis, $args) = @_;
43 graph_of_group($section, $gname,'', $basis, $args);
46 graph('General', 'Load', { },
48 "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
49 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
50 qw(blocked running stopped paging sleeping zombies)),
51 "AREA:running#88f:running processes:STACK",
52 "AREA:blocked#8f8:blocked processes:STACK",
53 "AREA:paging#f88:paging processes:STACK",
57 graph('General', 'Processes', { },
59 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
60 qw(blocked running stopped paging sleeping zombies)),
61 "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
62 "AREA:sleeping#ccc:sleeping:STACK",
63 "AREA:stopped#00f:stopped:STACK",
64 "AREA:zombies#ff0:zombie:STACK",
65 "AREA:busy#000:busy:STACK",
68 graph('General', 'CPU', { Units => '[%]' },
72 (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
74 "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
75 } qw(idle interrupt nice softirq steal system user wait)),
76 "AREA:system#00f:system:STACK",
77 "AREA:wait#f88:wait:STACK",
78 "AREA:nice#ccc:nice:STACK",
79 "AREA:user#080:user:STACK",
80 "AREA:softirq#f0f:softirq:STACK",
81 "AREA:interrupt#ff0:interrupt:STACK",
82 "AREA:steal#0ff:steal:STACK",
85 graph('General', 'Memory', { },
87 (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
88 qw(free used cached)),
89 (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
90 qw(buffered free used cached)),
91 "CDEF:c_swap_used=0,swap_used,-",
92 "CDEF:c_swap_cached=0,swap_cached,-",
93 "CDEF:c_swap_free=0,swap_free,-",
94 "AREA:c_swap_used#000:used swap",
95 "AREA:c_swap_cached#888:\"cached\" swap:STACK",
96 # "AREA:c_swap_free#88f:free swap:STACK",
97 "AREA:mem_used#ff0:used memory",
98 "AREA:mem_buffered#00f:page cache:STACK",
99 "AREA:mem_cached#008:buffer cache:STACK",
100 "AREA:mem_free#ccc:unused memory:STACK",
103 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
106 ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
107 "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
108 "CDEF:mrx_$_=0,rx_$_,-")
109 } qw(octets packets errors)),
111 ("CDEF:${_}_kb=${_}_octets,1024,/",
112 "CDEF:${_}_errsx=${_}_errors,1000,*")
114 "AREA:tx_kb#080:kby",
115 "LINE:tx_packets#0f0:pkts",
116 "LINE:tx_errsx#000:errs",
117 "AREA:mrx_kb#008:kby",
118 "LINE:mrx_packets#00f:pkts",
119 "LINE:mrx_errsx#444:errs",
122 graph('General', 'Users', { },
124 "DEF:users=$R/users/users.rrd:users:AVERAGE",
125 "LINE:users#008:users"
128 foreach my $src (<$R/df/df-*.rrd>) {
130 $vol =~ s,\.rrd$,, or next;
133 graph('Disk space', $vol, {
138 (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
139 "AREA:used#000:used:STACK",
140 "AREA:free#88f:free:STACK",
144 foreach my $src (<$SELF/data/news/*.rrd>) {
146 $site =~ s,\.rrd$,, or next;
148 $site =~ s,_(in|out)$,,;
150 $site =~ s/^([-.0-9a-z]+)_//;
151 my $us= $1; # all very well but we ignore it
152 graph_of_group("News", $site, $inout,
158 (map { "DEF:$_=$src:$_:AVERAGE" }
159 qw(missing offered deferred unwanted accepted
160 rejected body_missing)),
161 "AREA:accepted#00f:ok",
162 "AREA:body_missing#ff0:missing:STACK",
163 "AREA:rejected#f00:rej:STACK",
164 "AREA:unwanted#bbb:unw:STACK",
165 "AREA:deferred#eee:deferred:STACK",
169 (map { "DEF:$_=$src:$_:AVERAGE" }
170 qw(accepted refused rejected duplicate)),
171 (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
172 "CDEF:kb_$_=bytes_$_,1024,/")
173 } qw(accepted duplicate)),
174 "AREA:accepted#00f:ok:STACK",
175 "AREA:rejected#f00:rej:STACK",
176 "AREA:duplicate#000:dupe:STACK",
177 "AREA:refused#bbb:unw:STACK",
178 "LINE:kb_duplicate#ff0:kb dupe",
179 "LINE:kb_accepted#008:kb",
183 if (param('debug')) {
184 print "Content-Type: text/plain\n\n";
191 my $var= $nav->{Variable};
192 $$var= param($nav->{Param});
193 $$var= $nav->{Default} if !defined $$var;
194 die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
195 push @navsettings, $nav;
203 Variable => \$section,
204 Default => $sections[0],
205 Values => [@sections],
206 Show => sub { return $_[0]; }
210 sub num_param ($$$$) {
211 my ($param,$def,$min,$max) = @_;
212 my $v= param($param);
213 return $def if !defined $v;
214 $v =~ m/^([1-9]\d{0,8})$/ or die;
216 die unless $v >= $min && $v <= $max;
220 my $group= param('graph');
222 my $elem= param('elem');
224 my $g= $graphs{$section,$group,$elem};
227 my @args= @{ $g->{Args} };
229 my $width= num_param('w',370,100,1600);
230 my $height= num_param('h',200,100,1600);
232 my $sloth= param('sloth');
233 die unless defined $sloth;
234 $sloth =~ m/^(\d+)$/ or die;
235 my $end= $timeranges[$sloth];
236 die unless defined $end;
237 unshift @args, qw(--end now --start), "end-${end}s";
239 if (param('debug')) {
240 print((join "\n",@args),"\n"); exit 0;
242 print "Content-Type: image/png\n\n";
245 if (length $elem) { $title.= " $elem"; }
247 $title .= " $g->{Units}" if $g->{Units};
248 unshift @args, '-t', $title;
250 exec (qw(rrdtool graph - -a PNG --full-size-mode),
251 '-w',$width, '-h',$height,
258 print header(), start_html($title);
260 foreach my $nav (@navsettings) {
262 print $nav->{Desc}, ": ";
264 my $current= $nav->{Variable}; $current= $$current;
265 foreach my $couldbe (@{ $nav->{Values} }) {
267 my $show= $nav->{Show}($couldbe);
268 if ($couldbe eq $current) {
269 print "<b>$show</b>";
271 print "<a href=\"$self";
273 foreach my $nav2 (@navsettings) {
274 my $current2= $nav2->{Variable}; $current2= $$current2;
275 $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
276 next if $current2 eq $nav2->{Default};
277 print $delim2, "$nav2->{Param}=$current2";
280 print "\">$show</a>";
284 $outerdelim= "<br>\n";
291 my $detail= param('detail');
293 my $elems= $group_elems{$section,$detail};
295 start_page("$detail graphs");
296 foreach my $xsloth (0..5) {
297 foreach my $elem (@$elems) {
298 my $g= $graphs{$section,$detail,$elem};
300 my $tsloth= $xsloth + $g->{Slower};
301 my $imgurl= "$self?graph=$detail§ion=$section".
302 "&sloth=$tsloth&elem=$elem";
303 print "<a href=\"$imgurl&w=780&h=800\">";
304 print "<img src=\"$imgurl\"></a>\n";
314 Desc => 'Time interval',
321 return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
325 if (param('debug')) {
327 print Dumper(\%graphs);
331 start_page("$section graphs");
333 foreach my $group (@{ $section_groups{$section} }) {
334 print "<a href=\"$self?detail=$group§ion=$section\">";
335 my $imgurl= "$self?graph=$group§ion=$section";
336 my $elems= $group_elems{$section,$group};
337 if (@$elems > 1) { print "<table><tr><td>"; }
338 foreach my $elem (@$elems) {
339 my $g= $graphs{$section,$group,$elem};
340 print "<img src=\"$imgurl&elem=$elem&sloth=".
341 ($sloth + $g->{Slower})."\">";
343 if (@$elems > 1) { print "</td></tr></table>"; }