chiark / gitweb /
disk space
[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 sub graph ($$$$) {
22     my ($section, $gname, $basis, $args) = @_;
23     $basis->{Args}= $args;
24     $graphs{$section,$gname}= $basis;
25     if (!exists $sections{$section}) {
26         push @sections, $section;
27     }
28     push @{ $sections{$section} }, $gname;
29 }
30
31 graph('General', 'Load', { },
32       [
33        "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
34        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
35             qw(blocked running stopped paging sleeping zombies)),
36        "AREA:running#88f:running processes:STACK",
37        "AREA:blocked#8f8:blocked processes:STACK",
38        "AREA:paging#f88:paging processes:STACK",
39        "LINE:load#000:load",
40        ]);
41
42 graph('General', 'Processes', { },
43       [
44        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
45             qw(blocked running stopped paging sleeping zombies)),
46        "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
47        "AREA:sleeping#ccc:sleeping:STACK",
48        "AREA:stopped#f00:stopped:STACK",
49        "AREA:zombies#0f0:zombie:STACK",
50        "AREA:busy#f00:busy:STACK",
51        ]);
52
53 graph('General', 'CPU', { Units => '[%]' },
54       [
55        (map {
56            my $thing= $_;
57            (map { "DEF:$thing$_=$R/cpu-$_/cpu-$thing.rrd:value:AVERAGE" }
58                 (0..7)),
59            "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
60        } qw(idle interrupt nice softirq steal system user wait)),
61        "AREA:system#00f:system:STACK",
62        "AREA:wait#f88:wait:STACK",
63        "AREA:nice#ccc:nice:STACK",
64        "AREA:user#080:user:STACK",
65        "AREA:softirq#f0f:softirq:STACK",
66        "AREA:interrupt#ff0:interrupt:STACK",
67        "AREA:steal#0ff:steal:STACK",
68        ]);
69
70 foreach my $src (<$R/df/df-*.rrd>) {
71     my $vol= $src;
72     $vol =~ s,.*/,,;
73     $vol =~ s,^df-,,;
74     $vol =~ s,\.rrd$,,;
75     graph('Disk space', $vol, { },
76           [
77            qw(-b 1024 -l 0),
78            (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
79            "AREA:used#000:used:STACK",
80            "AREA:free#88f:free:STACK",
81            ]);
82 }
83
84 if (param('debug')) {
85     print "Content-Type: text/plain\n\n";
86 }
87
88 my $gname= param('graph');
89 my $section= param('section');
90 $section ||= $sections[0];
91 die unless $sections{$section};
92
93 if ($gname) {
94     my $g= $graphs{$section,$gname};
95     die unless $g;
96
97     my @args= @{ $g->{Args} };
98
99     my $end= param('end');
100     if (defined $end) {
101         $end =~ m/^(\d+)$/ or die;
102         unshift @args, qw(--end now --start), "end-${end}s";
103     }
104     if (param('debug')) {
105         print((join "\n",@args),"\n"); exit 0;
106     }
107     print "Content-Type: image/png\n\n";
108
109     my $title= $gname;
110     $title .= " $g->{Units}" if $g->{Units};
111     unshift @args, '-t', $title;
112     
113     exec qw(rrdtool graph - -a PNG --full-size-mode -w 370 -h 200), @args;
114     die $!;
115 }
116
117 sub start_page ($) {
118     my ($title) = @_;
119     print header(), start_html($title);
120     my $delim= '';
121     foreach my $s2 (@sections) {
122         print $delim;
123         if ($s2 eq $section) {
124             print "<b>$section</b>";
125         } else {
126             print "<a href=\"$self";
127             if ($s2 ne $sections[0]) { print "?section=$s2"; }
128             print "\">$s2</a>";
129         }
130         $delim= ' | ';
131     }
132     print h1("$title");
133 }
134
135 my $detail= param('detail');
136 if ($detail) {
137     die unless $graphs{$section,$detail};
138     start_page("$detail graphs");
139     foreach my $end (qw(3600 86400 604800 2419200)) {
140         print "<img src=\"$self?graph=$detail&section=$section&end=$end\">\n";
141     }
142     print end_html();
143     exit 0;
144 }
145
146 if (param('debug')) {
147     use Data::Dumper;
148     print Dumper(\%graphs);
149     exit 0;
150 }
151
152 start_page("$section graphs");
153
154 foreach my $gname (@{ $sections{$section} }) {
155     print "<a href=\"$self?detail=$gname&section=$section\">"; #,h2($gname),"</a>";
156     print "<img src=\"$self?graph=$gname&section=$section\"></a>\n";
157 }
158