#!/usr/bin/perl
# replace unwanted numeric data in an rrd file with NaN values
# from Ed Ravin, eravin@panix.com, May 2002. License is GPL.
use strict;
use Date::Parse;
#################### user-configurable stuff
my $RRDTOOL= "rrdtool"; # location of rrdtool
my $TEMPFILE= "/tmp/rrdcut.$$.xml";
#################### end user-configurable stuff
my $usage= "Cut rows out of an RRD database based on date and time. Usage:
rrdcut -starttime 'DATE' -endtime 'DATE' rrdfile [...]
'DATE' format is anything accepted by Date::Parse str2time()
All rows in all datasources dated BETWEEN the start and end times will be
replaced with NaN values.
";
my $starttime;
my $endtime;
die $usage unless @ARGV > 1;
while (1)
{
die $usage unless @ARGV >= 1;
if ($ARGV[0] =~ /^-?-(start|end)time$/)
{
my $whichend= $1 . "time";
shift @ARGV;
my $thetime= str2time($ARGV[0]) ||
die "$0: unable to parse -$whichend option value\n";
$starttime= $thetime if ($whichend eq "starttime");
$endtime= $thetime if ($whichend eq "endtime");
}
elsif ($ARGV[0] =~/^-/)
{
die "$0: unknown option $ARGV[0]\n";
}
elsif ($ARGV[0] !~ /^-/)
{
last;
}
shift @ARGV;
}
die $usage unless $starttime and $endtime;
# sample row
# we want the ctime value: vvvvvvvvvv
# 1.2338352416e+07 8.0156175954e+06 0.0000000000e+00 1.4221113929e+07
foreach my $file (@ARGV)
{
open(RRDXML, "$RRDTOOL dump $file|") ||
die "$0: cannot rrddump $file: $!\n";
open(TEMP, ">$TEMPFILE") ||
die "$0: cannot open temp file $TEMPFILE for output: $!\n";
while()
{
if (m@^\s* \s* @ix )
{
my $rowtime= $1;
if ($starttime <= $rowtime and $rowtime <= $endtime)
{
s@ \s* \S+ \s* @ NaN @ixg;
}
}
print TEMP $_;
}
close TEMP || die "$0: failure closing temp file $TEMPFILE: $!\n";
rename ($file, "$file.old") || die "$0: failure renaming $file: $!\n";
my $rc= system "$RRDTOOL restore $TEMPFILE $file";
warn "$0: WARNING: rrdrestore fails, status $rc. $file may be corrupt.\n"
if $rc;
}
unlink $TEMPFILE unless
exists($ENV{"RRDCUT_DEBUG"}) and $ENV{"RRDCUT_DEBUG"} eq "YES";