[rrd-users] rrdtrim: a script to "trim" DS values

steve rader rader at teak.wiscnet.net
Tue Feb 1 22:33:37 MET 2000


 > From: steve rader
 > 
 > Does anyone have a script that will replace DS values
 > above some value to a new value?  In other words, I need to
 > "trim" DS values to above a certain ceiling to a new value
 > (usually NaN.)
 > 
 > I know I can sorta do this with 
 > 
 >  rrdtool restore some.rrd some.rrd --range-check
 > 
 > but there are times when I want to set a ceiling that is
 > lower than a RRD's DS max vals.

Here's another 'en for contrib dir: "rrdtrim" replaces DS values above
an arbitrary value to a new value.  For example, given data for a T1
circuit, doing

  rrdtrim some-t1.rrd 192000 192000

will "trim" the values of some-t1.rrd to the 1.536 Mbits/sec (192000
Mbytes/sec.)  This will leave the orginal .rrd file as some-t1.rrd.old
and will print Unix diff style output of the DS value changes in XML 
format.

later
steve
- - -
systems guy
wiscnet.net
- - - cut here - - -
#!/usr/bin/perl
#
# rrdtrim - "trim" DS values above a certain max value to
# a given value.
#
# For example, given an RRD with data for traffic on a 56k
# frame-relay circuit, "throw away" (set to NaN) any values that
# exceed 56 kbits/sec (7000 bytes/sec) like so:
#
#   rrdtrim some.rrd 7000 NaN 
#
# steve rader
# <rader at wiscnet.net>
# Jan 25th, 2000
#
# $Id: rrdtrim.in,v 1.3 2000/01/27 22:23:33 rader Exp $
#

$exec_prefix = '/usr/local/rrdtool';
$rrdtool = "${exec_prefix}/bin/rrdtool";

if ( $#ARGV != 2 ) {
  print "usage: rrdtrim some.rrd max_value new_value\n";
  exit 1;
}

$rrd = $ARGV[0]; $trim = $ARGV[1]; $newval = $ARGV[2];

if ( ! -f "$rrd" ) {
  print "can\'t access $ARGV[0]\n";
  print "usage: rrdtrim some.rrd max_value new_value\n";
  exit 1;
}

if ( $newval =~ /e/ ) {
  print "can\'t use scientific notation for new_value!\n";
  print "usage: rrdtrim some.rrd max_value new_value\n";
  exit 1;
}

`mv $rrd $rrd.old`;

open(IN,"$rrdtool dump $rrd.old|");
while (<IN>) { push @in,$_; }
close(IN);

open(OUT,">$rrd.xml");
foreach $line (@in) {
  $orig_line = $line;
  foreach $v (split(/<value>/,$line)) {
    if ( $v =~ /\s*(\d\.\d+)e\+(\d+)\s*<\/value>/ ) {
      # convert e to float...
      $float = $1; $power = $2;
      $val = $float*(10**$power);
      if ( $val > $trim ) { 
        $line =~ s/<value>\s*${float}e\+${power}/<value> $newval/;
      }
    }
  }
  foreach $v (split(/<v>/,$line)) {
    if ( $v =~ /\s*(\d\.\d+)e\+(\d+)\s*<\/v>/ ) {
      # convert e to float...
      $float = $1; $power = $2;
      $val = $float*(10**$power);
      if ( $val > $trim ) { 
        $line =~ s/<v>\s*${float}e\+${power}/<v> $newval/;
      }
    }
  }
  if ( $line ne $orig_line ) { 
      print " < $orig_line";
      print " > $line";
  }
  print OUT "$line"; 
}
close(OUT);

`$rrdtool restore $rrd.xml $rrd`;

unlink $rrd.xml;

exit 0;



--
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



More information about the rrd-users mailing list