Add total upload/download to network usage graph
[invirt/scripts/munin.git] / web / usage.cgi
index e815d78..92ea122 100755 (executable)
@@ -7,7 +7,37 @@ use RRDs;
 use File::Spec::Functions;
 use subs 'die';
 
-our %graph_types = (cpu => "xen_cpu");
+$ENV{"RRDCACHED_ADDRESS"} = "/var/run/munin/rrdcached.sock";
+
+our %graph_types =
+  (cpu =>
+   {
+    plugin_name => "xen_cpu",
+    args => ["--title", "Domain CPU usage",
+            qw(
+                --base 1000
+                -r
+                --lower-limit 0
+                --vertical-label %
+                --units-exponent 0)],
+    cdef => "10000,/",
+    draw => "AREA",
+   },
+   net =>
+   {
+    plugin_name => "xen_net",
+    args => ["--title", "Domain network usage",
+            qw(
+                --base 1000
+                --vertical-label), "bits in (-) / out (+) per second",
+           ],
+    draw => "LINE",
+    total_unit => "B",
+    sub_types => [{suffix => "_up", cdef => "8,*", total_name => "Total upload"},
+                 {suffix => "_down", cdef => "-8,*", total_name => "Total download"},
+                ]
+   },
+                  );
 our %formats = qw(svg image/svg+xml png image/png eps application/postscript pdf application/pdf);
 
 my @args = (qw(
@@ -15,14 +45,9 @@ my @args = (qw(
 --font UNIT:7:/usr/share/munin/VeraMono.ttf
 --font AXIS:7:/usr/share/munin/VeraMono.ttf
 -
---title), "Domain CPU usage", qw(
---base 1000
--r
---lower-limit 0
---vertical-label %
 --height 175
 --width 400
---units-exponent 0));
+));
 
 my $q = new CGI;
 
@@ -48,20 +73,41 @@ $uuid = "$1_$2_$3_$4_$5";
 
 my $type = $q->param("type") || "cpu";
 $type =~ m|^(\w+)$| or die "Invalid graph type";
-$type = $graph_types{$1} or die "Invalid graph type";
-
-my $path = catfile(GRAPH_DIR, "*-$type-uuid_$uuid-?.rrd");
-my @files = glob $path or die "No data found";
+my %type = %{$graph_types{$1}} or die "Invalid graph type";
+push @args, @{$type{"args"}};
 
 push @args, "--start", "-".$days."d";
 
-foreach my $i (0..$#files) {
-  $files[$i] =~ m|^([^:]+)$|;
-  push @args, "DEF:odata$i=$1:42:AVERAGE";
-  push @args, "CDEF:data$i=odata$i,UN,0,odata$i,IF,10000,/";
+my @sub_types = {suffix => ""};
+if ($type{"sub_types"}) {
+  @sub_types = @{$type{"sub_types"}};
+}
+
+foreach my $sub_type (@sub_types) {
+  my $cdef = $sub_type->{"cdef"} || $type{"cdef"};
+  my $suffix = $sub_type->{"suffix"};
+
+  my $path = catfile(GRAPH_DIR, "*-$type{plugin_name}-uuid_$uuid$suffix-?.rrd");
+  my @files = glob $path;
+  unless (@files) {
+    print STDERR "No data found: $path\n";
+    die "No data found";
+  }
+
+  foreach my $i (0..$#files) {
+    $files[$i] =~ m|^([^:]+)$|;
+    my $data = "data$suffix$i";
+    push @args, "DEF:o$data=$1:42:AVERAGE";
+    push @args, "CDEF:$data=o$data,UN,0,o$data,IF";
+  }
+  push @args, "CDEF:total$suffix=0,".join(",+,", map {"data$suffix$_"} 0..$#files).",+";
+  push @args, "CDEF:graph$suffix=total$suffix".($cdef ? ",$cdef" : "");
+  push @args, "$type{draw}:graph$suffix#0000FF";
+  if (my $total_name = $sub_type->{"total_name"}) {
+    push @args, "VDEF:t$suffix=total$suffix,TOTAL";
+    push @args, "GPRINT:t$suffix:$total_name %6.2lf %S".$type{"total_unit"};
+  }
 }
-push @args, "CDEF:total=0,".join(",+,", map {"data$_"} 0..$#files).",+";
-push @args, "AREA:total#0000FF";
 
 $ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";