http://exchange.nagios.org/上有shell版本的check_memcache插件,nagios可以监控到命中率,是基于telnet 的,使用之后看到进程里有个telnet 进程,很不爽,而且脚本需要修改一下,不然会报错。而我需要的是监控命中率、内存使用率以及当前连接数。所以只能自己动手来写个脚本了。
#!/usr/bin/python
import memcache
import getopt
import sys
def usage():
print """check_memcache is a Nagios to monitor memcached memory Plugin
Usage:
check_memcache [-h|--help][-w|warning][-c|critical]
Options:
--help|-h)
print check_memcache help
--warning|-w)
Sets a warning level for memcache use memory.Default is ff
--critical|-c)
Sets a critical level for memcache use memory.Default is:off. """
sys.exit(0)
try:
options,args = getopt.getopt(sys.argv[1:],"hw:c:",["help","warning=","critical="])
except getopt.GetoptError:
usage()
sys.exit(3)
for name,value in options:
if name in ("-h","--hlep"):
usage()
if name in ("-w","--warning"):
warning = int(value)
if name in ("-c","--critical"):
critical = int(value)
try:
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
conn = mc.get_stats()
memstat = conn[0][1]
except:
print 'please check your memcached host and port'
sys.exit(3)
#for key in a:
# print '%s : %s' % (key,a[key])
tobal = float(memstat['limit_maxbytes'])
memory = float(memstat['bytes'])
free = (tobal - memory)/1024
free = '%d%s' % (free,'kb')
connection = memstat['accepting_conns']
memory = memory * 1024
if memory < 0.01:
memory = 0.01
else:
memory = int(memory)
get_miss = float(memstat['get_misses'])
get_hits = float(memstat['get_hits'])
if get_miss == 0 and get_hits == 0:
hitrate = 100
else:
hitrate = get_hits/(get_hits + get_miss)*100
hitrate = '%d%s' % (hitrate,'%')
output = 'use:%skb,free:%s,hitrate:%s,connection:%s' % (memory,free,hitrate,connection)
perfdata = "'use'=%skb 'free'=%s 'hitrate'=%s 'connection'=%s" % (memory,free,hitrate,connection)
if 'warning' in dir() and 'critical' in dir():
if memory >= warning:
print 'WARNING - %s|%s' % (output,perfdata)
sys.exit(1)
elif memory >=critical:
print 'CRITICAL - %s|%s' % (output,perfdata)
sys.exit(2)
else:
print 'OK - %s|%s' % (output,perfdata)
sys.exit(0)
else:
print 'OK - %s|%s' % (output,perfdata)
sys.exit(0)
command[check_memcache]=/usr/lib/nagios/plugins/check_memcache -w $ARG1$ -c $ARG2$