共计 1683 个字符,预计需要花费 5 分钟才能阅读完成。
需求是我们需要对服务器上的流量进行监控,网络上有个流传的 check_traffic.sh,它需要被监控机开启 snmp。但是感觉都使用上了 nagios 还要开 snmp。有点斧子剪刀一起用的感觉,所以就动手写了个监控流量的 shell。
相关阅读 :
网络监控器 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
脚本如下:
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。
Nagios 的详细介绍 :请点这里
Nagios 的下载地址 :请点这里
正文完
星哥玩云-微信公众号
