10477777 发表于 2018-8-3 12:19:43

check_memcache with python

  
#!/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 isff
  
--critical|-c)
  
Sets a critical level for memcache use memory.Default is:off. """
  
sys.exit(0)
  

  
try:
  
options,args = getopt.getopt(sys.argv,"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
  

  
except:
  
print 'please check your memcached host and port'
  
sys.exit(3)
  

  
#for key in a:
  
#print '%s : %s' % (key,a)
  

  
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 = &quot;'use'=%skb 'free'=%s 'hitrate'=%s 'connection'=%s&quot; % (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)
  
页: [1]
查看完整版本: check_memcache with python