You need to have a BSD system with pf (packet filter) installed.
Setup some rules in pf edit the config file in /etc/pf.conf adding rules like these
Code: Select all
# pass all loopback traffic
pass quick on lo0
# set up default rules to track non-specific traffic
pass in log label "other-in"
pass out log label "other-out"
# log all http traffic (192.168.0.11 being the IP apache or like is running on)
pass in quick proto tcp to 192.168.0.11 port 80 label "in-http"
pass out quick proto tcp from 192.168.0.11 port 80 label "out-http"
pfctl -nf /etc/pf.conf
if that outputs no error you can load the config file with this
pfctl -f /etc/pf.conf
If your server is live and receiving traffic you will be able to see traffic with the command
pfctl -s labels
Setup a crontab to dump the pf data to a text file ever 5 minutes resetting the counters each time
*/5 * * * * /sbin/pfctl -zsl > /home/cacti/bandwidth.dat
Now you can setup a script to output the data to cacti. I use a php script to do this stored in /home/cacti/public_html/scripts/bw.php
Code: Select all
#!/usr/local/bin/php
<?php
// get rule type
$rule = $argv[1];
// setup in and out rules
$rule_in = "in-$rule";
$rule_out = "out-$rule"
// grab the bandwidth file contents
$lines = file("/home/cacti/bandwidth.dat");
// loop through file lines
foreach($lines AS $line)
{
// if line is blank go to next
if(empty($line)) continue;
// split the line up
$parts = explode(" ", $line);
// setup an array and store the rules and bytes parts in it
$rw = array();
$rw['rule'] = $parts[0];
$rw['bytes'] = str_replace("\n", "", $parts[3]);
// check its the line that matches the rule
if($rw['rule'] != $rule_in && $rw['rule'] != $rule_out) continue;
// print out the values
print $rw['rule'].":".$rw['bytes']." ";
}
?>
Name: HTTP Bandwidth
Input Type: Script/Command
Input String: /home/cacti/public_html/scripts/bw.php http
Add two output fields
Field [Output]: in-http
Friendly Name: HTTP in traffic
and
Field [Output]: out-http
Friendly Name: HTTP out traffic
You can now setup a Data Source for this Input Method and a graph like normal.