falcon 結(jié)合腳本redis-monitor.py進(jìn)行監(jiān)控redis
* * * * * python /usr/local/openfalcon-0.2/mymon/redis-monitor.py &> /usr/local/openfalcon-0.2/mymon/redis-monitor.log
cat redis-monitor.py
#注意修改配置項(xiàng):_redis_cli,def __init__,insts_list,ip
#!/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,請(qǐng)將下面的值替換為你到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中保存了redis配置文件列表,程序?qū)倪@些配置中讀取port和password,建議使用動(dòng)態(tài)發(fā)現(xiàn)的方法獲得,如:
# 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]\{4,5\}\)/\\1/p' %s" % inst)
passwd = commands.getoutput("sed -n 's/^requirepass *\([^ ]*\)/\\1/p' %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:
#一些老版本的redis中info輸出的信息很少,如果缺少一些我們需要采集的key就跳過(guò)
if key not in stats.keys():
continue
#計(jì)算命中率
if key == 'keyspace_hit_ratio':
try:
value = float(stats['keyspace_hits'])/(int(stats['keyspace_hits']) + int(stats['keyspace_misses']))
except ZeroDivisionError:
value = 0
#碎片率是浮點(diǎn)數(shù)
elif key == 'mem_fragmentation_ratio':
value = float(stats[key])
else:
#其他的都采集成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"}' % connection
if __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 進(jìn)程監(jiān)控
redis狀態(tài)監(jiān)控
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:falcon監(jiān)控redis-創(chuàng)新互聯(lián)
瀏覽地址:http://www.rwnh.cn/article12/cehdgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、商城網(wǎng)站、全網(wǎng)營(yíng)銷推廣、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容