212350631bc37b383dcc4318a8c632d5000aa86c
[invirt/scripts/munin.git] / web / 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 RRDs;
7 use File::Spec::Functions;
8 use subs 'die';
9
10 our %graph_types =
11   (cpu =>
12    {
13     plugin_name => "xen_cpu",
14     args => ["--title", "Domain CPU usage",
15              qw(
16                  --base 1000
17                  -r
18                  --lower-limit 0
19                  --vertical-label %
20                  --units-exponent 0)],
21     cdef => "10000,/",
22     draw => "AREA",
23    },
24    net =>
25    {
26     plugin_name => "xen_net",
27     args => ["--title", "Domain network usage",
28              qw(
29                  --base 1000
30                  --vertical-label), "bits in (-) / out (+) per ${graph_period}",
31             ],
32     cdef => "8,*",
33     draw => "LINE",
34     sub_types => [{suffix => "_up", cdef => "8,*"},
35                   {suffix => "_down", cdef => "-8,*"},
36                  ]
37    },
38                    );
39 our %formats = qw(svg image/svg+xml png image/png eps application/postscript pdf application/pdf);
40
41 my @args = (qw(
42 --font LEGEND:7:/usr/share/munin/VeraMono.ttf
43 --font UNIT:7:/usr/share/munin/VeraMono.ttf
44 --font AXIS:7:/usr/share/munin/VeraMono.ttf
45 -
46 --height 175
47 --width 400
48 ));
49
50 my $q = new CGI;
51
52 my $format = $q->param("format") || "png";
53 my $mime_type;
54 if (exists($formats{$format})) {
55     $format =~ m|^(\w+)$| or die "Invalid format";
56     $format = $1;
57     $mime_type = $formats{$format};
58 } else {
59     die "Invalid format";
60 }
61
62 push @args, "--imgformat", uc($format);
63
64 my $days = $q->param("days") || "8";
65 $days =~ m|^(\d+)$| or die "Invalid number of days specified";
66 $days = int($1);
67
68 my $uuid = $q->param("uuid");
69 $uuid =~ m|^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$| or die "Invalid UUID specified";
70 $uuid = "$1_$2_$3_$4_$5";
71
72 my $type = $q->param("type") || "cpu";
73 $type =~ m|^(\w+)$| or die "Invalid graph type";
74 my %type = %{$graph_types{$1}} or die "Invalid graph type";
75 push @args, @{$type{"args"}};
76
77 push @args, "--start", "-".$days."d";
78
79 my @sub_types = {suffix => ""};
80 if ($type{"sub_types"}) {
81   @sub_types = @{$type{"sub_types"}};
82 }
83
84 foreach my $sub_type (@sub_types) {
85   my $cdef = $sub_type->{"cdef"} or $type{"cdef"};
86   my $suffix = $sub_type->{"suffix"};
87
88   my $path = catfile(GRAPH_DIR, "*-$type{plugin_name}-uuid_$uuid$suffix-?.rrd");
89   my @files = glob $path;
90   unless (@files) {
91     print STDERR "No data found: $path\n";
92     die "No data found";
93   }
94
95   foreach my $i (0..$#files) {
96     $files[$i] =~ m|^([^:]+)$|;
97     my $data = "data$suffix$i";
98     push @args, "DEF:o$data=$1:42:AVERAGE";
99     push @args, "CDEF:$data=o$data,UN,0,o$data,IF".($cdef ? ",$cdef" : "");
100   }
101   push @args, "CDEF:total$suffix=0,".join(",+,", map {"data$suffix$_"} 0..$#files).",+";
102   push @args, "$type{draw}:total$suffix#0000FF";
103 }
104
105 $ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";
106
107 print $q->header(-type=>$mime_type);
108 $|=1;
109
110 {
111     use Data::Dumper;
112     print STDERR "XVM usage: ", Dumper(\@args);
113 }
114 RRDs::graph (@args);
115
116 sub die(@) {
117   use Image::Magick;
118
119   my $im = Image::Magick->new(background => "white",
120                               fill => "red",
121                               pointsize => 14,
122                               );
123   $im->Read('label:'.join('', @_));
124   $format = 'png' unless exists($formats{$format});
125   $mime_type = $formats{$format};
126   $| = 1;
127   print $q->header(-type=>$mime_type);
128   $im->Write($format.':-');
129   CORE::die @_;
130 }