前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nagios监控流量脚本

Nagios监控流量脚本

作者头像
星哥玩云
发布2022-07-01 14:54:12
1.4K0
发布2022-07-01 14:54:12
举报
文章被收录于专栏:开源部署开源部署

需求是我们需要对服务器上的流量进行监控,网络上有个流传的check_traffic.sh,它需要被监控机开启snmp。但是感觉都使用上了nagios还要开snmp。有点斧子剪刀一起用的感觉,所以就动手写了个监控流量的shell。

脚本如下:

代码语言:javascript
复制
#!/bin/sh

usage() { echo "Usage: $0 [-n <eth0>] [-w <tx rx>] [-c <tx rx>]" 1>&2; exit 1; }

foundw=0;
foundc=0;
foundn=0;

for item in $@ ; do
    if [[ $foundn == 1 ]]; then
        n=$item;
        foundn=2;
        continue;
    fi
    if [[ $foundw == 1 ]]; then
        w1=$item;
        foundw=2;
        continue;
    fi
    if [[ $foundw == 2 ]]; then
        w2=$item;
        foundw=3;
        continue;
    fi
    if [[ $foundc == 1 ]]; then
        c1=$item;
        foundc=2;
        continue;
    fi
    if [[ $foundc == 2 ]]; then
        c2=$item;
        foundc=2;
        continue;
    fi
    if [[ "$item" == "-w" ]]; then
        foundw=1;
        continue;
    fi
    if [[ "$item" == "-c" ]]; then
        foundc=1;
        continue;
    fi
    if [[ "$item" == "-n" ]]; then
        foundn=1;
        continue;
    fi
done

if [ -z "${w1}" ] || [ -z "${w2}" ] || [ -z "${c1}" ] || [ -z "${c2}" ] || [ -z "${n}" ]; then
    usage
fi

R1=`cat /sys/class/net/$n/statistics/rx_bytes`
T1=`cat /sys/class/net/$n/statistics/tx_bytes`
sleep 1
R2=`cat /sys/class/net/$n/statistics/rx_bytes`
T2=`cat /sys/class/net/$n/statistics/tx_bytes`
TBPS=`expr $T2 - $T1`
RBPS=`expr $R2 - $R1`
TMBPS=`expr $TBPS / 1024 / 128`
RMBPS=`expr $RBPS / 1024 / 128`

if [[ $TMBPS -ge $c1 ]] || [[ $RMBPS -ge $c2 ]] ; then
    echo "Critical - current is ${TMBPS}, ${RMBPS}";
    exit 2;
fi
if [[ $TMBPS -ge $w1 ]] || [[ $RMBPS -ge $w2 ]] ; then
    echo "WARNING - current is ${TMBPS}, ${RMBPS}";
    exit 1;
fi
echo "OK - current is ${TMBPS}, ${RMBPS}";
exit 0;

其中的w和c的数值单位都是Mb。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档