Want to remove a spike from a graph?

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

wesphillips
Posts: 21
Joined: Tue May 28, 2002 8:59 am

Want to remove a spike from a graph?

Post by wesphillips »

When a 100MB ethernet interface that normally passes 20MB gets reset, there is usually a big (around 80MB) spike that renders the normal data just a trickle on the bottom of the graph. This script fixes the problem by removing the spike from the graph. Since the data that you really wanted is not available, it cannot put valid data in the graph, but by removing the spike, the autoscale feature returns the rest of the data to normal levels.
This is a Perl script and you will need the Mysql module(I believe it is contained in the DBD::MySQL module). You will need to edit the values in the beginning of the script to reflect the correct settings for your server. You will need to edit the following:

The RRD file path. On Redhat, the default location is :
/var/www/html/cacti/rra/

The path to the rrdtool binary(including the filename). The default for version 1.0.38 is:
/usr/local/rrdtool-1.0.38/bin/rrdtool

And the group that apache runs under. The defaults are either "nobody" or "apache"

The script assumes that you use the "<path_rra>" cacti variable in your data source path in cacti. If you don't, you will need to modify the script.

The script should be placed in your cacti directory, as it looks for config.php in the include subdirectory.

Give the script restrictive permissions, like 600 or 660, since it is in the webroot.

Without further ado, here is the script:

Code: Select all

nospike.pl

#!/usr/bin/perl
        
###
### Edit these values to match your server's settings
###
my $rrdpath = "/var/www/html/cacti/rra/";
my $rrdtool = "/usr/local/rrdtool-1.0.38/bin/rrdtool";
my $apacheuser = "apache";

###
### Nothing should have to be edited after this line
###
use Mysql;

