4 use constant GRAPH_DIR => "/var/lib/munin/xvm-prod-hosts.mit.edu";
7 use File::Spec::Functions;
13 plugin_name => "xen_cpu",
14 args => ["--title", "Domain CPU usage",
26 plugin_name => "xen_net",
27 args => ["--title", "Domain network usage",
30 --vertical-label), "bits in (-) / out (+) per ${graph_period}",
34 sub_types => [{suffix => "_up", cdef => "8,*"},
35 {suffix => "_down", cdef => "-8,*"},
39 our %formats = qw(svg image/svg+xml png image/png eps application/postscript pdf application/pdf);
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
52 my $format = $q->param("format") || "png";
54 if (exists($formats{$format})) {
55 $format =~ m|^(\w+)$| or die "Invalid format";
57 $mime_type = $formats{$format};
62 push @args, "--imgformat", uc($format);
64 my $days = $q->param("days") || "8";
65 $days =~ m|^(\d+)$| or die "Invalid number of days specified";
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";
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"}};
77 push @args, "--start", "-".$days."d";
79 my @sub_types = {suffix => ""};
80 if ($type{"sub_types"}) {
81 @sub_types = @{$type{"sub_types"}};
84 foreach my $sub_type (@sub_types) {
85 my $cdef = $sub_type->{"cdef"} or $type{"cdef"};
86 my $suffix = $sub_type->{"suffix"};
88 my $path = catfile(GRAPH_DIR, "*-$type{plugin_name}-uuid_$uuid$suffix-?.rrd");
89 my @files = glob $path;
91 print STDERR "No data found: $path\n";
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" : "");
101 push @args, "CDEF:total$suffix=0,".join(",+,", map {"data$suffix$_"} 0..$#files).",+";
102 push @args, "$type{draw}:total$suffix#0000FF";
105 $ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";
107 print $q->header(-type=>$mime_type);
112 print STDERR "XVM usage: ", Dumper(\@args);
119 my $im = Image::Magick->new(background => "white",
123 $im->Read('label:'.join('', @_));
124 $format = 'png' unless exists($formats{$format});
125 $mime_type = $formats{$format};
127 print $q->header(-type=>$mime_type);
128 $im->Write($format.':-');