#!/usr/bin/perl -w
use Date::Manip;

#
# This simple script scans a directory of RRD files and changes the data
# type from $fromtype to $totype
#
# It writes the changes to $output_dir after making a backup in $backup_dir
# 

my $data_dir = "/var/lib/rrd";
my $backup_dir = "/home/nb/scripts/rrd-backup";
my $output_dir = "/home/nb/scripts/rrd-output";
my $restore_cmd = "/usr/local/bin/rrdtool restore";
my $parse_cmd = "/usr/local/bin/rrdtool dump";

my $fromtype = "COUNTER";
my $totype = "ABSOLUTE";


foreach $rrd (`ls -1 $data_dir`)
{
	my $maxctr = 0;

	chop $rrd;
	print "Working on $data_dir/$rrd:\n";

	print "    Backup: ";
	`cp $data_dir/$rrd $backup_dir`;
	print "done.\n";

	open (OUTFILE, ">$output_dir/$rrd.xml");
	open (PARSE, "$parse_cmd $data_dir/$rrd |");

	while (<PARSE>)
	{
		my $newline = $_;

		if ( $newline =~ /$fromtype/ )
		{
			print "\tFound: $newline";
			$newline =~ s/$fromtype/$totype/g;
		}

		print OUTFILE "$newline";
	}

	close PARSE;
	close OUTFILE;

	`rm $output_dir/$rrd 2>/dev/null`;
	`$restore_cmd $output_dir/$rrd.xml $output_dir/$rrd`;
}

exit 0;



