|
Packit |
667938 |
#!/usr/bin/perl
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# ipchainacc version 1.0.2
|
|
Packit |
667938 |
# Author: John Lange, john@darkcore.net
|
|
Packit |
667938 |
Ilya Konstantinov, mrtg-ipchains@future.galanet.net
|
|
Packit |
667938 |
# Date: Sep 06, 2000
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# This script was written to provide output for MRTG
|
|
Packit |
667938 |
# (multi router traffic grapher) via ipchains.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
## USAGE EXAMPLE (line from mrtg.conf)
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# Target[traffic]: /usr/local/mrtg-2/bin/ipchainacc 0 0
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# The arguments 0 0 mean it should report the bytes count
|
|
Packit |
667938 |
# for the 1st (1st is 0, 2st is 1 and so on) rule in
|
|
Packit |
667938 |
# the 'acctin' chain (supposed to be the incoming traffic)
|
|
Packit |
667938 |
# and the 1st rule in the 'acctout' chain.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
## Changelog
|
|
Packit |
667938 |
# v1.0.2 Made it actually account bytes instead of packages.
|
|
Packit |
667938 |
# Now ipchainacc requires 2 command-line arguments.
|
|
Packit |
667938 |
# This allows having more than one rule in the accounting
|
|
Packit |
667938 |
# chains, so you could have traffic cuts by protocol
|
|
Packit |
667938 |
# or IP blocks which match your rules.
|
|
Packit |
667938 |
# v1.0.1 added -x to ipchains to expand byte counters
|
|
Packit |
667938 |
# v1.0.0 Inital Release
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# This command must return 4 lines of output:
|
|
Packit |
667938 |
# Line 1 : current state of the 'incoming bytes counter'
|
|
Packit |
667938 |
# Line 2 : current state of the 'outgoing bytes counter'
|
|
Packit |
667938 |
# Line 3 : string, telling the uptime of the target.
|
|
Packit |
667938 |
# Line 4 : string, telling the name of the target.
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# This script relies on you having setup your ipchains rules beforehand
|
|
Packit |
667938 |
|
|
Packit |
667938 |
## edit for your system
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$ipchains='/sbin/ipchains'; # path to ipchains
|
|
Packit |
667938 |
$host=`/bin/hostname --fqdn`; # local hostname (for information only)
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# if you use the ipchains rules above, you don't have to change these
|
|
Packit |
667938 |
$inrule='acctin'; # name of input accounting rule
|
|
Packit |
667938 |
$outrule='acctout'; # name of output accounting rule
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
## -- don't edit below here ----
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# get command line params
|
|
Packit |
667938 |
my ($in_rule,$out_rule) = @ARGV;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# fetch the status from ipchains
|
|
Packit |
667938 |
@rules=`$ipchains -L $inrule -v -n -x`;
|
|
Packit |
667938 |
splice @rules, 0, 2;
|
|
Packit |
667938 |
@in_bytes=split(' ',$rules[$in_rule]);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
@rules=`$ipchains -L $outrule -v -n -x`;
|
|
Packit |
667938 |
splice @rules, 0, 2;
|
|
Packit |
667938 |
@out_bytes = split(' ',$rules[$out_rule]);
|
|
Packit |
667938 |
print "$out_line"; # just for debugging
|
|
Packit |
667938 |
|
|
Packit |
667938 |
#$c=1;
|
|
Packit |
667938 |
#foreach $value (@in_bytes) {
|
|
Packit |
667938 |
# printf "$c: $value\n";
|
|
Packit |
667938 |
# $c++;
|
|
Packit |
667938 |
#};
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# uptime of the machine
|
|
Packit |
667938 |
my $upTimeLine = `uptime`;
|
|
Packit |
667938 |
my $upTime;
|
|
Packit |
667938 |
if ($upTimeLine =~ /\s+?.*?\s+?up (\d+ days,\s+.*?),/)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$upTime = $1;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# 4 lines of output only.
|
|
Packit |
667938 |
if (!$in_bytes[1]) { $in_bytes[1] = 0; }
|
|
Packit |
667938 |
if (!$out_bytes[1]) { $out_bytes[1] = 0; }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
print "$in_bytes[1]\n",
|
|
Packit |
667938 |
"$out_bytes[1]\n",
|
|
Packit |
667938 |
"$upTime\n$host";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
#print ($in_bytes[1] ne ''?$in_bytes[1]:'0'),"\n",
|
|
Packit |
667938 |
# ($out_bytes[1] ne ''?$out_bytes[1]:'0'),"\n",
|
|
Packit |
667938 |
# "$upTime\n$host";
|