4 use constant GRAPH_DIR => "/var/lib/munin/xvm-prod-hosts.mit.edu";
7 use File::Spec::Functions;
10 our %graph_types = (cpu => "xen_cpu");
11 our %formats = qw(svg image/svg+xml png image/png eps application/postscript pdf application/pdf);
14 --font LEGEND:7:/usr/share/munin/VeraMono.ttf
15 --font UNIT:7:/usr/share/munin/VeraMono.ttf
16 --font AXIS:7:/usr/share/munin/VeraMono.ttf
18 --title), "Domain CPU usage", qw(
29 my $format = $q->param("format") || "png";
31 if (exists($formats{$format})) {
32 $format =~ m|^(\w+)$| or die "Invalid format";
34 $mime_type = $formats{$format};
39 push @args, "--imgformat", uc($format);
41 my $days = $q->param("days") || "8";
42 $days =~ m|^(\d+)$| or die "Invalid number of days specified";
45 my $uuid = $q->param("uuid");
46 $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";
47 $uuid = "$1_$2_$3_$4_$5";
49 my $type = $q->param("type") || "cpu";
50 $type =~ m|^(\w+)$| or die "Invalid graph type";
51 $type = $graph_types{$1} or die "Invalid graph type";
53 my $path = catfile(GRAPH_DIR, "*-$type-uuid_$uuid-?.rrd");
54 my @files = glob $path or die "No data found";
56 push @args, "--start", "-".$days."d";
58 foreach my $i (0..$#files) {
59 $files[$i] =~ m|^([^:]+)$|;
60 push @args, "DEF:odata$i=$1:42:AVERAGE";
61 push @args, "CDEF:data$i=odata$i,UN,0,odata$i,IF,10000,/";
63 push @args, "CDEF:total=0,".join(",+,", map {"data$_"} 0..$#files).",+";
64 push @args, "AREA:total#0000FF";
66 $ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";
68 print $q->header(-type=>$mime_type);
73 print STDERR "XVM usage: ", Dumper(\@args);
80 my $im = Image::Magick->new(background => "white",
84 $im->Read('label:'.join('', @_));
85 $format = 'png' unless exists($formats{$format});
86 $mime_type = $formats{$format};
88 print $q->header(-type=>$mime_type);
89 $im->Write($format.':-');