This script is a Perl script and requires Mysql and RRDs modules. the RRDs module comes with RRDTool, and the MySQL module is in the DBD::MySQL module on CPAN.
this script should go into your cacti directory. Give this file restrictive permissions, like 600 or 660, since it will be residing inside the webroot.
Edit the variables at the beginning of the file to match the settings of your server.
Code: Select all
maxset.pl
#!/usr/bin/perl
use Mysql;
use RRDs;
###
### Edit these values
###
my $rrdpath = "/var/www/html/cacti/rra/";
my $rrdtool = "/usr/local/rrdtool-1.0.38/bin/rrdtool";
###
### The code below this line should not have to be edited
###
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";
}
$db = Mysql->connect($sqlhost, $dbase, $user, $pass) or die ("Cannot connect to MySQL server on $sqlhost");
$query = $db->query("SELECT DSPath,MaxValue,DSName FROM rrd_ds");
while (my($dbdspath,$dbmaxvalue, $dbdsname) = $query->fetchrow_array()) {
@fstrrd = split("/", $dbdspath);
$fstrrd[1] =~ s/\.rrd//;
$rrd = $fstrrd[1];
$file = $rrdpath . $rrd . "\.rrd";
if ( ! -e $file) { die ("File $file does not exist"); }
$rrdoutput = `$rrdtool info $rrdpath$rrd\.rrd | grep "].max"`;
chomp($rrdoutput);
@dsplit = split(" = ", $rrdoutput);
$rrdmaxvalue = $dsplit[1];
@subs = split("e", $rrdmaxvalue);
$subs[1] = (substr($subs[1], 1)) * 1;
$rrdvalue = $subs[0] * (10 ** $subs[1]);
if ( $dbmaxvalue ne $rrdvalue ) {
RRDs::tune($file, "-a", $dbdsname . ":" . $dbmaxvalue);
my $err=RRDs::error;
print "$file changed from $rrdvalue to $dbmaxvalue\n";
if ($err) {
warn "ERROR updating $file: $err\n"
};
}
}