nagios 流量监控和报警的shell脚本
上下文:我们平台的监控系统用的是cacti+nagios。之前没有加流量异常报警设置,cacti上到是有插件可以时间报警,但是无法使用我们自己的短信接口(nagios用的购买的短信接口),所以想自己写一个脚本配合nagios实现基本的流量异常报警。
脚本思路:/proc/net/dev取出当前流经网卡的(接收和发送)的kb总数量,在检测间隔时间后再次读取这两个值,
相减既是间隔时间段内的增量,再根据此增量做计算,算出间隔时间内的平均流量,和基准流量作比较,触发nagios报警事件
局限性:我们只检测外网卡的流量,且默认em1网卡为外网卡(需根据各位实际情况自行调整)
#!/bin/bash#byljk#默认第一块网卡为外网卡,检查当前系统是以eth0还是em1作为网卡一,后面会引用网卡名interface=`ipa|grep-e"eth0"-e"em1"|awk'{print$NF}'|tail-1`#定义存储结果的函数,以便在任何异常/正常退出前都能保存最新的记录functiontmp_store{chmod777/tmp/receive/tmp/transfer&>/dev/null#以防以root用户验证脚本时以root创建这两个文件,等nagios以nagios用户调用脚本的时候无法写入新的记录值,导致检测结果不准cat/proc/net/dev|grep"$interface"|sed's/^*//g'|awk-F'[:]+''{print$2}'>/tmp/receivecat/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每五分钟检测一次流量,统计出来的单位为Mbspeed_TX=`echo"scale=0;($TX_bytes-$TX_bytes_last)*8/1024/1024/300"|bc`if[$speed_RX-gt5-a$speed_TX-gt5];then#此处的5,是我们的基准值,各位自行调整echo"speed_RX=$speed_RX,speed_TX=$speed_TX.bothgreatthannormal"tmp_store;exit2elif[$speed_RX-gt5-o$speed_TX-gt5];thenif[$speed_RX-gt5];thenecho"receiveis$speed_RXMbps,greatthan5Mbps"tmp_store;exit2elseecho"transferis$speed_TXMbps,greatthan5Mbps"tmp_store;exit2fielseecho"OKspeed_RX=$speed_RXMbps,speed_TX=$speed_TXMbps"tmp_store;exit0fi
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。