#!/usr/bin/perl -w

if ((@ARGV != 1) && (@ARGV != 2)) {
	print STDERR "Syntax: ${0} configfile [uid]\n";
	exit(1);
}

use Net::SNMP;

$UPSGRAPH::host = "";
$UPSGRAPH::rrdfile = "";
$UPSGRAPH::outdir = "";
$UPSGRAPH::community = "public";
$UPSGRAPH::step = 60;
@UPSGRAPH::fields = ();
$UPSGRAPH::vars = {};

do $ARGV[0] or die "can't read config: $!";

my $host = $UPSGRAPH::host;
my $rrdfile = $UPSGRAPH::rrdfile;
my $outdir = $UPSGRAPH::outdir;
my $community = $UPSGRAPH::community;
my $step = $UPSGRAPH::step;
my @fields = @UPSGRAPH::fields;
my $vars = $UPSGRAPH::vars;

if ($> == 0) {
	if (@ARGV != 2) {
		print STDERR "Running as root, please provide UID as 4th argument!\n";
		exit(1);
	}

	print "Running as root, switching to ".$ARGV[1]."\n";
	$< = $> = $ARGV[1];
}

if (! -e "${rrdfile}") {
	my $cmd = "rrdtool create \"${rrdfile}\" ";
	foreach my $var (@fields) {
		$cmd .= "DS:${var}:GAUGE:600:" .
			$vars->{$var}->{'min'} . ":" .
			$vars->{$var}->{'max'} . " ";
	}
	$cmd .= "RRA:AVERAGE:0.5:1:10080 --step ${step}";

	print "Creating ${rrdfile}...\n";
	print `${cmd}`;
}

my $child = fork();

die "fork failed!" if (!defined($child));

exit 0 if ($child != 0);

while(1) {
	($session,$error) = Net::SNMP->session(Hostname => $host,
			Community => $community);

	die "session error: $error" unless ($session);

	$session->translate(0);

	foreach my $var (@fields) {
		delete $vars->{$var}->{'value'};

		my $result = $session->get_request($vars->{$var}->{'oid'});
		next unless (defined $result);

		$vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}};
		if (defined($vars->{$var}->{'factor'})) {
			$vars->{$var}->{'value'} *= $vars->{$var}->{'factor'};
		}
	}

	$session->close;

	my $cmd = "rrdtool update \"${rrdfile}\" \"N";
	foreach my $var (@fields) {
		if (!(defined($vars->{$var}->{'value'}))) {
			$vars->{$var}->{'value'} = 'U';
		}
		$cmd .= ":" . $vars->{$var}->{'value'};
	}
	$cmd .= "\"";
	print `${cmd}`;

	open(HTML, ">${outdir}/index.html.new");

	print HTML '<html><head><meta http-equiv="refresh" content="60"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http_equiv="expires" content="Sat, 26 Jul 1997 05:00:00 GMT"/><title>USV status</title></head>';
	print HTML '<body bgcolor="#ffffff">';

	foreach my $var (@fields) {
		my $graphdef = "-t \"" . $vars->{$var}->{'name'} . "\" \"DEF:${var}=${rrdfile}:${var}:AVERAGE\" \"LINE1:${var}#FF0000\"";
		my $cmd = "rrdtool graph \"${outdir}/${var}.png.new\" -w 720 ${graphdef}";
		my $size = `$cmd`;
		rename("${outdir}/${var}.png.new", "${outdir}/${var}.png");
		(my $width, my $height) = split(/x/,$size);

		my $cmd2 = "rrdtool graph \"${outdir}/${var}.long.png.new\" -w 1008 -e now -s end-1w ${graphdef}";
		my $size2 = `$cmd2`;
		rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png");

		print HTML "<a href=\"${var}.long.png\"><img src=\"${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>";
	}

	print HTML "</body></html>\n";
	print HTML "<br>Generated on: " . localtime(time());
	print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.';

	close(HTML);

	rename("${outdir}/index.html.new", "${outdir}/index.html");

	sleep(${step}/2);
}
