chiark / gitweb /
found
[rrd-graphs.git] / cgi
diff --git a/cgi b/cgi
index 85777859b4254cee81442a2fef8eba2b91479152..5b51f8a1cef72b61efff57651f94b686d8a8a03f 100755 (executable)
--- a/cgi
+++ b/cgi
@@ -4,6 +4,7 @@ use strict qw(vars);
 use CGI::SpeedyCGI qw/:standard -no_xhtml/;
 use CGI qw/:standard -no_xhtml/;
 use POSIX;
+use MD5;
 
 sub fail ($) {
     print(header(-status=>500),
@@ -161,7 +162,7 @@ if (!open NM, '<', "$SELF/data/news/name-map") {
     }
 }
 
-our @news_graphs;
+our %news_sources;
 
 foreach my $src (<$SELF/data/news/*.rrd>) {
     my $site= $src;
@@ -174,23 +175,52 @@ foreach my $src (<$SELF/data/news/*.rrd>) {
     my $newsite= $news_name_map{$site,$inout};
     $site= $newsite if defined $newsite;
     next if $site eq '-';
-    #my $sk= join '.', reverse split /\./, $site;
+    push @{ $news_sources{$site}{$inout} }, $src;
+}
+
+our @news_graphs;
+
+foreach my $site (keys %news_sources) {
     my $sk= $site;
-    $sk .= " $&" if $sk =~ s/^[^.]*(?:news|nntp|peer)[^.]*\.//;
-    $sk .= " $inout";
-    push @news_graphs, [ $sk, $site, $inout, $src ];
+    for (;;) {
+        last unless $sk =~
+            s/^[^.]*(?:chiark|greenend|news|nntp|peer|feed|in|out)[^.]*\.//;
+        $sk .= " $&";
+    }
+    foreach my $inout (keys %{ $news_sources{$site} }) {
+        push @news_graphs, [ "$sk $inout", $site, $inout ];
+    }
 }
 
 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
-    my ($sortkey, $site, $inout, $src)= @$siteinfo;
+    my ($sortkey, $site, $inout)= @$siteinfo;
+    my @sources= @{ $news_sources{$site}{$inout} };
+
+    my @vals= $inout eq 'out'
+        ? qw(missing deferred unwanted accepted rejected body_missing)
+        : qw(accepted refused rejected duplicate
+             accepted_size duplicate_size);
+    my @defs;
+    foreach my $val (@vals) {
+        my $def= "CDEF:$val=0";
+        foreach my $si (0..$#sources) {
+            my $src= $sources[$si];
+            my $tvar= "${val}_${si}";
+            push @defs, "DEF:$tvar=$src:$val:AVERAGE";
+            $def .= ",$tvar,ADDNAN";
+        }
+        push @defs, $def;
+        if ($val =~ m/_size$/) {
+            push @defs, "CDEF:kb_$`=$val,1024,/";
+        }
+    }
     graph_of_group("News", $site, $inout,
          {
                Units => '[art/s]',
                TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
            }, $inout eq 'out' ?
          [
-          (map { "DEF:$_=$src:$_:AVERAGE" }
-               qw(missing deferred unwanted accepted rejected body_missing)),
+          @defs,
           "AREA:accepted#00f:ok",
           "AREA:body_missing#ff0:miss:STACK",
           "AREA:rejected#f00:rej:STACK",
@@ -198,11 +228,7 @@ foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
           "AREA:deferred#ddd:defer:STACK",
           ] :
          [
-          (map { "DEF:$_=$src:$_:AVERAGE" }
-               qw(accepted refused rejected duplicate)),
-          (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
-                  "CDEF:kb_$_=bytes_$_,1024,/")
-             } qw(accepted duplicate)),
+          @defs,
           "AREA:accepted#00f:ok:STACK",
           "AREA:rejected#f00:rej:STACK",
           "AREA:duplicate#000:dupe:STACK",
@@ -229,14 +255,16 @@ foreach my $physdiskrrd (<$R/disk-*/disk_octets.rrd>) {
 
 our @disk_vgs;
 
-sub lvgraphs ($$) {
-    my ($vg, $factor) = @_;
+sub lvgraphs {
+    my ($vg, $label, $factor, $rcolour, $wcolour) = @_;
     my @lvs;
     my $varname= $vg;
     $varname =~ s/[^0-9a-zA-Y]/ sprintf "Z%02x", ord($&) /ge;
     my $vginfo= {
-        Name => $vg,
-        Varname => $varname
+        Name => $label,
+        Varname => $varname,
+        Colour => { 'read' => $rcolour, 'write' => $wcolour },
+        Lvs => []
     };
     foreach my $bo (qw(octets ops)) {
         foreach my $rw (qw(read write)) {
@@ -255,15 +283,18 @@ sub lvgraphs ($$) {
         my $rrd= $disk_rdev2rrd{(stat _)[6]};
         next unless defined $rrd;
 
+        my $lvinfo= { Name => $lv };
+        push @{ $vginfo->{Lvs} }, $lvinfo;
+
         foreach my $bo (qw(octets ops)) {
-0 and
-            graph_of_group('IO', "$vg $lv", $bo, { Units => '[/s]' },
+            $lvinfo->{Defs}{$bo}=
               [
                (map { ("DEF:$_=${rrd}${bo}.rrd:$_:AVERAGE") } qw(read write)),
                "CDEF:mwrite=0,write,-",
                "AREA:read#00f:read",
                "AREA:mwrite#f00:write"
-               ]);
+               ];
+
             foreach my $rw (qw(read write)) {
                 $ix++;
                 my $tvar= "lv_${rw}_${bo}_${varname}_${ix}";
@@ -274,35 +305,52 @@ sub lvgraphs ($$) {
         }
     }
     foreach my $bo (qw(octets ops)) {
-        my $defs= [];
         foreach my $rw (qw(read write)) {
+            my $defs= [];
             push @$defs, @{ $vginfo->{VarDefs}{$bo}{$rw} };
             push @$defs, "CDEF:${rw}_vg_${varname}=".
                 $vginfo->{Sumdef}{$bo}{$rw}.
                 sprintf(",%f,*", $rw eq 'write' ? -$factor : $factor);
+            $vginfo->{Defs}{$bo}{$rw}= $defs;
         }
-        $vginfo->{Defs}{$bo}= $defs;
     }
     push @disk_vgs, $vginfo;
 }
 
-lvgraphs('vg-main', 1);
-lvgraphs('vg-chiark-stripe', 0.5);
+lvgraphs('vg-main',          'main',     1, qw(00f f00));
+lvgraphs('vg-chiark-stripe', 'stripe', 0.5, qw(008 800));
 
 foreach my $bo (qw(octets ops)) {
     my @a= ();
     foreach my $rw (qw(read write)) {
         my $stack= '';
         foreach my $vginfo (@disk_vgs) {
-            push @a, @{ $vginfo->{Defs}{$bo} };
-            push @a, "AREA:${rw}_vg_$vginfo->{Varname}#0ff:$vginfo->{Name}"
-                .$stack;
+            push @a, @{ $vginfo->{Defs}{$bo}{$rw} };
+            push @a, "AREA:${rw}_vg_$vginfo->{Varname}#".
+                $vginfo->{Colour}{$rw}.
+                ":$vginfo->{Name} ".substr($rw,0,1).
+                $stack;
             $stack= ':STACK';
         }
     }
-    graph_of_group('IO', 'T', $bo, { Units => '[/s]' }, \@a);
+    graph_of_group('IO', 'IO', $bo, { Units => '[/s]' }, \@a);
 }
 
+foreach my $vginfo (@disk_vgs) {
+    foreach my $bo (qw(octets ops)) {
+        foreach my $lv (@{ $vginfo->{Lvs} }) {
+            graph_of_group('IO', "$vginfo->{Name} $lv->{Name}",
+                           $bo, { Units => '[/s]' }, $lv->{Defs}{$bo});
+        }
+    }
+}
+
+push @{ $section_groups{General} }, {
+    Section => 'IO',
+    Group => 'IO',
+    UrlParams => "section=IO&sloth=SLOTH"
+};
+
 }
 #---------- right, that was the initialisation ----------
 
@@ -349,13 +397,16 @@ sub num_param ($$$$) {
 
 our $group= param('graph');
 
+my $defwidth= 370;
+my $defheight= 200;
+
 our $elem= param('elem');
 if (defined $elem) {
     my $g= $graphs{$section,$group,$elem};
     die unless $g;
 
-    my $width= num_param('w',370,100,1600);
-    my $height= num_param('h',200,100,1600);
+    my $width= num_param('w',$defwidth,100,1600);
+    my $height= num_param('h',$defheight,100,1600);
 
     my $sloth= param('sloth');
     die unless defined $sloth;
@@ -364,9 +415,6 @@ if (defined $elem) {
     my $end= $g->{TimeRanges}[$sloth];
     die unless defined $end;
 
-    my $cacheid= "$section!$group!$elem!$sloth!$width!$height";
-    my $cachepath= "cache/$cacheid.png";
-
     my @args= @{ $g->{Args} };
     s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
     unshift @args, qw(--end now --start), "end-${end}s";
@@ -378,6 +426,10 @@ if (defined $elem) {
     unshift @args, '-t', $title, '-w',$width, '-h',$height;
     unshift @args, qw(-a PNG --full-size-mode);
 
+    my $cacheid= "$section!$group!$elem!$sloth!$width!$height!";
+    $cacheid .= unpack "H*", MD5->hash(join '\0', @args);
+    my $cachepath= "cache/$cacheid.png";
+
     if (param('debug')) {
         print((join "\n",@args),"\n"); exit 0;
     }
@@ -428,10 +480,10 @@ sub start_page ($) {
 }
 
 our $detail= param('detail');
-if ($detail) {
+if (defined $detail) {
     my $elems= $group_elems{$section,$detail};
     die unless $elems;
-    start_page("$detail graphs");
+    start_page("$detail - $section - graphs");
     foreach my $tsloth (0..5) {
        foreach my $elem (@$elems) {
            my $g= $graphs{$section,$detail,$elem};
@@ -440,7 +492,8 @@ if ($detail) {
            my $imgurl= "$self?graph=$detail&section=$section".
                "&sloth=$tsloth&elem=$elem";
            print a({href=>"$imgurl&w=780&h=800"},
-                   img({src=>$imgurl, alt=>''}));
+                   img({src=>$imgurl, alt=>'',
+                        width=>$defwidth, height=>$defheight}));
        }
     }
     print end_html();
@@ -467,17 +520,26 @@ if (param('debug')) {
     exit 0;
 }
 
-start_page("$section graphs");
+start_page("$section graphs");
 
 foreach my $group (@{ $section_groups{$section} }) {
-    print a({href=>"$self?detail=$group&section=$section"});
-    my $imgurl= "$self?graph=$group&section=$section";
+    my $ref_group= $group;
+    my $ref_section= $section;
+    my $ref_urlparams= "detail=$group&section=$section";
+    if (ref $group) {
+        $ref_group= $group->{Group};
+        $ref_section= $group->{Section};
+        $ref_urlparams= $group->{UrlParams};
+        $ref_urlparams =~ s/\bSLOTH\b/$sloth/;
+    }
+    print a({href=>"$self?$ref_urlparams"});
+    my $imgurl= "$self?graph=$ref_group&section=$ref_section";
     print "<span style=\"white-space:nowrap\">";
-    my $elems= $group_elems{$section,$group};
+    my $elems= $group_elems{$ref_section,$ref_group};
     foreach my $elem (@$elems) {
-       my $g= $graphs{$section,$group,$elem};
+       my $g= $graphs{$ref_section,$ref_group,$elem};
        print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
-                  alt=>''});
+                  alt=>'', width=>$defwidth, height=>$defheight});
     }
     print "</span>";
     print "</a>\n";