Falcon Monitoring redis
Falcon is combined with script redis-monitor.py to monitor redis* python / usr/local/openfalcon-0.2/mymon/redis-monitor.py & > / usr/local/openfalcon-0.2/mymon/redis-monitor.logcat redis-monitor.py# Note to modify the configuration item: _ redis_cli,def _ _ init__,insts_list Ipipflowers binmax Env python#-*- coding:utf-8-*-import jsonimport timeimport socketimport osimport reimport sysimport commandsimport urllib2, base64class RedisStats: # if you compile and deploy to redis yourself Please replace the following value with your path to redis-cli _ redis_cli ='/ usr/local/bin/redis-cli' _ stat_regex = re.compile (ur' (\ w +): ([0-9] +\.? [0-9] *)\ r') def _ _ init__ (self, port='6379', passwd=None) Host='127.0.0.1'): self._cmd ='% s-h% s-p% s info'% (self._redis_cli, host, port) if passwd not in [', None]: self._cmd ='% s-h% s-p% s-a% s info'% (self._redis_cli, host, port) Passwd) def stats (self): 'Return a dict containing redis stats' info = commands.getoutput (self._cmd) # print info return dict (self._stat_regex.findall (info)) def main (): ip = '192.168.1.1' # ip = socket.gethostname () timestamp = int (time.time ()) step = 60 # inst_list saved redis profile list The program will read port and password from these configurations, and it is recommended that you use dynamic discovery to get For example: # inst_list = [i for i in commands.getoutput ("find / etc/-name 'redis*.conf'"). Split ('\ n')] insts_list = ['/ etc/redis/redis.conf'] p = [] monit_keys = [('connected_clients','GAUGE'), (' blocked_clients','GAUGE'), ('used_memory','GAUGE') ('used_memory_rss','GAUGE'), (' mem_fragmentation_ratio','GAUGE'), ('total_commands_processed','COUNTER'), (' rejected_connections','COUNTER'), ('expired_keys','COUNTER'), (' evicted_keys','COUNTER'), ('keyspace_hits','COUNTER'), (' keyspace_misses' 'COUNTER'), (' keyspace_hit_ratio','GAUGE') ] for inst in insts_list: port = commands.getoutput ("sed-n's / ^ port *\ ([0-9]\ {4jue 5\}\) /\\ 1compp'% s"% inst) passwd = commands.getoutput ("sed-n's / ^ postpass *\ ([^] *\) /\\ 1lapp's"% inst) metric = "redis" endpoint = ip Tags = 'port=%s'% port try: conn = RedisStats (port Passwd) stats = conn.stats () except Exception,e: continue for key,vtype in monit_keys: # there is very little information output from info in some older versions of redis If there is no key we need to collect, skip if key not in stats.keys (): continue # calculate the hit rate if key = 'keyspace_hit_ratio': try: value = float (stats [' keyspace_hits']) / (int (stats ['keyspace_hits']) + int ( Stats ['keyspace_misses']) except ZeroDivisionError: value = 0 # fragment rate is floating point elif key = =' mem_fragmentation_ratio': value = float (stats [key]) else: # everything else is collected into counter Int try: value = int (stats [key]) except: continue I = {'Metric':'% s% s'% (metric, key), 'Endpoint': endpoint,' Timestamp': timestamp, 'Step': step 'Value': value,' CounterType': vtype, 'TAGS': tags} p.append (I) print json.dumps (p, sort_keys=True Indent=4) method = "POST" handler = urllib2.HTTPHandler () opener = urllib2.build_opener (handler) url = 'http://127.0.0.1:1998/v1/push' request = urllib2.Request (url, data=json.dumps (p) request.add_header ("Content-Type",' application/json') request.get_method = lambda: method try: connection = opener.open (request) except urllib2.HTTPError E: connection = e # check. Substitute with appropriate HTTP code. If connection.code = = 200: print connection.read () else: print'{"err": 1, "msg": "% s"}'% connectionif _ name__ = ='_ _ main__': proc = commands.getoutput ('ps-ef | grep% s | grep-v grep | wc-l'% os.path.basename (sys.argv [0])) sys.stdout.flush () if int (proc) < 5: main ()
Redis process monitoring
Redis status monitoring