chiark / gitweb /
new invoke script
[rrd-graphs.git] / cgi
diff --git a/cgi b/cgi
index d6193adfab855856b43360b372c0f03afd77f687..a4863104630156ef5a3a8a96398742d8da2ce0c9 100755 (executable)
--- a/cgi
+++ b/cgi
@@ -1,6 +1,7 @@
-#!/usr/bin/perl -w
+#!/usr/bin/speedy -w -- -t100 -M1
 
 use strict qw(vars);
+use CGI::SpeedyCGI qw/:standard -no_xhtml/;
 use CGI qw/:standard -no_xhtml/;
 
 sub fail ($) {
@@ -12,12 +13,14 @@ sub fail ($) {
     exit 0;
 }
 
-our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
-our $SELF= '/home/ijackson/things/rrd-graphs';
+our (@sections, %section_groups, %group_elems, %graphs);
 
-my $self= url(-relative=>1);
+#---------- initialisation code, run once - graphs setup ----------
 
-our (@sections, %section_groups, %group_elems, %graphs);
+BEGIN {
+
+our $R= '/var/lib/collectd/rrd/chiark.greenend.org.uk';
+our $SELF= '/home/ijackson/things/rrd-graphs';
 
 our @timeranges= (3600, map { $_*86400 } qw(1 7 28), 13*7+1, 366);
 
@@ -74,8 +77,8 @@ graph('General', 'CPU', { Units => '[%]' },
           "CDEF:$thing=0".join('', map { ",$thing$_,+" } (0..7)).",8.0,/";
        } qw(idle interrupt nice softirq steal system user wait)),
        "CDEF:allintr=softirq,steal,+,interrupt,+",
-       "AREA:system#88f:system:STACK",
        "AREA:allintr#ff0:interrupt:STACK",
+       "AREA:system#88f:system:STACK",
        "AREA:user#00f:user:STACK",
        "AREA:nice#ccc:nice:STACK",
        "AREA:wait#f00:wait:STACK",
@@ -157,12 +160,9 @@ if (!open NM, '<', "$SELF/data/news/name-map") {
     }
 }
 
-sub news_name_sortkey {
-    return join '.', reverse split /\./, $_[0];
-}
+our @news_graphs;
 
-foreach my $src (sort { news_name_sortkey($a) cmp news_name_sortkey($b) }
-                <$SELF/data/news/*.rrd>) {
+foreach my $src (<$SELF/data/news/*.rrd>) {
     my $site= $src;
     $site =~ s,\.rrd$,, or next;
     $site =~ s,.*/,,;
@@ -173,6 +173,15 @@ foreach my $src (sort { news_name_sortkey($a) cmp news_name_sortkey($b) }
     my $newsite= $news_name_map{$site,$inout};
     $site= $newsite if defined $newsite;
     next if $site eq '-';
+    #my $sk= join '.', reverse split /\./, $site;
+    my $sk= $site;
+    $sk .= " $&" if $sk =~ s/^[^.]*(?:news|nntp|peer)[^.]*\.//;
+    $sk .= " $inout";
+    push @news_graphs, [ $sk, $site, $inout, $src ];
+}
+
+foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
+    my ($sortkey, $site, $inout, $src)= @$siteinfo;
     graph_of_group("News", $site, $inout,
          {
                Units => '[art/s]',
@@ -203,12 +212,20 @@ foreach my $src (sort { news_name_sortkey($a) cmp news_name_sortkey($b) }
           ]);
 }
 
+}
+
+#---------- right, that was the initialisation ----------
+
+our $self= url(-relative=>1);
+
 if (param('debug')) {
     print "Content-Type: text/plain\n\n";
 }
 
 our @navsettings;
 
+@navsettings= ();
+
 sub navsetting ($) {
     my ($nav) = @_;
     my $var= $nav->{Variable};
@@ -240,9 +257,9 @@ sub num_param ($$$$) {
     return $v + 0;
 }
 
-my $group= param('graph');
+our $group= param('graph');
 
-my $elem= param('elem');
+our $elem= param('elem');
 if (defined $elem) {
     my $g= $graphs{$section,$group,$elem};
     die unless $g;
@@ -253,27 +270,35 @@ if (defined $elem) {
     my $sloth= param('sloth');
     die unless defined $sloth;
     $sloth =~ m/^(\d+)$/ or die;
+    $sloth= $1+0;
     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";
     
-    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;
+    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;
+    }
 
-    exec (qw(rrdtool graph - -a PNG --full-size-mode),
-         '-w',$width, '-h',$height,
-         @args);
+#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 $!;
 }
 
@@ -312,16 +337,16 @@ sub start_page ($) {
     print h1("$title");
 }
 
-my $detail= param('detail');
+our $detail= param('detail');
 if ($detail) {
     my $elems= $group_elems{$section,$detail};
     die unless $elems;
     start_page("$detail graphs");
-    foreach my $xsloth (0..5) {
+    foreach my $tsloth (0..5) {
        foreach my $elem (@$elems) {
            my $g= $graphs{$section,$detail,$elem};
            die unless $g;
-           my $tsloth= $xsloth + $g->{Slower};
+           next if $tsloth >= @{ $g->{TimeRanges} };
            my $imgurl= "$self?graph=$detail&section=$section".
                "&sloth=$tsloth&elem=$elem";
            print a({href=>"$imgurl&w=780&h=800"},