Debian Codegraph @

contrib/mrtg-ipacc/ipacc

raw
1 #!/usr/bin/perl
2 #
3 # This is just a hack, but it works fine for me.
4 # I'm using the ip accounting kernel rules to feed the mrtg.
5 # You need to have IP accounting compiled in your kernel(2.0), or IPChains
6 # support if you are running 2.1 or 2.2
7 # Anyway I'm not using the ipchains in here as there's no need to.
8 # The old ipfwadm will do just fine.
9 # So for 2.1 or 2.2 you'll need the ipfwadm-wrapper.
10 #
11 #
12 # Cristian Caramida <[email protected]>
13 # but very soon <[email protected]>
14 #
15 # Usage:
16 #
17 # You need to have IP accounting compiled in your kernel, and than just add
18 # to the mrtg.cfg
19 # Target[ip_addr]: `ipacc [mode] [-S aaa.bbb.ccc.ddd[/net_bits]] [port1 port2 ...] [-D aaa.bbb.ccc.ddd[/net_bits]] [port1 port2 ...]`
20 # where
21 # - mode - is one of "in" or "out" in lowercase
22 # - aaa.bbb.ccc.ddd - is a dotted IP address (not a host/net name)
23 # - net_bits - is the number of bits used by the network portion of the IP
24 # (eg. 24 for a whole C class, or 25 for half of a C class)
25 # - port1 port2 ... - the ports you want to plot trafic on
26 # I haven't tryied to call it without any parameters!
27 # It will probably make accounting for all trafic.
28
29 $ipfwadm='/sbin/ipfwadm'; # <-------- ATTENTION
30 $host="com.bbs.ro";
31 #If you are running with ipchains (kernel 2.1, 2.2 or later) you may
32 #either symlink the ipfwadm-wrapper to that location or simply modify the value
33
34
35
36 #---------------------------------------------------------------------------
37 # Modify this only if you know what you are doing !!! :-)
38 $mode='both';
39 $remote='';
40 $port='8889';
41 if(!($ARGV[0] =~ /^\-/)){ ($mode)=@ARGV; shift @ARGV;}
42 if($mode eq 'in') {$mode='in ';}
43 $mode=($mode eq 'in')?'in ':$mode;
44 $source_ip='0.0.0.0/0';
45 $source_port='';
46 $dest_ip='0.0.0.0/0';
47 $dest_port='';
48 $prot='all';
49 $inBytes=0;
50 $outBytes=0;
51 $upTime=0;
52
53 while ($ARGV[0] =~ /^\-/)
54 {
55
56 if($ARGV[0] eq '-S')
57 {
58 shift @ARGV;
59 ($source_ip)=@ARGV;
60 shift @ARGV;
61 while ($ARGV[0] =~ /[0123456789]+/)
62 {
63 $source_port.="$ARGV[0] ";
64 shift @ARGV;
65 }
66 }
67 elsif($ARGV[0] eq '-D')
68 {
69 shift @ARGV;
70 ($dest_ip)=@ARGV;
71 shift @ARGV;
72 while ($ARGV[0] =~ /[0123456789]+/)
73 {
74
75 $dest_port.="$ARGV[0] ";
76 shift @ARGV;
77 }
78 }
79 else {
80 die "Error: Unknown parameter $ARGV[0] \n";
81
82 }
83 }
84
85 chop $source_port;
86 chop $dest_port;
87
88 if($source_port.$dest_port ne '') { $prot='tcp';}
89 if($mode eq 'both')
90 {
91 $exec_line="$ipfwadm -A in -a -P $prot -S $source_ip $source_port -D $dest_ip $dest_port; $ipfwadm -A out -a -P $prot -S $dest_ip $dest_port -D $source_ip $source_port";
92 }
93 else {
94 $exec_line="$ipfwadm -A $mode -a -P $prot -S $source_ip $source_port -D $dest_ip $dest_port";
95 }
96 $new_sp=($source_port eq '')?'*':join(',',split(/ /,$source_port));
97 $new_dp=($dest_port eq '')?'*':join(',',split(/ /,$dest_port));
98 $ports=($source_port.$dest_port eq '')?"\"n/a\"":"\"".$new_sp." -> ".$new_dp."\"";
99
100 if($mode eq 'both')
101 {
102 $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\"";
103 }
104 else
105 {
106 $stats_line="$ipfwadm -A -l -x -n |grep \"$mode $prot $source_ip\" | grep $dest_ip | grep $ports ";
107 }
108
109
110 open(DATA,$stats_line."|");
111 @info=<DATA>;
112 close <DATA>;
113 $probe=($mode eq 'both')?2:1;
114
115 $mode=($mode eq 'in ')?'in':$mode;
116 if(@info<$probe)
117 {
118 system($exec_line);
119 }
120 else
121 {
122 if($probe%2)
123 {
124 eval "\$$mode"."Bytes=(split(/ +/,\$info[0]))[2]";
125 }
126 else
127 {
128 if($info[0] =~ /in /) { $inBytes=(split(/ +/,$info[0]))[2]; }
129 elsif($info[0] =~ /out /) { $outBytes=(split(/ +/,$info[0]))[2];}
130 if($info[1] =~ /in /) { $inBytes=(split(/ +/,$info[1]))[2]; }
131 elsif($info[1] =~ /out /) { $outBytes=(split(/ +/,$info[1]))[2];}
132 }
133 }
134 open(UPTIME,"uptime |cut -b 13-26|");
135 $upTime=<UPTIME>;
136 close(UPTIME);
137 printf "$inBytes\n$outBytes\n$upTime\n$host";
138
139
140
141