温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nagios 监控 网卡流量 脚本

发布时间:2020-08-02 07:39:26 来源:网络 阅读:1657 作者:ftlynx 栏目:移动开发
#!/bin/bash
#
#Time     : 2014-06-23
#Author   : ftlynx
#Function : use NET-SNMP get NIC traffic on nagios.

Usage(){
	echo "Usage: check_traffic.sh [options]"
	echo "     -H     Host IP."
	echo "     -P     net-snmp community string."
	echo "     -N     NIC desc."
	echo "     -W     nagios warning value. Format: 200,300.  200 is in traffic. 300 is out traffic. unit:Kb. Default: 5000,5000"
	echo "     -C     nagios crit    value. Reference -W. Default: 10000,10000"
	echo "     -V     net-snmp version. Default 2c."
	exit 2
}

DefaultValue(){
	if [ -z "$IP" -o -z "$nicdesc" -o -z "$community" ];then
		echo -e "Error:Parameter not enough.\n"
		Usage
	fi
	if [ -z "$warn" ];then
		warn="5000,5000"
	fi
	if [ -z "$crit" ];then
		crit="10000,10000"
	fi
	if [ -z "$version" ];then
		version=2c
	fi
}

GetResult(){
	while [ 1 ]
	do
		index=`snmpwalk -v $version -c $community $IP IF-MIB::ifDescr | grep "${nicdesc}$" |awk -F '.' '{print $2}' |awk '{print $1}'`
		if [ $? -ne 0 ];then
		    echo "Error: snmpwalk wrong."
		    exit 2
		fi
		if [ -z "$index" ];then
			continue
		else
			break
		fi
	done
	tempfile="/tmp/traffic.${IP}-$index"
	
	while [ 1 ]
	do
		if [ -f "$tempfile" ];then
			value=`cat $tempfile`
			last_time=`echo "$value" | awk '{print $1}'`
			last_in_traffic=`echo "$value" |awk '{print $2}'`
			last_out_traffic=`echo "$value" |awk '{print $3}'`

			now_time=`date +%s`
			now_in_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifInOctets.${index} |awk '{print $NF}'`
			now_out_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifOutOctets.${index} |awk '{print $NF}'`
			
			if [ -z "$now_in_traffic" -o -z "$now_out_traffic" ];then
				sleep 10
				continue
			fi

			in_traffic=$(($now_in_traffic - $last_in_traffic))
			out_traffic=$(($now_out_traffic - $last_out_traffic))
			second=$(($now_time - $last_time))
		else
			now_time=`date +%s`
			now_in_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifInOctets.${index} |awk '{print $NF}'`
			now_out_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifOutOctets.${index} |awk '{print $NF}'`
			if [ -z "$now_in_traffic" -o -z "$now_out_traffic" ];then
				sleep 10
				continue
			fi
			in_traffic=0
			out_traffic=0
		fi

		echo "$now_time $now_in_traffic $now_out_traffic" > $tempfile
		if [ $? -ne 0 ];then
			echo "$tempfile write fail."
				exit 2
		fi

		if [ $in_traffic -le 0 -o $out_traffic -le 0 ];then
			sleep 10
			continue
		else
		        in_result=$(($in_traffic / $second / 1024 * 8))
			out_result=$(($out_traffic / $second / 1024 * 8))
			break
		fi
	done

	#warn vaule
	in_warn=`echo $warn |awk -F ',' '{print $1}'`
	out_warn=`echo $warn |awk -F ',' '{print $2}'`

	#crit value
	in_crit=`echo $crit | awk -F ',' '{print $1}'`
	out_crit=`echo $crit | awk -F ',' '{print $2}'`

	echo "IN: ${in_result}Kbps[${in_warn}Kbps][${in_crit}Kbps]  OUT: ${out_result}Kbps[${out_warn}Kbps][${out_crit}Kbps] | IN=${in_result}Kb; OUT=${out_result}Kb;"
	if [ $in_result -ge $in_crit -o $out_result -ge $out_crit ];then
		exit 2
	fi
	if [ $in_result -ge $in_warn -o  $out_result -ge $out_warn ];then
		exit 1
	fi
	exit 0
}

