#!/usr/bin/perl -w

use RRDs;

sub hexb_ {
        return join("_", map(sprintf("%02x", $_) , @_));
}

sub make_ds {
my (@r);

foreach (@_) {
	push @r, "DS:".hexb_(map{(/^0/)?oct:(/^[0-9]+$/)?int:0} @$_).":GAUGE:600:U:U";
}
return @r;
}

sub usage {
print $0." <parmsfile>\n";
print "creates an rdd file using parms file as definition\n";
print "parms files consits  of line containing three-tuples\n";
print "the rrd file is named after the parmfile by replacing parm with rrd\n";
exit;
}

#main

my ($parmfile, $rrdfile);
my (@t);

usage() if ($#ARGV != 0);
$parmfile = $ARGV[0];
$rrdfile = $parmfile;
$rrdfile =~ s/\.parms//;
$rrdfile .= ".rrd";

open(PARMS, "<", $parmfile) or die "Can't open parmfile\n";
while(<PARMS>) {
	next if /^[ \t]*#/;
	push(@t, [/^([^# ]+),([^# ]+),([^# ]+)/]);
	usage() if (!$t[-1][0]);
}
close(PARMS) or die "Can't close parmfile";

RRDs::create($rrdfile, "--no-overwrite",
	make_ds(@t),
	"RRA:AVERAGE:0.5:1:600",
	"RRA:AVERAGE:0.5:6:700",
	"RRA:AVERAGE:0.5:24:775",
	"RRA:AVERAGE:0.5:288:797",
	"RRA:AVERAGE:0.5:2016:1400",
# MIN and MAX over one sample are the same as AVERAGE
	"RRA:MIN:0.5:6:700",
	"RRA:MIN:0.5:24:775",
	"RRA:MIN:0.5:288:797",
	"RRA:MIN:0.5:2016:1400",
	"RRA:MAX:0.5:6:700",
	"RRA:MAX:0.5:24:775",
	"RRA:MAX:0.5:288:797",
	"RRA:MAX:0.5:2016:1400",
	);
