The company has several servers running multiple memcached processes, in addition to monitoring their normal telnet, the existence of an instance to monitor their cache hit rate. Alarm for their cache hit rate.
There's a Perl written on the web that needs to be compiled and installed. There's a python write, post it, run it, and find the Python library version wrong. Such a simple script was so laborious that it was manually written.
The more I write, the more I feel that the shell handles data, functions, and string arrays that are too weak.
Okay, let's do it. This script is relatively simple and obtains memcached stats information through the check_tcp script. The information is then formatted as a string, and then the get_hits is compared to the cmd_gets to get the cache hit rate. The basic principle is relatively simple. But also to implement the Nagios alarm format, as well as the output of performance data, support PNP drawing.
In order to reach the party more conveniently, insert the previous function directly on demand into the script, should be copy paste run on the line.
If there is a bug please feedback!
Copy Code code as follows:
#!/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 "$";d O
Case "$" 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 ()
#检查参数, returns two digits than
########################
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 $} '
cmd_get= ' Echo$memcachedinfo | Grep-o "@ STAT cmd_get [0-9]*] | awk ' {print $} '
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