4 use CGI qw/:standard -no_xhtml/;
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 and processes', { },
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:STACK",
52 "AREA:blocked#8f8:disk wait:STACK",
53 "AREA:paging#f88:paging: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 "CDEF:allintr=softirq,steal,+,interrupt,+",
77 "AREA:system#88f:system:STACK",
78 "AREA:allintr#ff0:interrupt:STACK",
79 "AREA:user#00f:user:STACK",
80 "AREA:nice#ccc:nice:STACK",
81 "AREA:wait#f00:wait:STACK",
84 graph('General', 'Memory', { },
86 (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
87 qw(free used cached)),
88 (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
89 qw(buffered free used cached)),
90 "CDEF:c_swap_used=0,swap_used,-",
91 "CDEF:c_swap_cached=0,swap_cached,-",
92 "CDEF:c_swap_free=0,swap_free,-",
93 "AREA:c_swap_used#000:used swap",
94 "AREA:c_swap_cached#888:\"cached\" swap:STACK",
95 # "AREA:c_swap_free#88f:free swap:STACK",
96 "AREA:mem_used#ff0:used memory",
97 "AREA:mem_buffered#00f:page cache:STACK",
98 "AREA:mem_cached#008:buffer cache:STACK",
99 "AREA:mem_free#ccc:unused memory:STACK",
102 graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
105 ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
106 "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
107 "CDEF:mrx_$_=0,rx_$_,-")
108 } qw(octets packets errors)),
110 ("CDEF:${_}_kb=${_}_octets,1024,/",
111 "CDEF:${_}_errsx=${_}_errors,1000,*")
113 "AREA:tx_kb#080:kby",
114 "LINE:tx_packets#0f0:pkts",
115 "LINE:tx_errsx#000:errs",
116 "AREA:mrx_kb#008:kby",
117 "LINE:mrx_packets#00f:pkts",
118 "LINE:mrx_errsx#444:errs",
121 graph('General', 'Users', { },
123 "DEF:users=$R/users/users.rrd:users:AVERAGE",
124 "LINE:users#008:users"
127 foreach my $src (<$R/df/df-*.rrd>) {
129 $vol =~ s,\.rrd$,, or next;
132 graph('Disk space', $vol, {
137 (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
138 "AREA:used#000:used:STACK",
139 "AREA:free#88f:free:STACK",
145 if (!open NM, '<', "$SELF/data/news/name-map") {
146 die unless $!==&ENOENT;
150 next unless m/^[^\#]/;
151 m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
153 $news_name_map{$1,$_}= $3 foreach qw(in out);
155 $news_name_map{$1,$2}= $3;
160 sub news_name_sortkey {
161 return join '.', reverse split /\./, $_[0];
164 foreach my $src (sort { news_name_sortkey($a) cmp news_name_sortkey($b) }
165 <$SELF/data/news/*.rrd>) {
167 $site =~ s,\.rrd$,, or next;
169 $site =~ s,_(in|out)$,,;
171 $site =~ s/^([-.0-9a-z]+)_//;
172 my $us= $1; # all very well but we ignore it
173 my $newsite= $news_name_map{$site,$inout};
174 $site= $newsite if defined $newsite;
175 next if $site eq '-';
176 graph_of_group("News", $site, $inout,
182 (map { "DEF:$_=$src:$_:AVERAGE" }
183 qw(missing deferred unwanted accepted rejected body_missing)),
184 "AREA:accepted#00f:ok",
185 "AREA:body_missing#ff0:miss:STACK",
186 "AREA:rejected#f00:rej:STACK",
187 "AREA:unwanted#aaa:unw:STACK",
188 "AREA:deferred#ddd:defer:STACK",
191 (map { "DEF:$_=$src:$_:AVERAGE" }
192 qw(accepted refused rejected duplicate)),
193 (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
194 "CDEF:kb_$_=bytes_$_,1024,/")
195 } qw(accepted duplicate)),
196 "AREA:accepted#00f:ok:STACK",
197 "AREA:rejected#f00:rej:STACK",
198 "AREA:duplicate#000:dupe:STACK",
199 "AREA:refused#aaa:unw:STACK",
200 "LINE:kb_duplicate#ff0:kb dupe",
201 "LINE:kb_accepted#008:kb",
205 if (param('debug')) {
206 print "Content-Type: text/plain\n\n";
213 my $var= $nav->{Variable};
214 $$var= param($nav->{Param});
215 $$var= $nav->{Default} if !defined $$var;
216 die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
217 push @navsettings, $nav;
225 Variable => \$section,
226 Default => $sections[0],
227 Values => [@sections],
228 Show => sub { return $_[0]; }
232 sub num_param ($$$$) {
233 my ($param,$def,$min,$max) = @_;
234 my $v= param($param);
235 return $def if !defined $v;
236 $v =~ m/^([1-9]\d{0,8})$/ or die;
238 die unless $v >= $min && $v <= $max;
242 my $group= param('graph');
244 my $elem= param('elem');
246 my $g= $graphs{$section,$group,$elem};
249 my @args= @{ $g->{Args} };
251 my $width= num_param('w',370,100,1600);
252 my $height= num_param('h',200,100,1600);
254 my $sloth= param('sloth');
255 die unless defined $sloth;
256 $sloth =~ m/^(\d+)$/ or die;
257 my $end= $timeranges[$sloth];
258 die unless defined $end;
259 unshift @args, qw(--end now --start), "end-${end}s";
261 if (param('debug')) {
262 print((join "\n",@args),"\n"); exit 0;
264 print "Content-Type: image/png\n\n";
267 if (length $elem) { $title.= " $elem"; }
269 $title .= " $g->{Units}" if $g->{Units};
270 unshift @args, '-t', $title;
272 exec (qw(rrdtool graph - -a PNG --full-size-mode),
273 '-w',$width, '-h',$height,
280 print header(), start_html($title);
282 foreach my $nav (@navsettings) {
284 print $nav->{Desc}, ": ";
286 my $current= $nav->{Variable}; $current= $$current;
287 foreach my $couldbe (@{ $nav->{Values} }) {
289 my $show= $nav->{Show}($couldbe);
290 if ($couldbe eq $current) {
291 print "<b>$show</b>";
295 foreach my $nav2 (@navsettings) {
296 my $current2= $nav2->{Variable}; $current2= $$current2;
297 $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
298 next if $current2 eq $nav2->{Default};
299 $u .= $delim2; $u .= "$nav2->{Param}=$current2";
302 print a({href=>$u}, $show);
306 $outerdelim= "<br>\n";
313 my $detail= param('detail');
315 my $elems= $group_elems{$section,$detail};
317 start_page("$detail graphs");
318 foreach my $xsloth (0..5) {
319 foreach my $elem (@$elems) {
320 my $g= $graphs{$section,$detail,$elem};
322 my $tsloth= $xsloth + $g->{Slower};
323 my $imgurl= "$self?graph=$detail§ion=$section".
324 "&sloth=$tsloth&elem=$elem";
325 print a({href=>"$imgurl&w=780&h=800"},
326 img({src=>$imgurl, alt=>''}));
336 Desc => 'Time interval',
343 return ('Narrower', 'Normal', 'Wider', 'Extra wide')[$sl];
347 if (param('debug')) {
349 print Dumper(\%graphs);
353 start_page("$section graphs");
355 foreach my $group (@{ $section_groups{$section} }) {
356 print a({href=>"$self?detail=$group§ion=$section"});
357 my $imgurl= "$self?graph=$group§ion=$section";
358 print "<span style=\"white-space:nowrap\">";
359 my $elems= $group_elems{$section,$group};
360 foreach my $elem (@$elems) {
361 my $g= $graphs{$section,$group,$elem};
362 print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),