4 use constant GRAPH_DIR => "/var/lib/munin/xvm-prod-hosts.mit.edu";
7 use File::Spec::Functions;
10 $ENV{"RRDCACHED_ADDRESS"} = "/var/run/munin/rrdcached.sock";
15 plugin_name => "xen_cpu",
16 args => ["--title", "Domain CPU usage",
28 plugin_name => "xen_net",
29 args => ["--title", "Domain network usage",
32 --vertical-label), "bits in (-) / out (+) per ${graph_period}",
36 sub_types => [{suffix => "_up", cdef => "8,*"},
37 {suffix => "_down", cdef => "-8,*"},
41 our %formats = qw(svg image/svg+xml png image/png eps application/postscript pdf application/pdf);
44 --font LEGEND:7:/usr/share/munin/VeraMono.ttf
45 --font UNIT:7:/usr/share/munin/VeraMono.ttf
46 --font AXIS:7:/usr/share/munin/VeraMono.ttf
54 my $format = $q->param("format") || "png";
56 if (exists($formats{$format})) {
57 $format =~ m|^(\w+)$| or die "Invalid format";
59 $mime_type = $formats{$format};
64 push @args, "--imgformat", uc($format);
66 my $days = $q->param("days") || "8";
67 $days =~ m|^(\d+)$| or die "Invalid number of days specified";
70 my $uuid = $q->param("uuid");
71 $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";
72 $uuid = "$1_$2_$3_$4_$5";
74 my $type = $q->param("type") || "cpu";
75 $type =~ m|^(\w+)$| or die "Invalid graph type";
76 my %type = %{$graph_types{$1}} or die "Invalid graph type";
77 push @args, @{$type{"args"}};
79 push @args, "--start", "-".$days."d";
81 my @sub_types = {suffix => ""};
82 if ($type{"sub_types"}) {
83 @sub_types = @{$type{"sub_types"}};
86 foreach my $sub_type (@sub_types) {
87 my $cdef = $sub_type->{"cdef"} || $type{"cdef"};
88 my $suffix = $sub_type->{"suffix"};
90 my $path = catfile(GRAPH_DIR, "*-$type{plugin_name}-uuid_$uuid$suffix-?.rrd");
91 my @files = glob $path;
93 print STDERR "No data found: $path\n";
97 foreach my $i (0..$#files) {
98 $files[$i] =~ m|^([^:]+)$|;
99 my $data = "data$suffix$i";
100 push @args, "DEF:o$data=$1:42:AVERAGE";
101 push @args, "CDEF:$data=o$data,UN,0,o$data,IF".($cdef ? ",$cdef" : "");
103 push @args, "CDEF:total$suffix=0,".join(",+,", map {"data$suffix$_"} 0..$#files).",+";
104 push @args, "$type{draw}:total$suffix#0000FF";
107 $ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";
109 print $q->header(-type=>$mime_type);
114 print STDERR "XVM usage: ", Dumper(\@args);
121 my $im = Image::Magick->new(background => "white",
125 $im->Read('label:'.join('', @_));
126 $format = 'png' unless exists($formats{$format});
127 $mime_type = $formats{$format};
129 print $q->header(-type=>$mime_type);
130 $im->Write($format.':-');