chiark / gitweb /
cache pngs
[rrd-graphs.git] / cgi
diff --git a/cgi b/cgi
index e854d8b298d6783bc15309f9be3e3a580c62ffea..c348508dd9b4001ff849897f5b7df2ffb87a36b1 100755 (executable)
--- a/cgi
+++ b/cgi
@@ -1,7 +1,7 @@
 #!/usr/bin/perl -w
 
 use strict qw(vars);
-use CGI qw/:standard/;
+use CGI qw/:standard -no_xhtml/;
 
 sub fail ($) {
     print(header(-status=>500),
@@ -25,7 +25,7 @@ sub graph_of_group ($$$$$) {
     my ($section, $group, $elem, $basis, $args) = @_;
     $basis->{Args}= $args;
     $basis->{Slower}= 0 unless exists $basis->{Slower};
-
+    $basis->{TimeRanges} ||= \@timeranges;
     $graphs{$section,$group,$elem}= $basis;
     if (!exists $group_elems{$section,$group}) {
        # new group then
@@ -43,14 +43,14 @@ sub graph ($$$$) {
     graph_of_group($section, $gname,'', $basis, $args);
 }
 
-graph('General', 'Load', { },
+graph('General', 'Load and processes', { },
       [
        "DEF:load=$R/load/load.rrd:shortterm:AVERAGE",
        (map { "DEF:$_=$R/processes/ps_state-$_.rrd:value:AVERAGE" }
            qw(blocked running stopped paging sleeping zombies)),
-       "AREA:running#88f:running processes:STACK",
-       "AREA:blocked#8f8:blocked processes:STACK",
-       "AREA:paging#f88:paging processes:STACK",
+       "AREA:running#88f:running:STACK",
+       "AREA:blocked#8f8:disk wait:STACK",
+       "AREA:paging#f88:paging:STACK",
        "LINE:load#000:load",
        ]);
 
@@ -73,13 +73,12 @@ graph('General', 'CPU', { Units => '[%]' },
                (0..7)),
           "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
        } qw(idle interrupt nice softirq steal system user wait)),
-       "AREA:system#00f:system:STACK",
-       "AREA:wait#f88:wait:STACK",
+       "CDEF:allintr=softirq,steal,+,interrupt,+",
+       "AREA:system#88f:system:STACK",
+       "AREA:allintr#ff0:interrupt:STACK",
+       "AREA:user#00f:user:STACK",
        "AREA:nice#ccc:nice:STACK",
-       "AREA:user#080:user:STACK",
-       "AREA:softirq#f0f:softirq:STACK",
-       "AREA:interrupt#ff0:interrupt:STACK",
-       "AREA:steal#0ff:steal:STACK",
+       "AREA:wait#f00:wait:STACK",
        ]);
 
 graph('General', 'Memory', { },
@@ -141,7 +140,29 @@ foreach my $src (<$R/df/df-*.rrd>) {
           ]);
 }
 
