1 | 1 | simandl | package HotSaNICmod::OSdep; |
2 | | | |
3 | | | use RRDs; |
4 | | | use lib "../../lib"; |
5 | | | |
6 | | | sub version { |
7 | | | ($VERSION = '$Revision: 1.4 $') =~ s/.*(\d+\.\d+).*/$1/; |
8 | | | return "$^O.pm $VERSION"; |
9 | | | } |
10 | | | |
11 | | | sub sample { |
12 | | | my %args=@_; |
13 | | | |
14 | | | my $IPTABLES=$args{IPTABLES}; |
15 | | | |
16 | | | if ( (! -e "acct_int.dat") || (! -e "acct_ext.dat")) { |
17 | 6 | simandl | system("$IPTABLES -L acct_int_other -xvn > acct_int.dat"); |
18 | | | system("$IPTABLES -L acct_ext_other -xvn > acct_ext.dat"); |
19 | | | foreach $prt ("tcp","udp") { |
20 | | | system("$IPTABLES -L acct_int_$prt -xvn | sed 's/all/$prt/g' >> acct_int.dat"); |
21 | | | system("$IPTABLES -L acct_ext_$prt -xvn | sed 's/all/$prt/g' >> acct_ext.dat"); |
22 | 5 | simandl | system("$IPTABLES -L acct_int_$prt -xvn >> acct_int.dat"); |
23 | | | system("$IPTABLES -L acct_ext_$prt -xvn >> acct_ext.dat"); |
24 | | | } |
25 | 1 | simandl | } |
26 | | | |
27 | | | my %acct_int_old=readfile("acct_int.dat",$IPTABLES); |
28 | | | my %acct_ext_old=readfile("acct_ext.dat",$IPTABLES); |
29 | 6 | simandl | system("$IPTABLES -L acct_int_other -xvn > acct_int.dat"); |
30 | | | system("$IPTABLES -L acct_ext_other -xvn > acct_ext.dat"); |
31 | | | foreach $prt ("tcp","udp") { |
32 | 5 | simandl | system("$IPTABLES -L acct_int_$prt -xvn | sed 's/all/$prt/g' >> acct_int.dat"); |
33 | | | system("$IPTABLES -L acct_ext_$prt -xvn | sed 's/all/$prt/g' >> acct_ext.dat"); |
34 | | | } |
35 | 1 | simandl | my %acct_int=readfile("acct_int.dat",$IPTABLES); |
36 | | | my %acct_ext=readfile("acct_ext.dat",$IPTABLES); |
37 | | | |
38 | | | my $time=time; |
39 | | | |
40 | | | foreach my $nn (sort(keys(%acct_int))) { |
41 | 6 | simandl | $otherin=$acct_int{$nn}[1]-$acct_int_old{$nn}[1]; |
42 | 1 | simandl | $tcpin=$acct_int{$nn}[3]-$acct_int_old{$nn}[3]; |
43 | | | $udpin=$acct_int{$nn}[5]-$acct_int_old{$nn}[5]; |
44 | 6 | simandl | $otherout=$acct_int{$nn}[2]-$acct_int_old{$nn}[2]; |
45 | 1 | simandl | $tcpout=$acct_int{$nn}[4]-$acct_int_old{$nn}[4]; |
46 | | | $udpout=$acct_int{$nn}[6]-$acct_int_old{$nn}[6]; |
47 | 6 | simandl | updatedb($args{MODNAME},$time,"int$nn",$tcpin,$udpin,$otherin,$tcpout,$udpout,$otherout); |
48 | 1 | simandl | } |
49 | | | |
50 | | | foreach my $nn (sort(keys(%acct_ext))) { |
51 | 6 | simandl | $otherin=$acct_ext{$nn}[1]-$acct_ext_old{$nn}[1]; |
52 | 1 | simandl | $tcpin=$acct_ext{$nn}[3]-$acct_ext_old{$nn}[3]; |
53 | | | $udpin=$acct_ext{$nn}[5]-$acct_ext_old{$nn}[5]; |
54 | 6 | simandl | $otherout=$acct_ext{$nn}[2]-$acct_ext_old{$nn}[2]; |
55 | 1 | simandl | $tcpout=$acct_ext{$nn}[4]-$acct_ext_old{$nn}[4]; |
56 | | | $udpout=$acct_ext{$nn}[6]-$acct_ext_old{$nn}[6]; |
57 | 6 | simandl | updatedb($args{MODNAME},$time,"ext$nn",$tcpin,$udpin,$otherin,$tcpout,$udpout,$otherout); |
58 | 1 | simandl | } |
59 | | | } |
60 | | | |
61 | | | sub readfile { |
62 | | | my ($file,$IPTABLES)=@_; |
63 | | | my $ip=""; |
64 | | | undef my %hash; |
65 | | | open (FILE,$file); |
66 | | | while (<FILE>) { |
67 | | | chomp; |
68 | | | if (index($IPTABLES,"ipchains") >= 0 ) { ($pkt,$bytes,$target,$proto,$opt,$tosa,$tosx,$ifname,$src,$dst)=split; } |
69 | | | else { ($pkt,$bytes,$target,$proto,$opt,$in,$out,$src,$dst)=split; } |
70 | | | if ($pkt =~ /^[0-9]*$/ ) { |
71 | | | if ($dst eq "") { ($proto,$opt,$in,$out,$src,$dst)=($target,$proto,$opt,$in,$out,$src); } |
72 | | | if ($src eq "0.0.0.0/0") { $ip=$dst;$dir=1; } elsif ($dst eq "0.0.0.0/0") { $ip=$src;$dir=0; } |
73 | | | if ($proto eq "all") { $prt=1 }; |
74 | | | if ($proto eq "tcp") { $prt=3 }; |
75 | | | if ($proto eq "udp") { $prt=5 }; |
76 | | | if ($ip ne "") {$hash{"$ip"}[$prt+$dir]=$bytes;} |
77 | | | } |
78 | | | } |
79 | | | close (FILE); |
80 | | | return %hash; |
81 | | | } |
82 | | | |
83 | | | sub updatedb { |
84 | 7 | simandl | my ($MODNAME,$time,$name,$tcpin,$udpin,$otherin,$tcpout,$udpout,$otherout)=@_; |
85 | 1 | simandl | $name =~ s/\//_/g; |
86 | | | if ( !-e "rrd/$name.rrd") { system "./makerrd $name U"; } |
87 | 7 | simandl | RRDs::update "rrd/$name.rrd",$time.":".$tcpin.":".$udpin.":".$otherin.":".$tcpout.":".$udpout.":".$otherout; |
88 | 1 | simandl | if ($ERROR = RRDs::error) { print time," ",$MODNAME,": unable to update `$name.rrd': $ERROR\n"; } |
89 | | | } |
90 | | | |
91 | | | |
92 | | | 1; |
93 | | | |