A simple Python script that monitors Redis performance has already talked about how to monitor memcached, and now incidentally how to monitor Redis. Let's start with the following information about monitoring redis:
- Redis Ping: Verify Ping
- Redis Alive: Check to see if the port is alive
- Redis connections: View the number of connections
- Redis blockedclients: Waiting on the number of blocked clients
- Connection utilization for Redis Connectionsusage:redis
- Redis Memoryusage:redis Memory Usage
- Redis Memoryusagerate:redis Memory Utilization
- Redis Evictedkeys: Number of keys that have been removed since they were run
- Redis rejectedconnections: Number of rejected connections
- OPS for Redis Ops:redis
- Redis Hitrate:redis Hit Rate
Install the required environment
pip install redis
Script Content
#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' chenmingle ' Import sysimport subprocessimport jsontry:import Redisexcept Exception, E:print ' pip install Redis ' sys.exit (1) class Redis (object): Def __init__ (self, host, PO RT, Password=none): Self.host = host Self.port = Port Self.password = password if self.passwor D:self.rds = Redis. Strictredis (Host=host, Port=port, password=self.password) else:self.rds = Redis. Strictredis (Host=host, port=port) Try:self.info = Self.rds.info () except Exception, E: Self.info = None def redis_connections (self): Try:return self.info[' connected_clients '] exc EPT Exception, E:return 0 def redis_connections_usage (self): try:curr_connections = self . redis_connections () max_clients = Self.parse_config (' maxclients ') rate = float (curr_connections)/ Float (max_clients) return "%.2f"% (rate * +) except Exception, E:return 0 def redis_used_memory (self): Try:return self.info[' used_memory '] except Exception, E:return 0 def Redis_memor Y_usage (self): try:used_memory = self.info[' used_memory '] max_memory = self.info[' MaxMemory ' ] System_memory = self.info[' total_system_memory ') if max_memory:rate = float (used_m Emory)/float (max_memory) else:rate = float (used_memory)/float (system_memory) ret Urn "%.2f"% (rate * +) except Exception, E:return 0 def redis_ping (self): try: Return self.rds.ping () except Exception, E:return False def rejected_connections (self): try: Return self.info[' rejected_connections ' except Exception, E:return 999 def evicted_keys ( Self): try: Return self.info[' Evicted_keys ' except Exception, E:return 999 def blocked_clients (self): Try:return self.info[' blocked_clients '] except Exception, E:return 0 def ops (self): Try:return self.info[' instantaneous_ops_per_sec '] except Exception, E:return 0 de F Hitrate (self): try:misses = self.info[' keyspace_misses '] hits = self.info[' Keyspace_hits '] Rate = float (hits)/float (int (hits) + int (misses)) return '%.2f '% (rate *) except EXCE Ption, E:return 0 def parse_config (self, type): Try:return self.rds.config_get (type) [Typ E] except Exception, E:return None def Test (self): print ' Redis ping:%s '% self.redis_ping () print ' Redis alive:%s '% check_alive (self.host, self.port) print ' Redis connections:%s '% Self.redis_con Nections () Print' Redis blockedclients%s '% self.blocked_clients () print ' Redis connectionsusage:%s%% '% Self.redis_connections_us Age () print ' Redis memoryusage:%s '% self.redis_used_memory () print ' Redis memoryusagerate:%s%% '% self.re Dis_memory_usage () print ' Redis evictedkeys:%s '% Self.evicted_keys () print ' Redis rejectedconnections:%s ' % self.rejected_connections () print ' Redis Ops:%s '% self.ops () print ' Redis hitrate:%s%% '% self.hitrate () def check_alive (host, port): cmd = ' nc-z%s%s >/dev/null 2>&1 '% (host, port) return Subprocess.call ( CMD, shell=true) def parse (type, host, port, password): RDS = Redis (host, port, password) if type = = ' Connections ': Print rds.redis_connections () elif type = = ' Connectionsusage ': Print rds.redis_connections_usage () elif Type = = ' blockedclients ': Print rds.blocked_clients () elif type = = ' ping ': Print rds.redis_ping () Eli F type = = ' Alive ': Print check_alive (host, port) elif type = = ' Memoryusage ': Print rds.redis_used_memory () elif type = = ' Memor Yusagerate ': Print rds.redis_memory_usage () elif type = = ' rejectedconnections ': Print Rds.rejected_connec tions () elif type = = ' Evictedkeys ': Print Rds.evicted_keys () elif type = = ' Hitrate ': Print rds.hitrate () elif type = = ' Ops ': Print Rds.ops () else:rds.test () if __name__ = = ' __main__ ': try:type = sys.argv[1] host = sys.argv[2] Port = sys.argv[3] If sys.argv.__len__ () >=5:password = sys.argv[4] Else:password = None except Exception, e:print "Usage:python%s type 127.0.0 .1 6379 "% sys.argv[0] Sys.exit (1) Parse (type, host, port, password)
Test the script to view the Redis information (if Redis does not have a password, do not fill in the password):
/bin/python /home/python/check_redis.py test 192.168.4.18 6379 passwordRedis ping: TrueRedis alive: 0Redis connections: 447Redis blockedClients 0Redis connectionsUsage: 4.47%Redis memoryUsage: 2885122048Redis memoryUsageRate: 17.32%Redis evictedKeys: 0Redis rejectedConnections: 0Redis ops: 1050Redis hitRate: 71.87%
Finally added to the Zabbix custom key
cat /etc/zabbix/zabbix_agentd.d/redis.conf# RedisUserParameter=redis.stats[*],/bin/python /home/python/check_redis.py $1 192.168.4.18 6379 password
The next step to add a custom template is to refer to the previous blog post
A simple Python script that monitors Redis performance