[rrd-users] Adding a DS to an existing RRD

Jeff Rodriguez jeff at unixisgod.com
Wed Aug 27 01:19:34 MEST 2003


This something I needed, and something I know others have asked for. It's a quick perl script to add a DS to an existing RRD without nuking all the existing data.
*Instructions:*
rrdtool dump my.rrd > file.xml
./rrdAddDS.pl file.xml newDsName > new_file.xml
rrdtool restore new_file.xml my_new.rrd

Voila!

Here's a quick command line script I wrote to transform every RRD in the current directory.

opendir(DIR, "./");
my @files = grep { /\.rrd$/ } readdir(DIR);
foreach my $file (@files)
{
	system("rrdtool dump $file > temp.xml");
	system("~/rrdAddDS.pl temp.xml images > new.xml");
	unlink($file);
	system("rrdtool restore new.xml $file");
	unlink("temp.xml");
	unlink("new.xml");
}


Build on it as you wish, let me know if you likes, forward on any improvements!

-Jeff

------------------------------------------------------------------------

#!/usr/bin/perl
# rrdAddDS.pl

open(INF, @ARGV[0]) or die "Could not open '$ARGV[0]'\n";

while ($line = <INF>)
{
	chomp($line); # Strip off Newline characters

	##############
	# Add the DS #
	##############
	if ($insertDS == 1)
	{
		print <<EndDS;

        <ds>
                <name> $ARGV[1] </name>
                <type> GAUGE </type>
                <minimal_heartbeat> 600 </minimal_heartbeat>
                <min> 0.0000000000e+00 </min>
                <max> NaN </max>

                <!-- PDP Status -->
                <last_ds> UNKN </last_ds>
                <value> 1.8530980000e+01 </value>
                <unknown_sec> 0 </unknown_sec>
        </ds>
EndDS
		$insertDS = 0;
	}

	###################################
	# Insert DS under CDP_PREP entity #
	###################################
	if ($insertCDP_PREP == 1)
	{
		print "                        <ds><value> NaN </value>  <unknown_datapoints> 0 </unknown_datapoints></ds>\n";
		$insertCDP_PREP = 0;
	}

	###########################################
	# Look for end of the <lastupdate> entity #
	###########################################
	if ($line =~ /<\/lastupdate>/)
	{
		$insertDS = 1;
	}

	###########################################
	# Look for start of the <cdp_prep> entity #
	###########################################
	if ($line =~ /<cdp_prep>/)
	{
		$insertCDP_PREP = 1;
	}

	##############################
	# Look for the end of an RRA #
	##############################
	if ($line =~ /<\/database>/)
	{
		$parse = 0;
	}

	########################################################################
	# Add the extra "<v> NaN </v>" under the RRAs. Just print normal lines #
	########################################################################
	if ($parse == 1)
	{
		print substr($line, 0, -6);
		print "<v> NaN </v>";
		print substr($line, -6);
		print "\n";
	}
	else
	{
		print "$line\n";
	}

	################################
	# Look for the start of an RRA #
	################################
	if ($line =~ /<database>/)
	{
		$parse = 1;
	}
}

------------------------------------------------------------------------



--
Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/rrd-users
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the rrd-users mailing list