if (-e './include/config.php') {
  open (CFGFILE, './include/config.php') or die "Couldn't open the config.php file\n";   
    for $config (<CFGFILE>) {
      chomp $config;
      if ($config =~ /^\$database_default/) {
        $dbase = (split /\"/, $config)[1];
        $p++;
        next;
      } elsif ($config =~ /^\$database_hostname/) {
        $sqlhost = (split /\"/, $config)[1];
        $p++;
        next;
      } elsif ($config =~ /^\$database_username/) {
        $user = (split /\"/, $config)[1];
        $p++;
        next;
      } elsif ($config =~ /^\$database_password/) {
        $pass = (split /\"/, $config)[1];
        $p++; 
        next;
      }
  }
} else {
  die "could not find config.php";
}
  
print "What rrd file are you trying to remove a spike from?\nDon't type the extension (.rrd), just the name\n";
print "(Example: traf_1_1_1_1.rrd rould be traf_1_1_1_1):";
$rrd = <STDIN>;
chomp($rrd);
$rrdoutput = `/usr/local/rrdtool-1.0.38/bin/rrdtool info /var/www/html/cacti/rra/$rrd\.rrd | grep "].max"`;
chomp($rrdoutput);
$db = Mysql->connect($sqlhost, $dbase, $user, $pass) or die ("Cannot connect to MySQL server on $sqlhost");
$query = $db->query("SELECT MaxValue FROM rrd_ds WHERE DSPath=\"<path_rra>/$rrd\.rrd\"");
$dbvalue = $query->fetchrow;
$dbvalue = $dbvalue * 1;
@dsplit = split(" = ", $rrdoutput);
$dsplit[0] =~ s/ds\[//;
$dsplit[0] =~ s/\]\.max//;
$dsname = $dsplit[0];
$rrdmaxvalue = $dsplit[1];
@subs = split("e", $rrdmaxvalue);
$subs[0] = $subs[0] * 1;
$subs[1] = (substr($subs[1], 1)) * 1;
$rrdvalue = $subs[0] * (10 ** $subs[1]);
if ($rrdvalue != $dbvalue) {
  print "The rrd file($rrdvalue) and Cacti's database($dbvalue) values don't match! Run maxset.pl first.";
  die;
}
print "The MaxValue for that rrd is $rrdvalue. Do you wish to temporarily change this value?(y/n)";
$yn = <STDIN>;
chomp($yn);  
if ($yn eq "y") {
  print "What value do you want to insert into the rrd?";
  $newmax = <STDIN>;
  chomp($newmax);
  `$rrdtool tune $rrdpath.$rrd\.rrd -a $dsname:$newmax`;
  print "The value $newmax was applied to the rrd and will be used for the spike removal";
} else {
  print "The value $rrdvalue is going to be used for the spike removal";
}
print "Press enter to continue, or CTRL-C to abort...";
$x = <STDIN>;
`$rrdtool dump $rrdpath.$rrd\.rrd > /tmp/$rrd\.xml`;
`/bin/rm -f $rrdpath.$rrd\.rrd`;
`$rrdtool restore /tmp/$rrd\.xml $rrdpath.$rrd\.rrd -r`;
`/bin/chown $user."\.".$apacheuser $rrdpath.$rrd\.rrd`;
`/bin/chmod 664 $rrdpath.$rrd\.rrd`;
if ($yn eq "y") {
  print "Do you wish to return the maxvalue of $rrd\.rrd from $newmax to $rrdvalue?(y/n) ";
  $ny = <STDIN>;
  if ($ny eq "y") {  
    `$rrdtool tune $rrdpath.$rrd\.rrd -a $dsname:$rrdvalue`;
  }
}
Watch for another script that compares the maxvalue in the RRD and the MySQL database and updates the RRD to match.
User avatar
TFC
Cacti Pro User
Posts: 739
Joined: Wed Apr 09, 2003 2:17 am
Location: Izmir/Turkey

Post by TFC »

How can I use this script? Can u help me?
Steve

Script fails.

Post by Steve »

I have updated the path to rrd '/usr/bin/rrdtool' - yet it quotes the following below.
_______________________
[root@Saturn cacti]# perl nospike.pl
What rrd file are you trying to remove a spike from?
Don't type the extension (.rrd), just the name
(Example: traf_1_1_1_1.rrd rould be traf_1_1_1_1):vlan_switch_cisco_catalyst_3524xl_traffic_in_60
sh: line 1: /usr/local/rrdtool-1.0.38/bin/rrdtool: No such file or directory
DBD::mysql::st execute failed: Table 'cacti.rrd_ds' doesn't exist at /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/Mysql.pm line 167, <STDIN> line 1.
Can't call method "fetchrow" on an undefined value at nospike.pl line 49, <STDIN> line 1.
______________________

Can you help???
cwlab-admin
Posts: 15
Joined: Sat Jul 26, 2003 1:10 am
Location: Chicago, IL
Contact:

Post by cwlab-admin »

check line 47. there is a hard coded path that should be changed to

Code: Select all

$rrdoutput = `$rrdtool info $rrdpath/$rrd\.rrd | grep "].max"`;
still havent figured out the rest though
cwlab-admin
Posts: 15
Joined: Sat Jul 26, 2003 1:10 am
Location: Chicago, IL
Contact:

Post by cwlab-admin »

fixed it finally. im guessing since that post is about 1 year old it was for a cacti version that most of us dont use anymore.

Code: Select all


#!/usr/bin/perl

#############################################
# modified from wesphillips script at
# http://www.raxnet.net/board/viewtopic.php?t=1663
#
# traffic_in / traffic_out spike removal
#
# cacti .84
# rrdtool 1.039
# perl 5.8
#
# tony zoeteman - tony@cticonnect.net
#############################################

#
# Edit these values to match your server's settings
#
my $rrdpath = "/home/sites/cwlab-net-admin/public_html/status/traffic/rra/";
my $rrdtool = "/usr/bin/rrdtool";
my $cactiuser = "cwlab-admin";


#
# Nothing should have to be edited after this line
#

#check proper arguments
#
$ARGC = @ARGV;
if ($ARGC != 2) {
   print "usage: perl nospike.pl filename maxvalue(Mb)\n";
   exit;
}

# argument 1 is the name of the rrd
#
$rrd = $ARGV[0];
chomp($rrd);

#argument 2 is the maxvalue to scale to
$newmax = $ARGV[1];
chomp($newmax);

# use rrdtool to find maxvalue in the current rrd
#
$rrdoutput = `$rrdtool info $rrdpath/$rrd | grep "].max" -m1`;
chomp($rrdoutput);
@dsplit = split(" = ", $rrdoutput);
$dsplit[0] =~ s/ds\[//;                         #strip leading crap
$dsplit[0] =~ s/\]\.max//;                      #strip trailing crap
$dsname = $dsplit[0];
$rrdmaxvalue = $dsplit[1];                      #in scientific notation as a string
@subs = split("e", $rrdmaxvalue);               #split scientific notation number into two strings
$subs[0] = $subs[0] * 1;                        #change the coefficient into a integer
$subs[1] = (substr($subs[1], 1)) * 1;           #drop the 'e', change the base into an integer
$rrdvalue = $subs[0] * (10 ** $subs[1]);        #multiply to create the maxvalue as an integer

# scale the rrd
#
`$rrdtool tune $rrdpath$rrd -a traffic_in:$newmax`;
`$rrdtool tune $rrdpath$rrd -a traffic_out:$newmax`;

`$rrdtool dump $rrdpath$rrd > /tmp/$rrd\.xml`;
`/bin/mv $rrdpath$rrd $rrdpath$rrd\.bak`;
`$rrdtool restore /tmp/$rrd\.xml $rrdpath$rrd -r`;
`/bin/chown $cactiuser.$cactiuser $rrdpath$rrd`;
`/bin/chmod 644 $rrdpath$rrd`;

# restore maxvalue as first set in rrd
#
`$rrdtool tune $rrdpath$rrd -a traffic_in:$rrdvalue`;
`$rrdtool tune $rrdpath$rrd -a traffic_out:$rrdvalue`;


Guest

Post by Guest »

when runnign i get error on grep -m1 ... i did man grep and cannot find the -m option what is -m supposed to do, or did you mean -n ?
grep: invalid option -- m
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
xenic

Get rid of spikes - for good

Post by xenic »

I've been running RRD databases with a style of 'DERIVE' and a min value of 0, instead of 'COUNTER' , this efficiently removes spikes , as 'negative' values are set to "unknown" .

(ie. at time 10:00 values for counter is 100 , at 10:05 value is 50 (counter loop / device restart) , at 10:10 value is 60 , and the value for 10:05 is set to U)

--Ilkka
cwlab-admin
Posts: 15
Joined: Sat Jul 26, 2003 1:10 am
Location: Chicago, IL
Contact:

Post by cwlab-admin »

Anonymous wrote:when runnign i get error on grep -m1 ... i did man grep and cannot find the -m option what is -m supposed to do, or did you mean -n ?
grep: invalid option -- m
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
the '-m' is ther to limit grep into returning just 1 line. the rrdtool info command will display tons of informations on yoru rrd, wich usually contains two data sources. since most interfaces have the inbound and outbound set with the same maxvalue we only need to use one line from the info dump to figure out the max.

http://unixhelp.ed.ac.uk/CGI/man-cgi?grep

if you cant get grep to return one line with that option, i would play with the regular expressions to make it strip down the useless info.
Guest

Post by Guest »

There is an error with the usage info. the scaling factor used as the second argument should be in bits per seconds, sorry
--
t

Code: Select all

#check proper arguments
#
$ARGC = @ARGV;
if ($ARGC != 2) {
   print "usage: perl nospike.pl filename maxvalue(bps)\n";
   exit;
} 
wesphillips
Posts: 21
Joined: Tue May 28, 2002 8:59 am

Post by wesphillips »

Sorry about that, I haven't checked this post in a while. The original code was written for cacti version 6, not 8. I didn't really try to rewrite the code, so I am glad that someone else did.
Hero

Post by Hero »

Is it possible to kill outgoing spikes sperated??

my line speed is 768/128KBits! I killed the spikes above 1MBit, OK, the incomming spike is away but there is still an outgoing spike up to 1 Mbit!
wesphillips
Posts: 21
Joined: Tue May 28, 2002 8:59 am

Post by wesphillips »

Hero wrote:Is it possible to kill outgoing spikes sperated??

my line speed is 768/128KBits! I killed the spikes above 1MBit, OK, the incomming spike is away but there is still an outgoing spike up to 1 Mbit!
It is possible. If you have two rra's in your rrd file, you need to make sure that the maxvalue is set correctly for both rra's in your rrd file. then you simply run rrdtool twice. First you run the rrdtool dump command(check the docs for the syntax) to create an xml dumpfile and then run the rrdtool restore command with the -r option on the xml file you just created. That kills any spikes in your file that are over the maxvalue in the rrd. You cannot use the script for this purpose, however, as it was not designed to handle the seperate maxvalues for multiple rra's.
User avatar
TFC
Cacti Pro User
Posts: 739
Joined: Wed Apr 09, 2003 2:17 am
Location: Izmir/Turkey

Post by TFC »

usage: perl nospike.pl filename maxvalue(Mb)
Mbits or Mbytes?
LAST:75.23Kbit
AVG:50.57kbit
MAX:1.76Mbit
So, I want to prun my graph from 0.2Mbit as max.value
I use command line (I assumed script is set for bit/sec):

Code: Select all

[root@localhost scripts]# perl nospike.pl m_4a_7507_traffic_in_12.rrd 0.2
and output:
...
...
...
out of range found [ds: 1], [value : 5.3440529199e+03]
out of range found [ds: 0], [value : 3.3372991178e+04]
out of range found [ds: 1], [value : 5.2113629556e+03]
out of range found [ds: 0], [value : 1.8155931167e+04]
out of range found [ds: 1], [value : 3.4990923000e+03]
out of range found [ds: 0], [value : 1.7887321245e+04]
out of range found [ds: 1], [value : 1.9071848067e+04]
/bin/chown: `cactiuser.cactiuser': invalid user
then my graph turns empty. My graph turns me AVG,LAST and MAX values as nan
Then I use( I assumed script set to bytes/sec):

Code: Select all

[root@localhost scripts]# perl nospike.pl m_4a_7507_traffic_in_12.rrd 0,025
answer is same and fraph is empty and values are "nan"
Did I anything wrong?
P.S: After rename myxxx.rrd.bak to xxx.rrd, graph can not be updated. Why?
wesphillips
Posts: 21
Joined: Tue May 28, 2002 8:59 am

Post by wesphillips »

TFC wrote:
usage: perl nospike.pl filename maxvalue(Mb)
Mbits or Mbytes?
LAST:75.23Kbit
AVG:50.57kbit
MAX:1.76Mbit
So, I want to prun my graph from 0.2Mbit as max.value
I use command line (I assumed script is set for bit/sec):

Code: Select all

[root@localhost scripts]# perl nospike.pl m_4a_7507_traffic_in_12.rrd 0.2
and output:
...
...
...
out of range found [ds: 1], [value : 5.3440529199e+03]
out of range found [ds: 0], [value : 3.3372991178e+04]
out of range found [ds: 1], [value : 5.2113629556e+03]
out of range found [ds: 0], [value : 1.8155931167e+04]
out of range found [ds: 1], [value : 3.4990923000e+03]
out of range found [ds: 0], [value : 1.7887321245e+04]
out of range found [ds: 1], [value : 1.9071848067e+04]
/bin/chown: `cactiuser.cactiuser': invalid user
then my graph turns empty. My graph turns me AVG,LAST and MAX values as nan
Then I use( I assumed script set to bytes/sec):

Code: Select all

[root@localhost scripts]# perl nospike.pl m_4a_7507_traffic_in_12.rrd 0,025
answer is same and fraph is empty and values are "nan"
Did I anything wrong?
P.S: After rename myxxx.rrd.bak to xxx.rrd, graph can not be updated. Why?
If you look at the output of the execution of the nospike.pl script, you will see the "/bin/chown: `cactiuser.cactiuser': invalid user" statement at the end. One of the things that I have discovered is that if the file owner/permissions are not correct, the cmd.php script cannot update to the file. Please remember that the file has to be readable by the user that the webserver runs as AND the file has to be read/write-able for the user you use to run cacti. This could be the source of BOTH problems that you are describing.
User avatar
TFC
Cacti Pro User
Posts: 739
Joined: Wed Apr 09, 2003 2:17 am
Location: Izmir/Turkey

Post by TFC »

I modify nopsike.pl (cactiuser=root) and I change my rra folder with chown -R nobody:nobody rra/
But still same thing occurs.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest