Using Python to monitor memcached basic information
Use Python to monitor memcached's basic information, such as connections, Hitrate, freememory, Memoryusage, evictions, and more. Then customize the Zabbix keys value to implement a custom monitor template! Install the required environment first:
pip install python-memcached
Words not much to say, directly on the script:
#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' chenmingle ' Import sysimport subprocessimport jsontry:import Memcacheexcept Exception, E:print ' pip install python-memcached ' sys.exit (1) class memcached (object): Def __init_ _ (Self, host_list): SELF.MC = memcache. Client (host_list) try:self.stats = Self.mc.get_stats () [0][1] except Exception, E:pas S def get_curr_connections (self): "" "Get current connections for Memcached Userparameter:connect Ions "" "Try:return Int (self.stats[' curr_connections ']) except Exception, e:r Eturn 0 def get_cache_hit_rate (self): "" "Get the ' hit ' for Memcached userparameter:hitrate "" "Try:rate = float (self.stats[' get_hits ')/float (self.stats[' cmd_get ']) return"%.2 F "% (rate * +) except Exception, E:return 0.0 def get_free_memory (self): "" "Get the Free memory in Memcached Byte userparameter:freememory" "" Try:f ree = Int (self.stats[' limit_maxbytes ')-int (self.stats[' bytes ']) return free except Exception, E: return 0 def get_memory_usage_rate (self): "" "Get the memory usage rate in Memcached userpa Rameter:memoryrate "" "Try:rate = float (self.stats[' bytes ')/float (self.stats[' limit_maxbytes ']) return "%.2f"% (rate *) except Exception, E:return 0.0 def get_evictions (self): "" "Get evictd items in Memcached one minute avg userparameter:evictions" "" Try: # minutes = Int (self.stats[' uptime ')/# return int (self.stats[' evictions '])/int (minutes) return int (self.stats[' evictions ')) except Exception, E:return 0 def Test (self): # print J Son.dumps (Self.stats, indent=4) print ' Connections:%s '% self.get_curr_connections () print ' Hitrate:%s percent '% Self.get_cache_hit_ra Te () print ' Freememory:%s Byte '% self.get_free_memory () print ' Memoryusage:%s percent '% self.get_memory_usage _rate () print ' evictions:%s '% self.get_evictions () print ' Alive:%s '% check_alive (host_list) def Check_ali ve (host_list): host = Host_list.split (': ') [0] port = host_list.split (': ') [1] cmd = ' nc-z%s%s >/dev/null 2& Gt;&1 '% (host, port) return Subprocess.call (cmd, shell=true) def parse (type, host_list): MC = Memcached ([host_li ST]) if type = = ' Connections ': Print mc.get_curr_connections () elif type = = ' Hitrate ': Print Mc.get_ca Che_hit_rate () elif type = = ' Freememory ': Print mc.get_free_memory () elif type = = ' Memoryusage ': print Mc.get_memory_usage_rate () elif type = = ' evictions ': Print mc.get_evictions () elif type = = ' Alive ': P Rint check_alive (Host_liST) Else:mc.test () if __name__ = = ' __main__ ': try:host_list = sys.argv[1] Type = sys.argv[2] Except Exception, E:print "Usage:python%s 127.0.0.1:11211 connections"% sys.argv[0] sys.exit (1) par SE (type, host_list)
Routinely test the effect of the script:
[[email protected] zabbix_agentd.d]# /usr/local/bin/python /home/python/check_memcached.py 10.0.0.90:11211 testconnections: 1593hitRate: 98.15 %freeMemory: 849551027 BytememoryUsage: 80.22 %evictions: 7477932alive: 0
Now that you have completed a simple script to monitor the basic information of memcached, the next step is to add a custom monitor to the Zabbix for easy viewing later.
- First define the monitoring item configuration:
cd /etc/zabbix/zabbix_agentd.dcat Memcached.conf# MemcachedUserParameter=memcached.stats[*],/usr/local/bin/python /home/python/check_memcached.py 10.0.0.90:11211 $1
- To configure custom template custom monitoring entries on Zabbix:
This completes a simple monitoring memcached of the custom template, if you think this article is good, please on my GitHub point star, thank you!!
Using Python to monitor memcached basic information