1 #!/usr/bin/speedy -w -- -t100 -M1
4 use CGI::SpeedyCGI qw/:standard -no_xhtml/;
5 use CGI qw/:standard -no_xhtml/;
8 print(header(-status=>500),
16 our (@sections, %section_groups, %group_elems, %graphs);
18 #---------- initialisation code, run once - graphs setup ----------
22 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
23 our $SELF= '/home/ijackson/things/rrd-graphs';
25 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
27 sub graph_of_group ($$$$$) {
28 my ($section, $group, $elem, $basis, $args) = @_;
29 $basis->{Args}= $args;
30 $basis->{Slower}= 0 unless exists $basis->{Slower};
31 $basis->{TimeRanges} ||= \@timeranges;
32 $graphs{$section,$group,$elem}= $basis;
33 if (!exists $group_elems{$section,$group}) {
35 if (!exists $section_groups{$section}) {
37 push @sections, $section;
39 push @{ $section_groups{$section} }, $group;
41 push @{ $group_elems{$section,$group} }, $elem;
45 my ($section, $gname, $basis, $args) = @_;
46 graph_of_group($section, $gname,'', $basis, $args);
49 graph('General', 'Load and processes', { },
51 "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
52 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
53 qw(blocked running stopped paging sleeping zombies)),
54 "AREA:running#88f:running:STACK",
55 "AREA:blocked#8f8:disk wait:STACK",
56 "AREA:paging#f88:paging:STACK",
60 graph('General', 'Processes', { },
62 (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
63 qw(blocked running stopped paging sleeping zombies)),
64 "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
65 "AREA:sleeping#ccc:sleeping:STACK",
66 "AREA:stopped#00f:stopped:STACK",
67 "AREA:zombies#ff0:zombie:STACK",
68 "AREA:busy#000:busy:STACK",
71 graph('General', 'CPU', { Units => '[%]' },
75 (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
77 "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
78 } qw(idle interrupt nice softirq steal system user wait)),
79 "CDEF:allintr=softirq,steal,+,interrupt,+",
80 "AREA:allintr#ff0:interrupt:STACK",
81 "AREA:system#88f:system:STACK",
82 "AREA:user#00f:user:STACK",
83 "AREA:nice#ccc:nice:STACK",
84 "AREA:wait#f00:wait:STACK",
87 graph('General', 'Memory', { },
89 (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
90 qw(free used cached)),
91 (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
92 qw(buffered free used cached)),
93 "CDEF:c_swap_used=0,swap_used,-",
94 "CDEF:c_swap_cached=0,swap_cached,-",
95 "CDEF:c_swap_free=0,swap_free,-",
96 "AREA:c_swap_used#000:used swap",
97 "AREA:c_swap_cached#888:\"cached\" swap:STACK",
98 # "AREA:c_swap_free#88f:free swap:STACK",
99 "AREA:mem_used#ff0:used memory",
100 "AREA:mem_buffered#00f:page cache:STACK",
101 "AREA:mem_cached#008:buffer cache:STACK",
102 "AREA:mem_free#ccc:unused memory:STACK",
105 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
108 ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
109 "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
110 "CDEF:mrx_$_=0,rx_$_,-")
111 } qw(octets packets errors)),
113 ("CDEF:${_}_kb=${_}_octets,1024,/",
114 "CDEF:${_}_errsx=${_}_errors,1000,*")
116 "AREA:tx_kb#080:kby",
117 "LINE:tx_packets#0f0:pkts",
118 "LINE:tx_errsx#000:errs",
119 "AREA:mrx_kb#008:kby",
120 "LINE:mrx_packets#00f:pkts",
121 "LINE:mrx_errsx#444:errs",
124 graph('General', 'Users', { },
126 "DEF:users=$R/users/users.rrd:users:AVERAGE",
127 "LINE:users#008:users"
130 foreach my $src (<$R/df/df-*.rrd>) {
132 $vol =~ s,\.rrd$,, or next;
135 graph('Disk space', $vol, {
140 (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
141 "AREA:used#000:used:STACK",
142 "AREA:free#88f:free:STACK",
148 if (!open NM, '<', "$SELF/data/news/name-map") {
149 die unless $!==&ENOENT;
153 next unless m/^[^\#]/;
154 m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
156 $news_name_map{$1,$_}= $3 foreach qw(in out);
158 $news_name_map{$1,$2}= $3;
163 sub news_name_sortkey {
164 return join '.', reverse split /\./, $_[0];
167 foreach my $src (sort { news_name_sortkey($a) cmp news_name_sortkey($b) }
168 <$SELF/data/news/*.rrd>) {
170 $site =~ s,\.rrd$,, or next;
172 $site =~ s,_(in|out)$,,;
174 $site =~ s/^([-.0-9a-z]+)_//;
175 my $us= $1; # all very well but we ignore it
176 my $newsite= $news_name_map{$site,$inout};
177 $site= $newsite if defined $newsite;
178 next if $site eq '-';
179 graph_of_group("News", $site, $inout,
182 TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
185 (map { "DEF:$_=$src:$_:AVERAGE" }
186 qw(missing deferred unwanted accepted rejected body_missing)),
187 "AREA:accepted#00f:ok",
188 "AREA:body_missing#ff0:miss:STACK",
189 "AREA:rejected#f00:rej:STACK",
190 "AREA:unwanted#aaa:unw:STACK",
191 "AREA:deferred#ddd:defer:STACK",
194 (map { "DEF:$_=$src:$_:AVERAGE" }
195 qw(accepted refused rejected duplicate)),
196 (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
197 "CDEF:kb_$_=bytes_$_,1024,/")
198 } qw(accepted duplicate)),
199 "AREA:accepted#00f:ok:STACK",
200 "AREA:rejected#f00:rej:STACK",
201 "AREA:duplicate#000:dupe:STACK",
202 "AREA:refused#aaa:unw:STACK",
203 "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
204 "LINE:kb_duplicate#ff0:kb dupe",
205 "LINE:kb_accepted_smooth#008:~kb",
211 #---------- right, that was the initialisation ----------
213 our $self= url(-relative=>1);
215 if (param('debug')) {
216 print "Content-Type: text/plain\n\n";
225 my $var= $nav->{Variable};
226 $$var= param($nav->{Param});
227 $$var= $nav->{Default} if !defined $$var;
228 die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
229 push @navsettings, $nav;
237 Variable => \$section,
238 Default => $sections[0],
239 Values => [@sections],
240 Show => sub { return $_[0]; }
244 sub num_param ($$$$) {
245 my ($param,$def,$min,$max) = @_;
246 my $v= param($param);
247 return $def if !defined $v;
248 $v =~ m/^([1-9]\d{0,8})$/ or die;
250 die unless $v >= $min && $v <= $max;
254 our $group= param('graph');
256 our $elem= param('elem');
258 my $g= $graphs{$section,$group,$elem};
261 my $width= num_param('w',370,100,1600);
262 my $height= num_param('h',200,100,1600);
264 my $sloth= param('sloth');
265 die unless defined $sloth;
266 $sloth =~ m/^(\d+)$/ or die;
268 my $end= $g->{TimeRanges}[$sloth];
269 die unless defined $end;
271 my $cacheid= "$section!$group!$elem!$sloth!$width!$height";
272 my $cachepath= "cache/$cacheid.png";
274 my @args= @{ $g->{Args} };
275 s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
276 unshift @args, qw(--end now --start), "end-${end}s";
279 if (length $elem) { $title.= " $elem"; }
281 $title .= " $g->{Units}" if $g->{Units};
282 unshift @args, '-t', $title, '-w',$width, '-h',$height;
283 unshift @args, qw(-a PNG --full-size-mode);
285 if (param('debug')) {
286 print((join "\n",@args),"\n"); exit 0;
289 #print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
290 exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
292 rrdtool graph "$p" --lazy "$@" >/dev/null
293 printf "Content-Type: image/png\n\n"
301 print header(), start_html($title);
303 foreach my $nav (@navsettings) {
305 print $nav->{Desc}, ": ";
307 my $current= $nav->{Variable}; $current= $$current;
308 foreach my $couldbe (@{ $nav->{Values} }) {
310 my $show= $nav->{Show}($couldbe);
311 if ($couldbe eq $current) {
312 print "<b>$show</b>";
316 foreach my $nav2 (@navsettings) {
317 my $current2= $nav2->{Variable}; $current2= $$current2;
318 $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
319 next if $current2 eq $nav2->{Default};
320 $u .= $delim2; $u .= "$nav2->{Param}=$current2";
323 print a({href=>$u}, $show);
327 $outerdelim= "<br>\n";
334 our $detail= param('detail');
336 my $elems= $group_elems{$section,$detail};
338 start_page("$detail graphs");
339 foreach my $tsloth (0..5) {
340 foreach my $elem (@$elems) {
341 my $g= $graphs{$section,$detail,$elem};
343 next if $tsloth >= @{ $g->{TimeRanges} };
344 my $imgurl= "$self?graph=$detail§ion=$section".
345 "&sloth=$tsloth&elem=$elem";
346 print a({href=>"$imgurl&w=780&h=800"},
347 img({src=>$imgurl, alt=>''}));
357 Desc => 'Time interval',
364 return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
368 if (param('debug')) {
370 print Dumper(\%graphs);
374 start_page("$section graphs");
376 foreach my $group (@{ $section_groups{$section} }) {
377 print a({href=>"$self?detail=$group§ion=$section"});
378 my $imgurl= "$self?graph=$group§ion=$section";
379 print "<span style=\"white-space:nowrap\">";
380 my $elems= $group_elems{$section,$group};
381 foreach my $elem (@$elems) {
382 my $g= $graphs{$section,$group,$elem};
383 print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),