while getopts H:P:N:W:C:V: args
do
	case $args in
		H)
			IP="$OPTARG"
			;;
		P)
			community="$OPTARG"
			;;
		W)
			warn="$OPTARG"
			;;
		C)
			crit="$OPTARG"
			;;
		V)
			version="$OPTARG"
			;;
		N)
			nicdesc="$OPTARG"
			;;
		?)
		Usage
	esac
done
DefaultValue
GetResult

需要注意的地方:

  1. 使用IF-MIB::ifDescr,使用网卡名称来获取网卡对应的索引值,然后使用IF-MIB::ifInOctets.{index}和IF-MIB::ifOutOctets.{index}来获取网卡的进出流量。

    使用snmpwalk -v 2c -c public 127.0.0.1 IF-MIB::ifDescr 来查看有哪些接口,找到自己对应的。

  2. 要保证使用snmpwalk能抓取到数据。所以脚本中使用死循环来判断。

  3. 由于使用的snmp的32位计数器,所以当达到最大值时,计数器会从头开始。这样取两次间隔时会出现负数,所以脚本中有判断两次间隔的值一定要大于0。同时使用sleep 10来延迟10秒再取值,同时 这个间隔时间最好不要小于10s,因为使用snmpwalk抓取数据时,间隔太小会导致抓取到的值是一样的。

  4. 临时文件的权限,如果先手动运行就会就root用户,导致放在naigos中的时候不能写临时文件。

  5. 被监控机要安装snmp服务。

  6. 本脚本实用于linux 和windows。同时满足pnp4nagios的绘图。当然要自己定义模板(见下面)

    

本人使用:

定义nagiso命令:

define command{
        command_name    check_traffic
        command_line    $USER1$/check_traffic.sh -H $HOSTADDRESS$ -P $ARG1$ -W $ARG2$ -C $ARG3$ -N $ARG4$  #ARG1 is snmp-community; ARG2 is warn; ARG3 is crit; ARG4 is NIC Name
}

定义nagios 服务:

windows 的

define service{
        use                     XXX
        host_name               XXX
        service_description     wan_traffic
        check_command           check_traffic!public!5000,5000!9000,9000!"Net Device PV Driver #2"
}

linux的

define service{
        use                     XXX
        host_name               XXX
        service_description     wan_traffic
        check_command           check_traffic!public!10000,12000!14000,14000!eth2
}


pnp4nagios的模板

[root@nagios pnp4nagios]# cat share/templates/check_traffic.php 
<?php
#
## Copyright (c) 2006-2010 Joerg Linge (http://www.pnp4nagios.org)
## Plugin: check_iftraffic.pl (COUNTER)
#
$opt[1]  = "--vertical-label \"Traffic\" -b 1024 --title \"Interface Traffic for $hostname / $servicedesc\" ";
$def[1]  = "DEF:var1=$RRDFILE[1]:$DS[1]:AVERAGE " ;
$def[1] .= "DEF:var2=$RRDFILE[2]:$DS[2]:AVERAGE " ;
$def[1] .= "LINE1:var1#003300:\"in\" " ;
$def[1] .= "GPRINT:var1:LAST:\"%7.2lf %SKb/s last\" " ;
$def[1] .= "GPRINT:var1:AVERAGE:\"%7.2lf %SKb/s avg\" " ;
$def[1] .= "GPRINT:var1:MAX:\"%7.2lf %SKb/s max\\n\" " ;
$def[1] .= "LINE1:var2#00ff00:\"out\" " ;
$def[1] .= "GPRINT:var2:LAST:\"%7.2lf %SKb/s last\" " ;
$def[1] .= "GPRINT:var2:AVERAGE:\"%7.2lf %SKb/s avg\" " ;
$def[1] .= "GPRINT:var2:MAX:\"%7.2lf %SKb/s max\" "
?>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI