|
Packit |
667938 |
#!/usr/bin/perl
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# This is just a hack, but it works fine for me.
|
|
Packit |
667938 |
# I'm using the ip accounting kernel rules to feed the mrtg.
|
|
Packit |
667938 |
# You need to have IP accounting compiled in your kernel(2.0), or IPChains
|
|
Packit |
667938 |
# support if you are running 2.1 or 2.2
|
|
Packit |
667938 |
# Anyway I'm not using the ipchains in here as there's no need to.
|
|
Packit |
667938 |
# The old ipfwadm will do just fine.
|
|
Packit |
667938 |
# So for 2.1 or 2.2 you'll need the ipfwadm-wrapper.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# Cristian Caramida <brick@bbs.ro>
|
|
Packit |
667938 |
# but very soon <brick@pcnet.ro>
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# Usage:
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# You need to have IP accounting compiled in your kernel, and than just add
|
|
Packit |
667938 |
# to the mrtg.cfg
|
|
Packit |
667938 |
# Target[ip_addr]: `ipacc [mode] [-S aaa.bbb.ccc.ddd[/net_bits]] [port1 port2 ...] [-D aaa.bbb.ccc.ddd[/net_bits]] [port1 port2 ...]`
|
|
Packit |
667938 |
# where
|
|
Packit |
667938 |
# - mode - is one of "in" or "out" in lowercase
|
|
Packit |
667938 |
# - aaa.bbb.ccc.ddd - is a dotted IP address (not a host/net name)
|
|
Packit |
667938 |
# - net_bits - is the number of bits used by the network portion of the IP
|
|
Packit |
667938 |
# (eg. 24 for a whole C class, or 25 for half of a C class)
|
|
Packit |
667938 |
# - port1 port2 ... - the ports you want to plot trafic on
|
|
Packit |
667938 |
# I haven't tryied to call it without any parameters!
|
|
Packit |
667938 |
# It will probably make accounting for all trafic.
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$ipfwadm='/sbin/ipfwadm'; # <-------- ATTENTION
|
|
Packit |
667938 |
$host="com.bbs.ro";
|
|
Packit |
667938 |
#If you are running with ipchains (kernel 2.1, 2.2 or later) you may
|
|
Packit |
667938 |
#either symlink the ipfwadm-wrapper to that location or simply modify the value
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
#---------------------------------------------------------------------------
|
|
Packit |
667938 |
# Modify this only if you know what you are doing !!! :-)
|
|
Packit |
667938 |
$mode='both';
|
|
Packit |
667938 |
$remote='';
|
|
Packit |
667938 |
$port='8889';
|
|
Packit |
667938 |
if(!($ARGV[0] =~ /^\-/)){ ($mode)=@ARGV; shift @ARGV;}
|
|
Packit |
667938 |
if($mode eq 'in') {$mode='in ';}
|
|
Packit |
667938 |
$mode=($mode eq 'in')?'in ':$mode;
|
|
Packit |
667938 |
$source_ip='0.0.0.0/0';
|
|
Packit |
667938 |
$source_port='';
|
|
Packit |
667938 |
$dest_ip='0.0.0.0/0';
|
|
Packit |
667938 |
$dest_port='';
|
|
Packit |
667938 |
$prot='all';
|
|
Packit |
667938 |
$inBytes=0;
|
|
Packit |
667938 |
$outBytes=0;
|
|
Packit |
667938 |
$upTime=0;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
while ($ARGV[0] =~ /^\-/)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if($ARGV[0] eq '-S')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
shift @ARGV;
|
|
Packit |
667938 |
($source_ip)=@ARGV;
|
|
Packit |
667938 |
shift @ARGV;
|
|
Packit |
667938 |
while ($ARGV[0] =~ /[0123456789]+/)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$source_port.="$ARGV[0] ";
|
|
Packit |
667938 |
shift @ARGV;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
elsif($ARGV[0] eq '-D')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
shift @ARGV;
|
|
Packit |
667938 |
($dest_ip)=@ARGV;
|
|
Packit |
667938 |
shift @ARGV;
|
|
Packit |
667938 |
while ($ARGV[0] =~ /[0123456789]+/)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$dest_port.="$ARGV[0] ";
|
|
Packit |
667938 |
shift @ARGV;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
else {
|
|
Packit |
667938 |
die "Error: Unknown parameter $ARGV[0] \n";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
chop $source_port;
|
|
Packit |
667938 |
chop $dest_port;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if($source_port.$dest_port ne '') { $prot='tcp';}
|
|
Packit |
667938 |
if($mode eq 'both')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$exec_line="$ipfwadm -A out -a -P $prot -S $source_ip $source_port -D $dest_ip $dest_port; $ipfwadm -A in -a -P $prot -S $dest_ip $dest_port -D $source_ip $source_port";
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
else {
|
|
Packit |
667938 |
$exec_line="$ipfwadm -A $mode -a -P $prot -S $source_ip $source_port -D $dest_ip $dest_port";
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
$new_sp=($source_port eq '')?'*':join(',',split(/ /,$source_port));
|
|
Packit |
667938 |
$new_dp=($dest_port eq '')?'*':join(',',split(/ /,$dest_port));
|
|
Packit |
667938 |
$ports=($source_port.$dest_port eq '')?"\"n/a\"":"\"".$new_sp." -> ".$new_dp."\"";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if($mode eq 'both')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$stats_line=($source_port.$dest_port eq '')?"$ipfwadm -A -l -x -n| grep $prot |grep $source_ip |grep $dest_ip | grep \"n/a\"":"$ipfwadm -A -l -x -n| grep $prot |grep $source_ip |grep $dest_ip | grep \"$new_sp\" | grep \"$new_dp\"";
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
else
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$stats_line="$ipfwadm -A -l -x -n |grep \"$mode $prot $source_ip\" | grep $dest_ip | grep $ports ";
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
open(DATA,$stats_line."|");
|
|
Packit |
667938 |
@info=<DATA>;
|
|
Packit |
667938 |
close <DATA>;
|
|
Packit |
667938 |
$probe=($mode eq 'both')?2:1;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$mode=($mode eq 'in ')?'in':$mode;
|
|
Packit |
667938 |
if(@info<$probe)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
system($exec_line);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
else
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
if($probe%2)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
eval "\$$mode"."Bytes=(split(/ +/,\$info[0]))[2]";
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
else
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
if($info[0] =~ /in /) { $inBytes=(split(/ +/,$info[0]))[2]; }
|
|
Packit |
667938 |
elsif($info[0] =~ /out /) { $outBytes=(split(/ +/,$info[0]))[2];}
|
|
Packit |
667938 |
if($info[1] =~ /in /) { $inBytes=(split(/ +/,$info[1]))[2]; }
|
|
Packit |
667938 |
elsif($info[1] =~ /out /) { $outBytes=(split(/ +/,$info[1]))[2];}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
open(UPTIME,"uptime |cut -b 13-26|");
|
|
Packit |
667938 |
$upTime=<UPTIME>;
|
|
Packit |
667938 |
close(UPTIME);
|
|
Packit |
667938 |
printf "$inBytes\n$outBytes\n$upTime\n$host";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|