Mar 18

[原]流量监控脚本 v1.2 晴

linuxing , 10:59 , 编程 » 示例 , 评论(0) , 引用(0) , 阅读(19361) , Via 本站原创 | |
    从功能的角度来说,ipflow.pl和banip.sh已经基本满足我的要求。但使用中,还有个问题:每次查看想查看网络流量时,都必须登陆到路由网关服务器上运行ipflow.pl脚本,太麻烦了。所以,接下来我要做的,就是把输出结果进行格式化,显示为HTML样式。这样,我只要通过浏览器就能知道大概的网络流量。(为什么是大概呢?因为结果并不是实时的,而是对上一次crontab运行的时间段的结果)

一、想法
这个不涉及banip.sh脚本,只要给ipflow.pl脚本定义一个参数就可以了。
例如:

# ./ipflow.pl -p

二、脚本
定义一个$HTML的变量来控制输出:

#!/usr/bin/perl -w
# Date:2009-03-14
# Author:HyphenWang
# Version:1.2
use strict;
my $IPTABLES_CMD="/sbin/iptables -v -n -x -L FORWARD";
my $BANIP_CMD="/root/banip.sh";
my $SEC="3";
my $ZERO="1";
my $BANIP="1";
my $BANSEC="60";
# 判断是否有“-p”参数,有的话就输出HTML样式
my $HTML="1" if (defined $ARGV[0] and $ARGV[0] eq "-p");
my $limit_input_rate=200;
my $limit_output_rate=35;
my @exclude_ip=qw(30 153 155 200 221);
my (%first_input,%first_output);
my (%second_input,%second_output);
my (%ban_ip,$input_rate,$output_rate);

sub get_ipflow {
  my ($ip_input,$ip_output)=@_;
  for my $line (`$IPTABLES_CMD`) {
    my @columns = split(/\s+/,$line);
    $ip_input->{$columns[-1]}=$columns[2] if ($columns[3] eq "ACCEPT" and $columns[-1] =~ m/192\.168\.228\.\d+/);
    $ip_output->{$columns[-2]}=$columns[2] if ($columns[3] eq "ACCEPT" and $columns[-2] =~ m/192\.168\.228\.\d+/);
    $ban_ip{$columns[-1]}=1 if ($columns[3] eq "DROP" and $columns[-1] =~ m/192\.168\.228\.\d+/);
    $ban_ip{$columns[-2]}=1 if ($columns[3] eq "DROP" and $columns[-2] =~ m/192\.168\.228\.\d+/);
  }
}
get_ipflow(\%first_input,\%first_output);
sleep $SEC;
get_ipflow(\%second_input,\%second_output);

if (! defined $HTML) {
  print "Now is ".localtime()."\n";
  print "-"x53,"\n";
  print "IP Address\t\tIn Flow Rate\tOut Flow Rate\n";
}
else {
# 使用here documents方式输出HTML
print<<EOF;
<table cellspacing="0">
<tr><td><table border="1" align="left" cellspacing="0">
<tr align="center" bgcolor="#FFFF00">
<td width="200">IP Address</td>
<td width="200">In Flow Rate</td>
<td width="200">Out flow Rate</td>
</tr>
EOF
}

for my $ip (keys %first_input) {
  if ($ZERO != 1) {
    if (defined $second_input{$ip} and defined $second_output{$ip} and int(($second_input{$ip}-$first_input{$ip})/1024/$SEC) == 0) {
      next;
    }
  }
  if (defined $second_input{$ip} and defined $second_output{$ip}) {
    $input_rate = ($second_input{$ip}-$first_input{$ip})/1024/$SEC;
    $output_rate = ($second_output{$ip}-$first_output{$ip})/1024/$SEC;
    if (! defined $HTML) {
      printf ("%s\t\t%.f KByte/s\t%.f KByte/s\n",$ip,$input_rate,$output_rate);
    }
    else {
      printf ("<tr><td>%s</td>\n<td>%.f KByte/s</td>\n<td>%.f KByte/s</td>\n</tr>\n",$ip,$input_rate,$output_rate);
    }
    if ($BANIP == 1 and $input_rate >= $limit_input_rate and ! grep ("192.168.228.$_" eq $ip,@exclude_ip)) {
      system ("$BANIP_CMD INPUT $ip $BANSEC &");
      $ban_ip{$ip}=1;
    }
    if ($BANIP == 1 and $output_rate >= $limit_output_rate and ! grep ("192.168.228.$_" eq $ip,@exclude_ip)) {
      system ("$BANIP_CMD OUTPUT $ip $BANSEC &");
      $ban_ip{$ip}=1;
    }
  }
}
if (! defined $HTML) {
  print "-"x53,"\n";
  print "Banned IP Address:\n";
}
else {
print<<EOF;
</table></td></tr>
<tr><td><p><br></p></td></tr>
<tr><td><table border="1" align="left" cellspacing="0">
<tr>
<td  width="200" align="center" bgcolor="#FF0000">Banned IP Address</td>
</tr>
EOF
}
for (keys %ban_ip) {
  if (! defined $HTML) {
  print "$_\n";
  }
  else {
  print<<EOF;
<tr>
<td>$_</td>
</tr>
EOF
  }
}
if (defined $HTML) {
  print<<EOF;
</table></td></tr></td></table>
<br>
限速:200 KByte/s,若检查时发现超出该值,会自动中断对应的客户端IP一分钟。
若您被中断了,请等待,或告诉我。
EOF
}

本地下载:
◎ 结果
($ZERO为0)
普通格式:
引用
# ./ipflow.pl
Now is Mon Mar 16 16:11:50 2009
-----------------------------------------------------
IP Address              In Flow Rate    Out Flow Rate
192.168.228.200         383 KByte/s     21 KByte/s
-----------------------------------------------------
Banned IP Address:
192.168.228.50

HTML格式:
引用
# ./ipflow.pl -p
<table cellspacing="0">
<tr><td><table border="1" align="left" cellspacing="0">
<tr align="center" bgcolor="#FFFF00">
<td width="200">IP Address</td>
<td width="200">In Flow Rate</td>
<td width="200">Out flow Rate</td>
</tr>
<tr><td>192.168.228.200</td>
<td>394 KByte/s</td>
<td>16 KByte/s</td>
</tr>
</table></td></tr>
<tr><td><p><br></p></td></tr>
<tr><td><table border="1" align="left" cellspacing="0">
<tr>
<td  width="200" align="center" bgcolor="#FF0000">Banned IP Address</td>
</tr>
<tr>
<td>192.168.228.50</td>
</tr>
</table></td></tr></td></table>
<br>
限速:200 KByte/s,若检查时发现超出该值,会自动中断对应的客户端IP一分钟。
若您被中断了,请等待,或告诉我。

三、查看结果
把ipflow.pl输出的HTML文本保存到httpd服务器的根目录主页上,访问该页面即可查看结果。
引用
# crontab -l
*/1 * * * * /root/ipflow.pl -p > /var/www/html/ipflow.html

页面显示:
点击在新窗口中浏览此图片
Tags:
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]