"" "Server Density agent Plugin for memcachedthis script requires a config entry in the Agent config[memcached]host = ' 127. 0.0.1 ' port = 12000 "" "Import sysimport telnetlibimport reimport socketimport loggingimport jsonimport timeclass Memcached : Def __init__ (self, agentconfig, Checkslogger, rawconfig): Self.agentconfig = Agentconfig Self.checkslog GER = Checkslogger Self.rawconfig = Rawconfig def run (self): stats = {} if hasattr (self.rawconfig[ ' Memcached '], ' host '): host = self.rawconfig[' Memcached ' [' host '] else:host = ' 127.0.0.1 ' If Hasattr (self.rawconfig[' Memcached '), ' Port '): Port = self.rawconfig[' Memcached ' [' Port '] else: Port = 12000 Try:telnet = telnetlib. Telnet () Telnet.open (host, Port) telnet.write (' stats\r\n ') out = Telnet.read_until ("END") Telnet.write (' quit\r\n ') telnet.close () except socket. Error, Reason:sys.stderr.write ("%s\n"% reason) Sys.stderr.write ("is memcached running?\n") Sys.stderr.write ("Host:%s Port:%s\n"% (Host, Port)) return stats # Current/total stats[ ' Curr_items ' = Int (Re.search ("Curr_items (\d+)", out). Group (1)) stats[' total_items '] = Int (Re.search ("Total_items (\d+) ", out). Group (1)) # Memory usgae stats[' limit_maxbytes ' = Int (Re.search (" Limit_maxbytes (\d+) ", out). g Roup (1)) stats[' bytes '] = Int (re.search ("bytes", out). Group (1)) # Network \d+ traffic ' stats[ Read '] = Int (Re.search ("Bytes_read (\d+)", out). Group (1)) stats[' bytes_written '] = Int (Re.search ("Bytes_written" (\d +) ", out). Group (1)) # Connections stats[' curr_connections ' = Int (Re.search (" Curr_connections (\d+) "). G Roup (1)) stats[' total_connections '] = Int (Re.search ("Total_connections", Out). Group (1)) # \d+ S stats[' Cmd_get '] = Int (Re.search ("Cmd_get (\d+)", out). Group (1)) stats[' cmd_set ' = Int (Re.search ("Cmd_set (\d+)"). Group (1)) stats[' get_hits ' = Int (Re.search ("Get_hits (\d+)", out). Group (1)) stats[' get_misses '] = Int (Re.search ("get_ Misses (\d+) ", out). Group (1)) stats[' delete_hits '] = Int (Re.search (" Delete_hits "), and Out. Group (1)) \d+ [' delete_misses '] = Int (Re.search ("Delete_misses (\d+)", out). Group (1)) stats[' incr_hits '] = Int (Re.search ("Incr_hi TS (\d+) ", out). Group (1)) stats[' decr_hits '] = Int (Re.search (" Decr_hits "), and Out). Group (1)) \d+ ' stats[ Isses '] = Int (Re.search ("Incr_misses (\d+)", out). Group (1)) stats[' decr_misses '] = Int (Re.search ("Decr_misses" (\d+) ", out). Group (1)) # Evictions stats[' evictions ' = Int (Re.search (" Evictions (\d+) "). Group (1)) St ats[' reclaimed ' = Int (Re.search ("Reclaimed (\d+)", out). Group (1)) return statsif __name__ = ' __main__ ': "" " Standalone Test ConfiguRation "" "Raw_agent_config = {' Memcached ': {' host ': ' 127.0.0.1 ', ' Port ': 12000, }} Main_checks_logger = Logging.getlogger (' Memcached ') main_checks_logger.setlevel (logging. DEBUG) Main_checks_logger.addhandler (logging. Streamhandler (sys.stdout)) Host_check = Memcached ({}, Main_checks_logger, Raw_agent_config) while True:try: Print Json.dumps (Host_check.run (), indent=4, sort_keys=true) except:main_checks_logger.excep tion ("Unhandled Exception") Finally:time.sleep (60)
Written in Python, you can write a record to a log file, and it's better to paint a picture with gnuplot or RRDtool.
Simple implementation of Memcache's monitoring script