这篇文章给大家介绍Shell中怎么利用memcache监控缓存命中率,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
#!/bin/bash
###############################
#检查memcached的命中率
#加载nagios自带utils.sh
###############################
source/usr/local/nagios/libexec/utils.sh
print_usage()
{
echo"check_memcached -H IP -P port -w warning -c critical"
}
###################
#获取命令行执行参数
###################
whiletest-n "$1";do
case"$1"in
-H)
host=$2
shift
;;
-P)
port=$2
shift
;;
-w)
warning=$2
shift
;;
-c)
critical=$2
shift
;;
*)
echo"Unknown argument:$1"
print_usage
exit$STATE_UNKNOWN
;;
esac
shift
done
########################
#function div_f()
#检查参数,返回两个数字比
########################
functiondiv_f()
{
ref=`awk-vnum_a=$1 -vnum_b=$2 'BEGIN{printf "%0.2f \n",num_a/num_b}'`;
echo$ref;
}
##################
#得到命中率函数
##################
functiongetMemcachedHits()
{
memcachedinfo=`/usr/local/nagios/libexec/check_tcp-H $host -p $port -E -s'stats\r\nquit\r\n'-e 'uptime'| tr"\r""@"`
get_hits=`echo$memcachedinfo | grep-o "@ STAT get_hits [0-9]*"| awk'{print $4}'`
cmd_get=`echo$memcachedinfo | grep-o "@ STAT cmd_get [0-9]*"| awk'{print $4}'`
div_f $get_hits $cmd_get;
}
hits=`getMemcachedHits $host $port`;
##################
#得到命中率所在区间
##################
functionre_rang()
{
rang=$hits;
interval_a=$critical;
interval_b=$warningl
if[[ $rang < $interval_a ]];then
echo"0";
elif[[ $rang < $interval_b ]];then
echo"1";
elif[[ $rang > $interval_b ]]||[[ $rang == $interval_b ]] ;then
echo"2";
else
return;
fi
}
res=`re_rang $critical $warning $hits`;
case"$res"in
0)
echo"Critical memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
exit$STATE_CRITICAL
;;
1)
echo"Warning memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
exit$STATE_WARNING
;;
2)
echo"Ok memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
exit$STATE_OK
;;
*)
echo"Unkown"
exit$STATE_UNKNOWN
;;
esac
关于Shell中怎么利用memcache监控缓存命中率就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。