阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nagios 流量监控和报警的shell脚本

139次阅读
没有评论

共计 2495 个字符,预计需要花费 7 分钟才能阅读完成。

上下文:我们平台的监控系统用的是 cacti+nagios。之前没有加流量异常报警设置,cacti 上到是有插件可以时间报警,但是无法使用我们自己的短信接口(nagios 用的购买的短信接口),所以想自己写一个脚本配合 nagios 实现基本的流量异常报警。

脚本思路 :/proc/net/dev 取出当前流经网卡的(接收和发送)的 kb 总数量,在检测间隔时间后再次读取这两个值,
相减既是间隔时间段内的增量,再根据此增量做计算,算出间隔时间内的平均流量,和基准流量作比较,触发 nagios 报警事件

局限性:我们只检测外网卡的流量,且默认 em1 网卡为外网卡(需根据各位实际情况自行调整)

脚本内容

#!/bin/bash
#by ljk 
   # 默认第一块网卡为外网卡,检查当前系统是以 eth0 还是 em1 作为网卡一,后面会引用网卡名
   interface=`ip a|grep -e "eth0" -e "em1"|awk '{print $NF}'|tail -1`

   # 定义存储结果的函数,以便在任何异常 / 正常退出前都能保存最新的记录
   function tmp_store {chmod 777 /tmp/receive /tmp/transfer &> /dev/null 
       # 以防以 root 用户验证脚本时以 root 创建这两个文件,等 nagios 以 nagios 用户调用脚本的时候无法写入新的记录值,导致检测结果不准
       cat /proc/net/dev|grep "$interface"|sed 's/^ *//g'|awk -F'[:]+' '{print $2}' > /tmp/receive
       cat /proc/net/dev|grep "$interface"|sed 's/^ *//g'|awk -F'[:]+' '{print $10}' > /tmp/transfer
   }
   # 将当时流量统计记录于 tmp, 然后由 nagios 调用脚本, 定时 (5 分钟, 遇异常隔 5 分钟再检测) 采集新的统计值,
   做计算统计出时间段内流量的平均值
   RX_bytes_last=`cat /tmp/receive`
   TX_bytes_last=`cat /tmp/transfer`

   RX_bytes=`cat /proc/net/dev|grep "$interface"|sed 's/^ *//g'|awk -F'[:]+' '{print $2}'`
   TX_bytes=`cat /proc/net/dev|grep "$interface"|sed 's/^ *//g'|awk -F'[:]+' '{print $10}'`

   speed_RX=`echo "scale=0;($RX_bytes - $RX_bytes_last)*8/1024/1024/300"|bc`  
   #300 即 5 分钟,nagios 每五分钟检测一次流量,统计出来的 单位为 Mb
   speed_TX=`echo "scale=0;($TX_bytes - $TX_bytes_last)*8/1024/1024/300"|bc` 

   if   [$speed_RX -gt 5  -a $speed_TX -gt 5 ];then # 此处的 5,是我们的基准值,各位自行调整
        echo "speed_RX=$speed_RX, speed_TX=$speed_TX. both great than normal"
        tmp_store;
        exit 2
   elif [$speed_RX -gt 5 -o $speed_TX -gt 5 ];then
        if [$speed_RX -gt 5 ];then
           echo "receive is $speed_RX Mbps, great than 5Mbps"
           tmp_store;
           exit 2
        else
           echo "transfer is $speed_TX Mbps, great than 5Mbps"
           tmp_store;
           exit 2
        fi
   else 
        echo "OK speed_RX=$speed_RX Mbps, speed_TX=$speed_TX Mbps"
        tmp_store;
        exit 0
   fi

————————————– 分割线 ————————————–

在 Ubuntu 下配置 Mrtg 监控 Nginx 和服务器系统资源 http://www.linuxidc.com/Linux/2013-08/88417.htm

使用 snmp+Mrtg 监控 Linux 系统 http://www.linuxidc.com/Linux/2012-11/73561.htm

Mrtg 服务器搭建(监控网络流量)http://www.linuxidc.com/Linux/2012-07/64315.htm

网络监控器 Nagios 全攻略 http://www.linuxidc.com/Linux/2013-07/87067.htm

Nagios 搭建与配置详解 http://www.linuxidc.com/Linux/2013-05/84848.htm

Nginx 环境下构建 Nagios 监控平台 http://www.linuxidc.com/Linux/2011-07/38112.htm

在 RHEL5.3 上配置基本的 Nagios 系统(使用 Nagios-3.1.2) http://www.linuxidc.com/Linux/2011-07/38129.htm

CentOS 5.5+Nginx+Nagios 监控端和被控端安装配置指南 http://www.linuxidc.com/Linux/2011-09/44018.htm

Ubuntu 13.10 Server 安装 Nagios Core 网络监控运用 http://www.linuxidc.com/Linux/2013-11/93047.htm

————————————– 分割线 ————————————–

Nagios 的详细介绍:请点这里
Nagios 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-10/136221.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2495字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中