#!/bin/bash
#################################################
#
#      Filename: monitor_ovs_upcall_limit.sh
#
#        Author: allin.han@ucloud.cn
#   Description: ovs_upcall_limit_couner
#        Create: 2020-05-26 15:51:39
# Last Modified: 2020-05-26 15:51:39(allin.han@ucloud.cn)
#################################################

basepath=$(cd `dirname $0`; pwd)
filename=`echo $0|awk -F '/' '{print $NF}'`
tmpfile="$basepath/tmp/${filename}.tmp"
tmpdir=`dirname $tmpfile`
[ ! -d $tmpdir ] && mkdir $tmpdir
now=$(date +%s)

collect() {
        value=`cat /proc/sys/net/ovs/ovs_upcall_limit_couner`
	#获取不到填充oldvalue
	if [ "$value" == "" ];then
		value=$oldvalue
	fi
	#查看是否有历史值
	old=`cat $tmpfile`
	if [ "$old" == "" ];then
		echo "$value|$now" >> $tmpfile
	else
		oldvalue=`echo $old|awk -F '|' '{print $1}'`
		oldts=`echo $old|awk -F '|' '{print $2}'`
		ratevalue=$(($value-$oldvalue))
   		# 有些情况下，oldvalue为空，导致差值=value造成误报，暂时用这种方式过滤错误值
		if [[ $ratevalue -eq $value ]];then
			ratevalue=0
		fi
		#删除老值插入新值
		echo "$value|$now" > $tmpfile
		# 输出结果
		if [ "$ratevalue" -ge 0 ];then
			echo '[{"value":'$ratevalue',"tags":""}]'
		fi
	fi
}

collect 
exit 0
