码迷,mamicode.com
首页 > 编程语言 > 详细

监控redis python脚本

时间:2016-10-20 00:05:48      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:

#!/bin/env python
#-*- coding:utf-8 -*-

import json
import time
import socket
import os
import re
import sys
import commands
import urllib2, base64

class RedisStats:
redis_cli = ‘/data/redis/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‘,status=‘info‘):
self.cmd = ‘%s -h %s -p %s %s‘ % (self.redis_cli, host, port,status)

def stats(self):
info = commands.getoutput(self.cmd)
return dict(self.stat_regex.findall(info))

def main():
timestamp = int(time.time())
step = 60

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‘),
]

insts_list = [ ‘/etc/redis/6379.conf‘ ]

with open(‘./cfg.json‘) as f:
data = f.read().replace(‘\n‘,‘‘)
jsonhostname = json.loads(data)
hostpoint = jsonhostname[‘hostname‘]

for inst in insts_list:
port = commands.getoutput("sed -n ‘s/^port *\([0-9]\{4,5\}\)/\\1/p‘ %s" % inst)
metric = "redis"
endpoint = ‘%s‘ %(hostpoint)
tags = ‘port=%s‘ % port

try:
conn = RedisStats()
stats = conn.stats()
except Exception,e:
continue

for key,vtype in monit_keys:
if key == ‘keyspace_hit_ratio‘:
try:
value = float(stats[‘keyspace_hits‘])/(int(stats[‘keyspace_hits‘]) + int(stats[‘keyspace_misses‘]))
except ZeroDivisionError:
value = 0
elif key == ‘mem_fragmentation_ratio‘:
value = float(stats[key])
else:
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:1988/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"}‘ % connection
if __name__ == ‘__main__‘:
proc = commands.getoutput(‘ ps -ef|grep %s|grep -v grep|wc -l ‘ % os.path.basename(sys.argv[0]))
if int(proc) < 5:
main()

#!/bin/env python
#-*- coding:utf-8 -*-

import json
import time
import socket
import os
import re
import sys
import commands
import urllib2, base64

class RedisStats:
    redis_cli = /data/redis/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,status=info):
        self.cmd = %s -h %s -p %s %s % (self.redis_cli, host, port,status)

    def stats(self):
        info = commands.getoutput(self.cmd)
        return dict(self.stat_regex.findall(info))

def main():
    timestamp = int(time.time())
    step = 60
    
    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),
    ]
    
    insts_list = [ /etc/redis/6379.conf ]
  
    with open(./cfg.json) as f:
        data = f.read().replace(\n,‘‘)
        jsonhostname = json.loads(data)
        hostpoint = jsonhostname[hostname]

    for inst in insts_list:
        port = commands.getoutput("sed -n ‘s/^port *\([0-9]\{4,5\}\)/\\1/p‘ %s" % inst)
        metric = "redis"
        endpoint = %s %(hostpoint)
        tags = port=%s % port

        try:
            conn = RedisStats()
            stats = conn.stats()
        except Exception,e:
            continue

        for key,vtype in monit_keys:
            if key == keyspace_hit_ratio:
                try:
                    value = float(stats[keyspace_hits])/(int(stats[keyspace_hits]) + int(stats[keyspace_misses]))
                except ZeroDivisionError:
                    value = 0
            elif key == mem_fragmentation_ratio:
                value = float(stats[key])
            else:
                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:1988/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"} % connection
if __name__ == __main__:
    proc = commands.getoutput( ps -ef|grep %s|grep -v grep|wc -l  % os.path.basename(sys.argv[0]))
    if int(proc) < 5:
        main()

 

监控redis python脚本

标签:

原文地址:http://www.cnblogs.com/fengjian2016/p/5979020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!