#!/usr/bin/perl -wT

use diagnostics;
use constant GRAPH_DIR => "/var/lib/munin/xvm-prod-hosts.mit.edu";
use CGI;
use RRDs;
use File::Spec::Functions;
use subs 'die';

$ENV{"RRDCACHED_ADDRESS"} = "/var/run/munin/rrdcached.sock";

our %graph_types =
  (cpu =>
   {
    plugin_name => "xen_cpu",
    args => ["--title", "Domain CPU usage",
	     qw(
		 --base 1000
		 -r
		 --lower-limit 0
		 --vertical-label %
		 --units-exponent 0)],
    cdef => "10000,/",
    draw => "AREA",
   },
   net =>
   {
    plugin_name => "xen_net",
    args => ["--title", "Domain network usage",
	     qw(
		 --base 1000
		 --vertical-label), "bits in (-) / out (+) per ${graph_period}",
	    ],
    cdef => "8,*",
    draw => "LINE",
    sub_types => [{suffix => "_up", cdef => "8,*"},
		  {suffix => "_down", cdef => "-8,*"},
		 ]
   },
		   );
our %formats = qw(svg image/svg+xml png image/png eps application/postscript pdf application/pdf);

my @args = (qw(
--font LEGEND:7:/usr/share/munin/VeraMono.ttf
--font UNIT:7:/usr/share/munin/VeraMono.ttf
--font AXIS:7:/usr/share/munin/VeraMono.ttf
-
--height 175
--width 400
));

my $q = new CGI;

my $format = $q->param("format") || "png";
my $mime_type;
if (exists($formats{$format})) {
    $format =~ m|^(\w+)$| or die "Invalid format";
    $format = $1;
    $mime_type = $formats{$format};
} else {
    die "Invalid format";
}

push @args, "--imgformat", uc($format);

my $days = $q->param("days") || "8";
$days =~ m|^(\d+)$| or die "Invalid number of days specified";
$days = int($1);

my $uuid = $q->param("uuid");
$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";
$uuid = "$1_$2_$3_$4_$5";

my $type = $q->param("type") || "cpu";
$type =~ m|^(\w+)$| or die "Invalid graph type";
my %type = %{$graph_types{$1}} or die "Invalid graph type";
push @args, @{$type{"args"}};

push @args, "--start", "-".$days."d";

my @sub_types = {suffix => ""};
if ($type{"sub_types"}) {
  @sub_types = @{$type{"sub_types"}};
}

foreach my $sub_type (@sub_types) {
  my $cdef = $sub_type->{"cdef"} || $type{"cdef"};
  my $suffix = $sub_type->{"suffix"};

  my $path = catfile(GRAPH_DIR, "*-$type{plugin_name}-uuid_$uuid$suffix-?.rrd");
  my @files = glob $path;
  unless (@files) {
    print STDERR "No data found: $path\n";
    die "No data found";
  }

  foreach my $i (0..$#files) {
    $files[$i] =~ m|^([^:]+)$|;
    my $data = "data$suffix$i";
    push @args, "DEF:o$data=$1:42:AVERAGE";
    push @args, "CDEF:$data=o$data,UN,0,o$data,IF".($cdef ? ",$cdef" : "");
  }
  push @args, "CDEF:total$suffix=0,".join(",+,", map {"data$suffix$_"} 0..$#files).",+";
  push @args, "$type{draw}:total$suffix#0000FF";
}

$ENV{"PATH"} = "/usr/local/bin:/usr/bin:/bin";

print $q->header(-type=>$mime_type);
$|=1;

{
    use Data::Dumper;
    print STDERR "XVM usage: ", Dumper(\@args);
}
RRDs::graph (@args);

sub die(@) {
  use Image::Magick;

  my $im = Image::Magick->new(background => "white",
			      fill => "red",
			      pointsize => 14,
			      );
  $im->Read('label:'.join('', @_));
  $format = 'png' unless exists($formats{$format});
  $mime_type = $formats{$format};
  $| = 1;
  print $q->header(-type=>$mime_type);
  $im->Write($format.':-');
  CORE::die @_;
}