失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Shell脚本之网络流量监控

Shell脚本之网络流量监控

时间:2024-07-23 08:41:49

相关推荐

Shell脚本之网络流量监控

一、需求说明

互联网出口一般情况下流量不大,而且比较平稳,偶尔会出现业务高峰期,此时互联网出口流量激增。希望监控互联网出口设备的接口流量,在流量突增的时候发出邮件告警。每天定时邮件报告一次一天的平均流量。网络设备流量监控zabbix等工具都可以实现,此博文仅从shell脚本学习出发,实现如上需求。

二、脚本内容

此流量监控工具包含device_login.sh、netflow_check.sh两个部分,其中device_login.sh用于远程登录设备和采集信息。netflow_check.sh是主脚本,通过调用device_login.sh收集信息,然后通过过滤、存储、计算得到5分钟的平均流量值。当流量大于阈值的时候发出邮件告警。每天上午10钟的时候邮件通知24小时的流量平均值,流量的单位为KB/S。

1、远程登录设备脚本

[root@s146 flow]# cat device_login.sh#!/usr/bin/expect -f#auto ssh loginset timeout 300set sshhost [lindex $argv 0]set passwd [lindex $argv 1]spawn ssh -l admin $sshhostexpect "password:"send "$passwd\r"expect ">"send "display int g1/0/1 | in total\r"expect "<>"send "quit\r"expect eof

2、流量监控主脚本

[root@s146 flow]# cat netflow_check.sh #!/bin/bash#script name: netflow_check.sh#author: wuhsh#version: v1#description: 此脚本用于检查互联网出口流量,每5分钟检查一次#description: 防火墙5分钟平均流量(入)超过50Mbit/s时邮件告警,平均流量(出)超过15Mbit/s时邮件告警#参数定义fwhost=x.x.x.xpasswd=*******netflow_this=netflow_this.txtnetflow_last=netflow_last.txti=0curdir=`cd -P $(dirname $0); pwd`cd $curdirfunction singlecheck(){./device_login.sh $fwhost $passwd > $netflow_thisin_this=`sed -n 11p $netflow_this | awk '{print $5}'`out_this=`sed -n 12p $netflow_this | awk '{print $5}'`in_last=`sed -n 11p $netflow_last | awk '{print $5}'`out_last=`sed -n 12p $netflow_last | awk '{print $5}'`average_in=`expr $in_this - $in_last`average_in=`expr $average_in / 300`average_in=`expr $average_in / 1024`average_out=`expr $out_this - $out_last`average_out=`expr $average_out / 300`average_out=`expr $average_out / 1024`echo $in_thisecho $out_thisif [[ $average_out -ge 1920 ]] ;thenecho "出方向流量突增,5分钟平均出流量 $average_out KB/S" | mailx -s "IDC出口流量告警" 524627027@fiif [[ $average_in -ge 6400 ]] ;thenecho "入方向流量突增,5分钟平均入流量 $average_in KB/S" | mailx -s "IDC出口流量告警" 524627027@firm -rf netflow_last.txtmv netflow_this.txt netflow_last.txt}function main(){while truedo sleep 300tmp=$(singlecheck)array_input[$i]=`echo $tmp |awk '{print $1}'`array_output[$i]=`echo $tmp |awk '{print $2}'`time=`date +%H%M`if [[ $time -ge 1000 && $time -le 1005 && $i -gt 1 ]];thenaverage_oneday_in=`expr ${array_input[$i]} - ${array_input[0]}`average_oneday_out=`expr ${array_output[$i]} - ${array_output[0]}`let i+=1average_oneday_in=`expr $average_oneday_in / $i`average_oneday_in=`expr $average_oneday_in / 300`average_oneday_in=`expr $average_oneday_in / 1024`average_oneday_out=`expr $average_oneday_out / $i`average_oneday_out=`expr $average_oneday_out / 300`average_oneday_out=`expr $average_oneday_out / 1024`cat sum.txt >> netflowhistory.txtecho "现在是`date +%F-%T`,最近24小时平均流量如下:" > sum.txtecho "入方向最近24小时平均流量:$average_oneday_in KB/s" >> sum.txt echo "出方向最近24小时平均流量:$average_oneday_out KB/s" >> sum.txt echo "总计检查: $i 次" >> sum.txtmailx -s "IDC出口流量通知" 524627027@ < sum.txti=0unset array_outputunset array_inputcontinuefilet i+=1done}./device_login.sh $fwhost $passwd > $netflow_lastmain

三、使用示例

1、执行脚本

脚本使用的是无限循环,让脚本在后台执行即可。

[root@s146 flow]# ./netflow_check.sh &

[1] 15454

2、当端口流量大于阈值时报警

告警阈值设置的是入流量大于50Mb/s的时候告警,注意是比特,因为网络专线带宽都是按照比特计算的,而我们采集的是字节单位,所以实际设置的阈值是打印6400KB/s时告警。

3、每天定时报告24小时平均流量

执行1天之后我们可以看到24小时的平均流量,受执行时间的影响,完整1天的检查次数是286或287次。另外每天通知一下24小时流量结果还有一个好处就是知道监控程序是否正常执行,如果哪天没有收到邮件通知,可能原因程序有错或者服务器离线、重启了等。需要我们人工检测。

如果觉得《Shell脚本之网络流量监控》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。