For anyone interested, here's what I've come up with so far. Next comes the fun part of adding graph items.
Code: Select all
#!/usr/bin/perl
use DBI;
use Time::Local;
# Set our start time to 7:55AM, today. Might not be correct.
($day, $month, $year) = (localtime)[3,4,5];
$start_time = timelocal('00', '55', '7', $day, $month, $year);
# Get all the Ntop files that another script has already copied and renamed
# into the Cacti 'rra' directory, i.e. MIAFL_192.168.123.210_bytesRcvd.rrd &
# MIAFL_192.168.123.210_bytesSent.rrd.
#
open(FILES,"ls -1 /var/www/html/mrtg/cacti/rra/*_bytes*.rrd |");
@output = <FILES>;
foreach $file (@output) {
if ( $file =~ /.*bytesSent.rrd/ ) {
push( @{$sites{substr($file, 29, 5)}{'Sent'}}, $file );
}
elsif ( $file =~ /.*bytesRcvd.rrd/ ) {
push( @{$sites{substr($file, 29, 5)}{'Rcvd'}}, $file );
}
else {
next;
}
}
close(FILES);
$dsn = "DBI:mysql:database=cacti;host=localhost";
$dbh = DBI->connect($dsn, "cacti", "cacti")
or die "Couldn't connect to database: $DBI::errstr\n";
foreach $site ( sort keys %sites ) {
$site_id = undef;
$sth = $dbh->prepare("SELECT id FROM host WHERE description = \"$site\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$site_id = $ref->{ id };
}
# If the host doesn't exist, add a new host.
unless ( $site_id ) {
$dbh->do("INSERT INTO host (description, hostname, disabled) VALUES (\"$site\", \"localhost\", \"on\")");
$sth = $dbh->prepare("SELECT id FROM host WHERE description = \"$site\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$site_id = $ref->{ id };
}
}
# Process each _bytesRcvd.rrd file.
$def_letter = 'a';
$cdefa = '';
$rcvd_counter = 0;
foreach $file ( @{$sites{$site}{'Rcvd'}} ) {
chomp($file);
$file = substr($file, 29);
$local_data_id = undef;
$sth = $dbh->prepare("SELECT id FROM data_template_data WHERE name = \"$file\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$local_data_id = $ref->{ id };
}
# If this data source doesn't exist, create a new data source.
unless ( $local_data_id ) {
$dbh->do("INSERT INTO data_local (host_id) VALUES (\"$site_id\")");
$sth = $dbh->prepare("SELECT id FROM data_local ORDER BY id DESC LIMIT 0,1");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$local_data_id = $ref->{ id };
}
$dbh->do("INSERT INTO data_template_data (local_data_id, name, name_cache, data_source_path, active, rrd_step)
VALUES (\"$local_data_id\",\"$file\", \"$file\", \"<path_rra>\/$file\", \"on\", 300)");
$sth = $dbh->prepare("SELECT id FROM data_template_data ORDER BY id DESC LIMIT 0,1");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$data_temp_data_id = $ref->{ id };
}
for ($i=1; $i <= 4; $i++) {
$dbh->do("INSERT INTO data_template_data_rra (data_template_data_id, rra_id) VALUES (\"$data_temp_data_id\",\"$i\")");
}
$dbh->do("INSERT INTO data_template_rrd (local_data_id, rrd_heartbeat, data_source_type_id, data_source_name) VALUES (\"$local_data_id\", 600, 2, \"counter\")");
}
$cdefa = $cdefa . "TIME,$start_time,GT,$def_letter,$def_letter,UN,0,$def_letter,IF,IF,";
$def_letter++;
$rcvd_counter++;
}
# Process each _bytesSent.rrd file.
$cdefb = '';
$sent_counter = 0;
foreach $file ( @{$sites{$site}{'Sent'}} ) {
chomp($file);
$file = substr($file, 29);
$local_data_id = undef;
$sth = $dbh->prepare("SELECT id FROM data_template_data WHERE name = \"$file\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$local_data_id = $ref->{ id };
}
unless ( $local_data_id ) {
$dbh->do("INSERT INTO data_local (host_id) VALUES (\"$site_id\")");
$sth = $dbh->prepare("SELECT id FROM data_local ORDER BY id DESC LIMIT 0,1");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$local_data_id = $ref->{ id };
}
$dbh->do("INSERT INTO data_template_data (local_data_id, name, name_cache, data_source_path, active, rrd_step) VALUES (\"$local_data_id\",\"$file\", \"$file\", \"<path_rra>\/$file\", \"on\", 300)");
$sth = $dbh->prepare("SELECT id FROM data_template_data ORDER BY id DESC LIMIT 0,1");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$data_temp_data_id = $ref->{ id };
}
for ($i=1; $i <= 4; $i++) {
$dbh->do("INSERT INTO data_template_data_rra (data_template_data_id, rra_id) VALUES (\"$data_temp_data_id\",\"$i\")");
}
$dbh->do("INSERT INTO data_template_rrd (local_data_id, rrd_heartbeat, data_source_type_id, data_source_name) VALUES (\"$local_data_id\", 600, 2, \"counter\")");
}
$cdefb = $cdefb . "TIME,$start_time,GT,$def_letter,$def_letter,UN,0,$def_letter,IF,IF,";
$def_letter++;
$sent_counter++;
}
for($count = 1; $count < $rcvd_counter; $count++) {
$cdefa = $cdefa . '+,';
}
chop($cdefa); # remove the extra ',' from the last entry.
for($count = 1; $count < $sent_counter; $count++) {
$cdefb = $cdefb . '+,';
}
chop($cdefb); # remove the extra ',' from the last entry.
$cdef_id = undef;
$sth = $dbh->prepare("SELECT id FROM cdef WHERE name = \"$site Inbound\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$cdef_id = $ref->{ id };
}
unless ( $cdef_id ) {
$dbh->do("INSERT INTO cdef (hash, name) VALUES (MD5(\"$site Inbound\"),\"$site Inbound\")");
$sth = $dbh->prepare("SELECT id FROM cdef WHERE name = \"$site Inbound\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$cdef_id = $ref->{ id };
}
$dbh->do("INSERT INTO cdef_items (hash, cdef_id, sequence, type, value) VALUES (MD5(\"$cdefb\"), $cdef_id, 1, 6, \"$cdefb\")");
}
else {
$dbh->do("UPDATE cdef_items SET hash=MD5(\"$cdefb\"), value=\"$cdefb\" WHERE cdef_id = $cdef_id");
}
$cdef_id = undef;
$sth = $dbh->prepare("SELECT id FROM cdef WHERE name = \"$site Outbound\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$cdef_id = $ref->{ id };
}
unless ( $cdef_id ) {
$dbh->do("INSERT INTO cdef (hash, name) VALUES (MD5(\"$site Inbound\"),\"$site Outbound\")");
$sth = $dbh->prepare("SELECT id FROM cdef WHERE name = \"$site Inbound\"");
$sth->execute();
while ( $ref = $sth->fetchrow_hashref() ) {
$cdef_id = $ref->{ id };
}
$dbh->do("INSERT INTO cdef_items (hash, cdef_id, sequence, type, value) VALUES (MD5(\"$cdefa\"), $cdef_id, 1, 6, \"$cdefa\")");
}
else {
$dbh->do("UPDATE cdef_items SET hash=MD5(\"$cdefa\"), value=\"$cdefa\" WHERE cdef_id = $cdef_id");
}
}
$dbh->disconnect();
This is what I'm trying to automatically replicate within Cacti.
Code: Select all
/usr/local/rrdtool/bin/rrdtool graph - \
--imgformat=PNG \
--start=1142949300 \
--end=1142982000 \
--title="MIAFL Bandwidth" \
--base=1000 \
--height=120 \
--width=600 \
--alt-autoscale-max \
--lower-limit=0 \
--vertical-label="bits per second" \
--slope-mode \
DEF:a="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.58.234_bytesRcvd.rrd":counter:AVERAGE \
DEF:b="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.62.218_bytesRcvd.rrd":counter:AVERAGE \
DEF:c="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.177.201_bytesRcvd.rrd":counter:AVERAGE \
DEF:d="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.116.100_bytesRcvd.rrd":counter:AVERAGE \
DEF:e="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.182.226_bytesRcvd.rrd":counter:AVERAGE \
DEF:f="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.197.63_bytesRcvd.rrd":counter:AVERAGE \
DEF:g="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.198.131_bytesRcvd.rrd":counter:AVERAGE \
DEF:h="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.166.113_bytesRcvd.rrd":counter:AVERAGE \
DEF:i="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.171.33_bytesRcvd.rrd":counter:AVERAGE \
DEF:j="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.175.193_bytesRcvd.rrd":counter:AVERAGE \
DEF:k="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.170.157_bytesRcvd.rrd":counter:AVERAGE \
DEF:l="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.182.184_bytesRcvd.rrd":counter:AVERAGE \
DEF:m="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.58.234_bytesSent.rrd":counter:AVERAGE \
DEF:n="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.62.218_bytesSent.rrd":counter:AVERAGE \
DEF:o="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.177.201_bytesSent.rrd":counter:AVERAGE \
DEF:p="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.116.100_bytesSent.rrd":counter:AVERAGE \
DEF:q="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.182.226_bytesSent.rrd":counter:AVERAGE \
DEF:r="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.197.63_bytesSent.rrd":counter:AVERAGE \
DEF:s="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.198.131_bytesSent.rrd":counter:AVERAGE \
DEF:t="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.166.113_bytesSent.rrd":counter:AVERAGE \
DEF:u="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.171.33_bytesSent.rrd":counter:AVERAGE \
DEF:v="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.175.193_bytesSent.rrd":counter:AVERAGE \
DEF:w="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.170.157_bytesSent.rrd":counter:AVERAGE \
DEF:x="/var/www/html/mrtg/cacti/rra/MIAFL_192.168.182.184_bytesSent.rrd":counter:AVERAGE \
CDEF:cdefa=TIME,1142985480,GT,a,a,UN,0,a,IF,IF,TIME,1142985480,GT,b,b,UN,0,b,IF,IF,TIME,1142985480,GT,c,c,UN,0,c,IF,IF,TIME
,1142985480,GT,d,d,UN,0,d,IF,IF,TIME,1142985480,GT,e,e,UN,0,e,IF,IF,TIME,1142985480,GT,f,f,UN,0,f,IF,IF,TIME,1142985480,GT,
g,g,UN,0,g,IF,IF,TIME,1142985480,GT,h,h,UN,0,h,IF,IF,TIME,1142985480,GT,i,i,UN,0,i,IF,IF,TIME,1142985480,GT,j,j,UN,0,j,IF,I
F,TIME,1142985480,GT,k,k,UN,0,k,IF,IF,TIME,1142985480,GT,l,l,UN,0,l,IF,IF,+,+,+,+,+,+,+,+,+,+,+ \
CDEF:cdefb=TIME,1142985480,GT,m,m,UN,0,m,IF,IF,TIME,1142985480,GT,n,n,UN,0,n,IF,IF,TIME,1142985480,GT,o,o,UN,0,o,IF,IF,TIME
,1142985480,GT,p,p,UN,0,p,IF,IF,TIME,1142985480,GT,q,q,UN,0,q,IF,IF,TIME,1142985480,GT,r,r,UN,0,r,IF,IF,TIME,1142985480,GT,
s,s,UN,0,s,IF,IF,TIME,1142985480,GT,t,t,UN,0,t,IF,IF,TIME,1142985480,GT,u,u,UN,0,u,IF,IF,TIME,1142985480,GT,v,v,UN,0,v,IF,I
F,TIME,1142985480,GT,w,w,UN,0,w,IF,IF,TIME,1142985480,GT,x,x,UN,0,x,IF,IF,+,+,+,+,+,+,+,+,+,+,+ \
CDEF:cdefc=cdefa,8,* \
CDEF:cdefd=cdefb,8,* \
AREA:cdefd#00FF00:"Inbound" \
GPRINT:cdefd:LAST:" Current\:%8.2lf %s" \
GPRINT:cdefd:AVERAGE:"Average\:%8.2lf %s" \
GPRINT:cdefd:MAX:"Maximum\:%8.2lf %s\n" \
LINE1:cdefc#0000FF:"Outbound" \
GPRINT:cdefc:LAST:"Current\:%8.2lf %s" \
GPRINT:cdefc:AVERAGE:"Average\:%8.2lf %s" \
GPRINT:cdefc:MAX:"Maximum\:%8.2lf %s\n"