chiark / gitweb /
shows the news graphs
[rrd-graphs.git] / cgi
diff --git a/cgi b/cgi
index 3c0e124df4dffdba101d6a8b3c066f77d2c3f926..aed4059081c9c9490e56305f7fac382c28901cfe 100755 (executable)
--- a/cgi
+++ b/cgi
@@ -13,22 +13,34 @@ sub fail ($) {
 }
 
 our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
+our $SELF= '/home/ijackson/things/rrd-graphs';
 
 my $self= url(-relative=>1);
 
-our (@sections, %sections, %graphs);
+our (@sections, %section_groups, %group_elems, %graphs);
 
 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1);
 
-sub graph ($$$$) {
-    my ($section, $gname, $basis, $args) = @_;
+sub graph_of_group ($$$$$) {
+    my ($section, $group, $elem, $basis, $args) = @_;
     $basis->{Args}= $args;
     $basis->{Slower}= 0 unless exists $basis->{Slower};
-    $graphs{$section,$gname}= $basis;
-    if (!exists $sections{$section}) {
-       push @sections, $section;
+
+    $graphs{$section,$group,$elem}= $basis;
+    if (!exists $group_elems{$section,$group}) {
+       # new group then
+       if (!exists $section_groups{$section}) {
+           # new section even
+           push @sections, $section;
+       }
+       push @{ $section_groups{$section} }, $group;
     }
-    push @{ $sections{$section} }, $gname;
+    push @{ $group_elems{$section,$group} }, $elem;
+}
+
+sub graph ($$$$) {
+    my ($section, $gname, $basis, $args) = @_;
+    graph_of_group($section, $gname,'', $basis, $args);
 }
 
 graph('General', 'Load', { },
@@ -48,9 +60,9 @@ graph('General', 'Processes', { },
            qw(blocked running stopped paging sleeping zombies)),
        "CDEF:busy=0".(join '', map { ",$_,+" } qw(running blocked paging)),
        "AREA:sleeping#ccc:sleeping:STACK",
-       "AREA:stopped#f00:stopped:STACK",
-       "AREA:zombies#0f0:zombie:STACK",
-       "AREA:busy#f00:busy:STACK",
+       "AREA:stopped#00f:stopped:STACK",
+       "AREA:zombies#ff0:zombie:STACK",
+       "AREA:busy#000:busy:STACK",
        ]);
 
 graph('General', 'CPU', { Units => '[%]' },
@@ -70,6 +82,49 @@ graph('General', 'CPU', { Units => '[%]' },
        "AREA:steal#0ff:steal:STACK",
        ]);
 
+graph('General', 'Memory', { },
+      [ '-b',1024,
+       (map { "DEF:swap_$_=$R/swap/swap-$_.rrd:value:AVERAGE" }
+           qw(free used cached)),
+       (map { "DEF:mem_$_=$R/memory/memory-$_.rrd:value:AVERAGE" }
+           qw(buffered free used cached)),
+       "CDEF:c_swap_used=0,swap_used,-",
+       "CDEF:c_swap_cached=0,swap_cached,-",
+       "CDEF:c_swap_free=0,swap_free,-",
+       "AREA:c_swap_used#000:used swap",
+       "AREA:c_swap_cached#888:\"cached\" swap:STACK",
+#       "AREA:c_swap_free#88f:free swap:STACK",
+       "AREA:mem_used#ff0:used memory",
+       "AREA:mem_buffered#00f:page cache:STACK",
+       "AREA:mem_cached#008:buffer cache:STACK",
+       "AREA:mem_free#ccc:unused memory:STACK",
+       ]);
+
+graph('General', 'Network', { Units => '[/sec; tx +ve; errs x1000]' },
+      [
+       (map {
+          ("DEF:tx_$_=$R/interface/if_$_-eth0.rrd:tx:AVERAGE",
+           "DEF:rx_$_=$R/interface/if_$_-eth0.rrd:rx:AVERAGE",
+           "CDEF:mrx_$_=0,rx_$_,-")
+          } qw(octets packets errors)),
+       (map {
+          ("CDEF:${_}_kb=${_}_octets,1024,/",
+           "CDEF:${_}_errsx=${_}_errors,1000,*")
+          } qw(mrx tx)),
+       "AREA:tx_kb#080:kby",
+       "LINE:tx_packets#0f0:pkts",
+       "LINE:tx_errsx#000:errs",
+       "AREA:mrx_kb#008:kby",
+       "LINE:mrx_packets#00f:pkts",
+       "LINE:mrx_errsx#444:errs",
+      ]);
+
+graph('General', 'Users', {  },
+      [
+       "DEF:users=$R/users/users.rrd:users:AVERAGE",
+       "LINE:users#008:users"
+       ]);
+
 foreach my $src (<$R/df/df-*.rrd>) {
     my $vol= $src;
     $vol =~ s,.*/,,;
@@ -78,7 +133,7 @@ foreach my $src (<$R/df/df-*.rrd>) {
     graph('Disk space', $vol, {
             Slower => 1,
           },
-         [
+         [ '-A','-l',0,'-r',
           qw(-b 1024 -l 0),
           (map { "DEF:$_=$src:$_:AVERAGE" } qw(free used)),
           "AREA:used#000:used:STACK",
@@ -86,69 +141,185 @@ foreach my $src (<$R/df/df-*.rrd>) {
           ]);
 }
 
+foreach my $src (<$SELF/news-stats/*.rrd>) {
+    my $site= $src;
+    $site =~ s,.*/,,;
+    $site =~ s,\.rrd$,,;
+    $site =~ s,_(in|out)$,,;
+    my $inout= $1;
+    $site =~ s/^([-.0-9a-z]+)_//;
+    my $us= $1; # all very well but we ignore it
+    graph_of_group("News", $site, $inout,
+         {
+               Slower => 1,
+           }, $inout eq 'out' ?
+         [
+          (map { "DEF:$_=$src:$_:AVERAGE" }
+               qw(missing offered deferred unwanted accepted
+                  rejected body_missing)),
+          "AREA:accepted#00f:ok",
+          "AREA:body_missing#ff0:missing:STACK",
+          "AREA:rejected#f00:rej:STACK",
+          "AREA:unwanted#bbb:unw:STACK",
+          "AREA:deferred#eee:deferred:STACK",
+          "LINE:offered#080:",
+          ] :
+         [
+          (map { "DEF:$_=$src:$_:AVERAGE" }
+               qw(accepted refused rejected duplicate)),
+          (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
+                  "CDEF:kb_$_=bytes_$_,1024,/")
+             } qw(accepted duplicate)),
+          "AREA:accepted#00f:ok:STACK",
+          "AREA:rejected#f00:rej:STACK",
+          "AREA:duplicate#000:dupe:STACK",
+          "AREA:refused#bbb:unw:STACK",
+          "LINE:kb_duplicate#ff0:kb dupe",
+          "LINE:kb_accepted#008:kb",
+          ]);
+}
+
 if (param('debug')) {
     print "Content-Type: text/plain\n\n";
 }
 
-my $gname= param('graph');
-my $section= param('section');
-$section ||= $sections[0];
-die unless $sections{$section};
+our @navsettings;
+
+sub navsetting ($) {
+    my ($nav) = @_;
+    my $var= $nav->{Variable};
+    $$var= param($nav->{Param});
+    $$var= $nav->{Default} if !defined $$var;
+    die $nav->{Param} unless grep { $_ eq $$var } @{ $nav->{Values} };
+    push @navsettings, $nav;
+}
+
+our $section;
+
+navsetting({
+    Desc => 'Section',
+    Param => 'section',
+    Variable => \$section,
+    Default => $sections[0],
+    Values => [@sections],
+    Show => sub { return $_[0]; }
+});
 
-if ($gname) {
-    my $g= $graphs{$section,$gname};
+
+sub num_param ($$$$) {
+    my ($param,$def,$min,$max) = @_;
+    my $v= param($param);
+    return $def if !defined $v;
+    $v =~ m/^([1-9]\d{0,8})$/ or die;
+    $v= $1;
+    die unless $v >= $min && $v <= $max;
+    return $v + 0;
+}
+
+my $group= param('graph');
+
+my $elem= param('elem');
+if (defined $elem) {
+    my $g= $graphs{$section,$group,$elem};
     die unless $g;
 
     my @args= @{ $g->{Args} };
 
-    my $end= param('end');
-    if (defined $end) {
-        $end =~ m/^(\d+)$/ or die;
-        unshift @args, qw(--end now --start), "end-${end}s";
-    }
+    my $width= num_param('w',370,100,1600);
+    my $height= num_param('h',200,100,1600);
+
+    my $sloth= param('sloth');
+    die unless defined $sloth;
+    $sloth =~ m/^(\d+)$/ or die;
+    my $end= $timeranges[$sloth];
+    die unless defined $end;
+    unshift @args, qw(--end now --start), "end-${end}s";
+    
     if (param('debug')) {
         print((join "\n",@args),"\n"); exit 0;
     }
     print "Content-Type: image/png\n\n";
 
-    my $title= $gname;
+    my $title= $group;
+    if (length $elem) { $title.= " $elem"; }
+
     $title .= " $g->{Units}" if $g->{Units};
     unshift @args, '-t', $title;
     
-    exec qw(rrdtool graph - -a PNG --full-size-mode -w 370 -h 200), @args;
+    exec (qw(rrdtool graph - -a PNG --full-size-mode),
+         '-w',$width, '-h',$height,
+         @args);
     die $!;
 }
 
 sub start_page ($) {
     my ($title) = @_;
     print header(), start_html($title);
-    my $delim= '';
-    foreach my $s2 (@sections) {
-       print $delim;
-       if ($s2 eq $section) {
-           print "<b>$section</b>";
-       } else {
-           print "<a href=\"$self";
-           if ($s2 ne $sections[0]) { print "?section=$s2"; }
-           print "\">$s2</a>";
+    my $outerdelim= '';
+    foreach my $nav (@navsettings) {
+       print $outerdelim;
+       print $nav->{Desc}, ": ";
+       my $delim= '';
+       my $current= $nav->{Variable};  $current= $$current;
+       foreach my $couldbe (@{ $nav->{Values} }) {
+           print $delim;
+           my $show= $nav->{Show}($couldbe);
+           if ($couldbe eq $current) {
+               print "<b>$show</b>";
+           } else {
+               print "<a href=\"$self";
+               my $delim2= '?';
+               foreach my $nav2 (@navsettings) {
+                   my $current2= $nav2->{Variable};  $current2= $$current2;
+                   $current2= $couldbe if $nav2->{Param} eq $nav->{Param};
+                   next if $current2 eq $nav2->{Default};
+                   print $delim2, "$nav2->{Param}=$current2";
+                   $delim2= '&';
+               }
+               print "\">$show</a>";
+           }
+           $delim= ' | ';
        }
-       $delim= ' | ';
+       $outerdelim= "<br>\n";
     }
+    print "\n";
+
     print h1("$title");
 }
 
 my $detail= param('detail');
 if ($detail) {
-    my $g= $graphs{$section,$detail};
-    die unless $g;
+    my $elems= $group_elems{$section,$detail};
+    die unless $elems;
     start_page("$detail graphs");
-    foreach my $end (@timeranges[$g->{Slower}..$g->{Slower}+3]) {
-        print "<img src=\"$self?graph=$detail&section=$section&end=$end\">\n";
+    foreach my $elem (@$elems) {
+       my $g= $graphs{$section,$detail,$elem};
+       die unless $g;
+       foreach my $tsloth ($g->{Slower}..$g->{Slower}+3) {
+           my $imgurl= "$self?graph=$detail&section=$section".
+               "&sloth=$tsloth&elem=$elem";
+           print "<a href=\"$imgurl&w=780&h=800\">";
+           print "<img src=\"$imgurl\"></a>\n";
+       }
     }
     print end_html();
     exit 0;
 }
 
+our $sloth;
+
+navsetting({
+    Desc => 'Time interval',
+    Param => 'sloth',
+    Variable => \$sloth,
+    Default => 1,
+    Values => [0..2],
+    Show => sub {
+       my ($sl) = @_;
+       return ('Narrower', 'Normal', 'Wider')[$sl];
+    }
+});
+
 if (param('debug')) {
     use Data::Dumper;
     print Dumper(\%graphs);
@@ -157,11 +328,17 @@ if (param('debug')) {
 
 start_page("$section graphs");
 
-foreach my $gname (@{ $sections{$section} }) {
-    my $g= $graphs{$section,$gname};
-    print "<a href=\"$self?detail=$gname&section=$section\">";
-    my $end= $timeranges[$g->{Slower}+1];
-    my $imgurl= "$self?graph=$gname&section=$section&end=$end";
-    print "<img src=\"$imgurl\"></a>\n";
+foreach my $group (@{ $section_groups{$section} }) {
+    print "<a href=\"$self?detail=$group&section=$section\">";
+    my $imgurl= "$self?graph=$group&section=$section";
+    my $elems= $group_elems{$section,$group};
+    if (@$elems > 1) { print "<table><tr><td>"; }
+    foreach my $elem (@$elems) {
+       my $g= $graphs{$section,$group,$elem};
+       print "<img src=\"$imgurl&elem=$elem&sloth=".
+           ($sloth + $g->{Slower})."\">";
+    }
+    if (@$elems > 1) { print "</td></tr></table>"; }
+    print "</a>\n";
 }