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`;
}
}