Several servers in the company are running multiple memcached processes. In addition to monitoring whether the memcached works normally, whether the instance exists, the company also needs to monitor their cache hit rate. Set an alarm for their cache hit rate. There is a perl written on the Internet and needs to be compiled and installed. I wrote a python file and pasted it to run it. I found that the python library version is incorrect. This simple script is so difficult that you can manually write it. The more I write, the more I feel that shell is too weak to process data, functions, and string arrays.
Several servers in the company are running multiple memcached processes. In addition to monitoring whether the memcached works normally, whether the instance exists, the company also needs to monitor their cache hit rate. Set an alarm for their cache hit rate.
There is a perl written on the Internet and needs to be compiled and installed. I wrote a python file and pasted it to run it. I found that the python library version is incorrect. This simple script is so difficult that you can manually write it.
The more I write, the more I feel that shell is too weak to process data, functions, and string arrays.
Okay. Use it together. This script is relatively simple. You can use the check_tcp script to obtain the stats information of memcached. Then, format the information into a string, and then compare get_hits with the previous response _gets to get the cache hit rate. The basic principle is relatively simple. However, the nagios alarm format and performance data output must also be implemented, supporting pnp plotting.
In order to make it easier for the Party to reach out, you can directly Insert the previous functions into the script as needed, and copy and paste them to run them.
If any bug exists, please report it!
#! /Bin/bash ################################ check the memcached hit rate # load the built-in utils of nagios. sh ############################### source/usr/local/nagios/ libexec/utils. shprint_usage () {echo "check_memcached-h ip-P port-w warning-c critical" }################## obtain commands line execution parameters ################### whiletest-n "$1 "; docase "$1" in-H) host = $2 shift;-P) port = $2 shift;-w) warning = $2 shift;-c) critical = $2 shift; *) echo "Unknown argument: $1" print_usageexit $ STATE_UNKNOWN ;; esacshiftdone ####################### function div_f () # Check parameters, returns the ratio of two digits ####################### functiondiv_f () {ref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% 0.2f \ n", num_a/num_ B} ''; echo $ ref ;} ################### obtain the hit rate function ################## 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 prop _get [0-9] *" | awk '{print $4} ''div _ f $ get_hits $ response _get ;} hits = 'getmemcachedhits $ host $ port '; ################## obtain the hit rate between zones ################# # functionre_rang () {rang = $ hits; interval_a = $ critical; interval_ B = $ warninglif [[$ rang <$ interval_a]; thenecho "0 "; elif [[$ rang <$ interval_ B]; thenecho "1"; elif [[$ rang> $ interval_ B] | [[$ rang = $ interval_ B]; thenecho "2"; elsereturn; fi} res ='re _ rang $ critical $ warning $ hits '; case "$ res" in0) echo "Critical memcached_hits = $ hits | memcached_hits = $ hits; $ warning; $ critical;" exit $ STATE_CRITICAL; 1) echo "Warning seconds = $ 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