-foreach my $src (<$SELF/data/news/*.rrd>) {
+our %news_name_map;
+
+if (!open NM, '<', "$SELF/data/news/name-map") {
+    die unless $!==&ENOENT;
+} else {
+    while (<NM>) {
+       s/^\s*//; s/\s+$//;
+       next unless m/^[^\#]/;
+       m/^(\S+)\s+(in|out|\*)\s+(\S+)$/ or die;
+       if ($2 eq '*') {
+           $news_name_map{$1,$_}= $3 foreach qw(in out);
+       } else {
+           $news_name_map{$1,$2}= $3;
+       }
+    }
+}
+
+sub news_name_sortkey {
+    return join '.', reverse split /\./, $_[0];
+}
+
+foreach my $src (sort { news_name_sortkey($a) cmp news_name_sortkey($b) }
+                <$SELF/data/news/*.rrd>) {
     my $site= $src;
     $site =~ s,\.rrd$,, or next;
     $site =~ s,.*/,,;
@@ -149,21 +170,22 @@ foreach my $src (<$SELF/data/news/*.rrd>) {
     my $inout= $1;
     $site =~ s/^([-.0-9a-z]+)_//;
     my $us= $1; # all very well but we ignore it
+    my $newsite= $news_name_map{$site,$inout};
+    $site= $newsite if defined $newsite;
+    next if $site eq '-';
     graph_of_group("News", $site, $inout,
          {
-               Slower => 1,
-               Units => '[art/s]'
+               Units => '[art/s]',
+               TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
            }, $inout eq 'out' ?
          [
           (map { "DEF:$_=$src:$_:AVERAGE" }
-               qw(missing offered deferred unwanted accepted
-                  rejected body_missing)),
+               qw(missing deferred unwanted accepted rejected body_missing)),
           "AREA:accepted#00f:ok",
-          "AREA:body_missing#ff0:missing:STACK",
+          "AREA:body_missing#ff0:miss:STACK",
           "AREA:rejected#f00:rej:STACK",
-          "AREA:unwanted#bbb:unw:STACK",
-          "AREA:deferred#eee:deferred:STACK",
-          "LINE:offered#080:",
+          "AREA:unwanted#aaa:unw:STACK",
+          "AREA:deferred#ddd:defer:STACK",
           ] :
          [
           (map { "DEF:$_=$src:$_:AVERAGE" }
@@ -174,9 +196,10 @@ foreach my $src (<$SELF/data/news/*.rrd>) {
           "AREA:accepted#00f:ok:STACK",
           "AREA:rejected#f00:rej:STACK",
           "AREA:duplicate#000:dupe:STACK",
-          "AREA:refused#bbb:unw:STACK",
+          "AREA:refused#aaa:unw:STACK",
+          "CDEF:kb_accepted_smooth=kb_accepted,<interval/60>,TREND",
           "LINE:kb_duplicate#ff0:kb dupe",
-          "LINE:kb_accepted#008:kb",
+          "LINE:kb_accepted_smooth#008:~kb",
           ]);
 }
 
@@ -224,32 +247,40 @@ if (defined $elem) {
     my $g= $graphs{$section,$group,$elem};
     die unless $g;
 
-    my @args= @{ $g->{Args} };
-
     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;
+    $sloth= $1+0;
+    my $end= $g->{TimeRanges}[$sloth];
+
+    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";
     
-    if (param('debug')) {
-        print((join "\n",@args),"\n"); exit 0;
-    }
-    print "Content-Type: image/png\n\n";
-
     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',$width, '-h',$height,
-         @args);
+    unshift @args, '-t', $title, '-w',$width, '-h',$height;
+    unshift @args, qw(-a PNG --full-size-mode);
+
+    if (param('debug')) {
+        print((join "\n",@args),"\n"); exit 0;
+    }
+
+#print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
+    exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
+        p="$1"; shift
+        rrdtool graph "$p" --lazy "$@" >/dev/null
+       printf "Content-Type: image/png\n\n"
+       exec cat "$p"
+END
     die $!;
 }
 
@@ -268,16 +299,16 @@ sub start_page ($) {
            if ($couldbe eq $current) {
                print "<b>$show</b>";
            } else {
-               print "<a href=\"$self";
+               my $u= $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";
+                   $u .= $delim2;  $u .= "$nav2->{Param}=$current2";
                    $delim2= '&';
                }
-               print "\">$show</a>";
+               print a({href=>$u}, $show);
            }
            $delim= ' | ';
        }
@@ -300,8 +331,8 @@ if ($detail) {
            my $tsloth= $xsloth + $g->{Slower};
            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 a({href=>"$imgurl&w=780&h=800"},
+                   img({src=>$imgurl, alt=>''}));
        }
     }
     print end_html();
@@ -331,16 +362,16 @@ if (param('debug')) {
 start_page("$section graphs");
 
 foreach my $group (@{ $section_groups{$section} }) {
-    print "<a href=\"$self?detail=$group&section=$section\">";
+    print a({href=>"$self?detail=$group&section=$section"});
     my $imgurl= "$self?graph=$group&section=$section";
+    print "<span style=\"white-space:nowrap\">";
     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})."\">";
+       print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
+                  alt=>''});
     }
-    if (@$elems > 1) { print "</td></tr></table>"; }
+    print "</span>";
     print "</a>\n";
 }