Add total upload/download to network usage graph
[invirt/scripts/munin.git] / web / all-usage.cgi
1 #!/usr/bin/perl -wT
2
3 use diagnostics;
4 use constant GRAPH_DIR => "/var/lib/munin/xvm-prod-hosts.mit.edu";
5 use CGI;
6 use CGI::Carp qw(fatalsToBrowser);
7 use RRDs;
8 use File::Spec::Functions;
9
10 $ENV{"RRDCACHED_ADDRESS"} = "/var/run/munin/rrdcached.sock";
11
12 our %graph_types = (cpu => "xen_cpu");
13 our %formats = qw(svg image/svg+xml png image/png eps application/postscript pdf application/pdf);
14
15 my @args = (qw(
16 --font LEGEND:7:/usr/share/munin/VeraMono.ttf
17 --font UNIT:7:/usr/share/munin/VeraMono.ttf
18 --font AXIS:7:/usr/share/munin/VeraMono.ttf
19 -
20 --title), "Domain CPU usage", qw(
21 --base 1000
22 -r
23 --lower-limit 0
24 --vertical-label %
25 --height 600
26 --width 800
27 --units-exponent 0));
28
29 my $q = new CGI;
30
31 my $format = $q->param("format") || "png";
32 my $mime_type;
33 if (exists($formats{$format})) {
34     $format =~ m|^(\w+)$| or die "Invalid format";
35     $format = $1;
36     $mime_type = $formats{$format};
37 } else {
38     die "Invalid format";
39 }
40
41 push @args, "--imgformat", uc($format);
42
43 my $days = $q->param("days") || "8";
44 $days =~ m|^(\d+)$| or die "Invalid number of days specified";
45 $days = int($1);
46
47 my $type = $q->param("type") || "cpu";
48 $type =~ m|^(\w+)$| or die "Invalid graph type";
49 $type = $graph_types{$1} or die "Invalid graph type";
50
51 my $path = catfile(GRAPH_DIR, "*-$type-uuid_????????_????_????_????_????????????-?.rrd");
52 my @files = glob $path or die "No data found";
53
54 push @args, "--start", "-".$days."d";
55
56 my %uuids = map { m|(........_...._...._...._............)-.\.rrd$|; $1, 1 } @files;
57
58 # Color list shamelessly stolen from munin-graph
59 my @COLOUR = ("#22ff22", "#0022ff", "#ff0000", "#00aaaa", "#ff00ff",
60               "#ffa500", "#cc0000", "#0000cc", "#0080C0", "#8080C0", "#FF0080",
61               "#800080", "#688e23", "#408080", "#808000", "#000000", "#00FF00",
62               "#0080FF", "#FF8000", "#800000", "#FB31FB");
63 my $color_index = 0;
64
65 foreach my $uuid (sort keys %uuids) {
66   my @uuid_files = grep { m|$uuid-.\.rrd$| } @files;
67   foreach my $i (0..$#uuid_files) {
68     $uuid_files[$i] =~ m|^([^:]+)$|;
69     push @args, "DEF:odata_${uuid}_$i=$1:42:AVERAGE";
70     push @args, "CDEF:data_${uuid}_$i=odata_${uuid}_$i,UN,0,odata_${uuid}_$i,IF,10000,/";
71   }
72   push @args, "CDEF:total_${uuid}=0,".join(",+,", map {"data_${uuid}_$_"} 0..$#uuid_files).",+";
73   push @args, "AREA:total_${uuid}".$COLOUR[($color_index++)%@COLOUR]."::STACK";
74   # print STDERR "VM $uuid: @uuid_files\n";
75 }
76
77 $ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";
78
79 print $q->header(-type=>$mime_type);
80 $|=1;
81
82 {
83     use Data::Dumper;
84     print STDERR "XVM usage: ", Dumper(\@args);
85 }
86 RRDs::graph (@args);