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 login
set timeout 300
set sshhost [lindex $argv 0]
set passwd [lindex $argv 1]
spawn ssh -l admin $sshhost
expect "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.x
passwd=*******
netflow_this=netflow_this.txt
netflow_last=netflow_last.txt
i=0

curdir=`cd -P $(dirname $0); pwd`
cd $curdir

function singlecheck(){
  ./device_login.sh $fwhost $passwd > $netflow_this
  in_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_this
  echo $out_this
  if [[ $average_out -ge 1920 ]] ;then
    echo "出方向流量突增,5分钟平均出流量 $average_out KB/S" | mailx -s "IDC出口流量告警" [email protected]
  fi

  if [[ $average_in -ge 6400 ]] ;then
    echo "入方向流量突增,5分钟平均入流量 $average_in KB/S" | mailx -s "IDC出口流量告警" [email protected]
  fi
  rm -rf netflow_last.txt
  mv netflow_this.txt netflow_last.txt
}

function main(){
  while true
  do 
    sleep 300
    tmp=$(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 ]];then
      average_oneday_in=`expr ${array_input[$i]} - ${array_input[0]}`
      average_oneday_out=`expr ${array_output[$i]} - ${array_output[0]}`
      let i+=1
      average_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.txt
      echo "现在是`date +%F-%T`,最近24小时平均流量如下:" > sum.txt
      echo "入方向最近24小时平均流量:$average_oneday_in KB/s" >> sum.txt 
      echo "出方向最近24小时平均流量:$average_oneday_out KB/s" >> sum.txt 
      echo "总计检查: $i 次" >> sum.txt

      mailx -s "IDC出口流量通知" [email protected] < sum.txt
      i=0
      unset array_output
      unset array_input
      continue
    fi

    let i+=1
  done
}

./device_login.sh $fwhost $passwd > $netflow_last

main

三、使用示例

1、执行脚本

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

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

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

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

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

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

猜你喜欢

转载自blog.csdn.net/carefree2005/article/details/124824308
今日推荐