获得 redis key 大小
redis 用过一段时间后,发现一个KEY每天需更新值,但总是更新不全,故此为了定位问题,整理此脚本,辅助监控一下
写了两个脚本
- 注意:需要提前从 https://github.com/wangganyu188/redis-py.git ,下载redis支持python的api包并安装
git clone https://github.com/wangganyu188/redis-py.git
cd redis-py
python setup install
rediskeyinfo_monitor.py
#!/usr/bin/env python
#coding:utf8
#author: [email protected]
#redis_key_info_monitor.py
import redis
import time,os,sys
COLOR_RED = "\033[31;49;1m %s \033[31;49;0m"
COLOR_GREED = "\033[32;49;1m %s \033[39;49;0m"
COLOR_YELLOW = "\033[33;49;1m %s \033[33;49;0m"
COLOR_BLUE = "\033[34;49;1m %s \033[34;49;0m"
COLOR_PINK = "\033[35;49;1m %s \033[35;49;0m"
COLOR_GREENBLUE = "\033[36;49;1m %s \033[36;49;0m"
REDIS_HOST="192.168.1.100" #配置redis服务器ip地址
REDIS_PORT= 6379 #配置redis服务端口号
def getHumanSize(value):
gb = 1024 * 1024 * 1024.0
mb = 1024 * 1024.0
kb = 1024.0
if value >= gb:
return COLOR_RED % (str(round(value / gb, 2)) + " gb")
elif value >= mb:
return COLOR_YELLOW % (str(round(value / mb, 2)) + " mb")
elif value >= kb:
return COLOR_BLUE % (str(round(value / kb, 2)) + " kb")
else:
return COLOR_GREED % (str(value) + "b")
def get_KEY_size(key):
month = 3600 * 24 * 30
result = []
client = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
client.info()
count = 0
if len(client.keys(key)) == 0:
print "\033[31;1m keys: %s not exist \033[0m"%key
sys.exit()
else:
for key in client.keys(key):
try:
count += 1
nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
idleTime = client.object(‘idletime‘, key)
refcount = client.object(‘refcount‘, key)
length = client.debug_object(key)[‘serializedlength‘]
value = idleTime * refcount
message = "%s cktime: %s key: %s , idletime: %s,refcount: %s, length: %s , humSize: %s\n" % (count,nowtime,key, idleTime, refcount, length, getHumanSize(length))
with open(‘%s.log‘%key,‘a‘) as f:
f.write(message)
print "%s cktime: %s key: %s , idletime: %s,refcount: %s, length: %s , humSize: %s\n" % (count,nowtime,key, idleTime, refcount, length, getHumanSize(length))
except Exception:
pass
if __name__ == "__main__":
#while True:
# 此方法可以获取redis库中所有keys信息
get_KEY_size(‘*‘)
get_KEY_size方法再扩展一下,获取的信息,后期可以对redis应用情况做分析,
** getrediskey_info.py**
主要功能是控制每一分钟去redis库查询一次 key的信息,主要是监控
#!/usr/bin/env python
#coding:utf8
#author:[email protected]
#scriptname: get_redis_key_info.py
import os,sys,time
from redis_key_info_monitor import get_KEY_size
def help_prompt():
print """
This program prints files to the standard output.
Options include:
--version : Prints the version number
--help : Helpful tips
sample : python %s --keys <key>
"""%(__file__)
if len(sys.argv) < 2:
print "\033[31;1mno argument\033[0m"
help_prompt()
sys.exit()
if sys.argv[1].startswith(‘--‘):
option = sys.argv[1][2:]
if option == ‘version‘:
print ‘Version 0.1‘
elif option == ‘help‘:
help_prompt()
elif option == "keys" and sys.argv[2] is not None:
nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print "\033[32;1m **********************%s get key %s *****************\033[0m" %(nowtime,sys.argv[1])
while True:
get_KEY_size(sys.argv[2])
time.sleep(60)
else:
help_prompt()
else:
help_prompt()
执行示例
python get_redis_key_info.py --keys abc
nagios 监控redis 脚本
- 切记应用此脚前需要安装 支持python语言的 redis-py
#!/usr/bin/env python
#coding:utf8
#version 0.2
#author: [email protected]
import sys
import argparse
import redis
#SET NAGIOS EXIT STATUS CODE
ST_OK=0 #
ST_WR=1 #
ST_CR=2 #
ST_UK=3 #
#INITIALIZE SYS ARGUMENTS PARSER
parser = argparse.ArgumentParser()
parser.add_argument(‘-H‘, ‘--host‘, help=‘Redis Server IP Address‘)
parser.add_argument(‘-p‘, ‘--port‘, type=int, default=6379, help=‘Redis Service Port default:6379‘)
parser.add_argument(‘-d‘, ‘--dbid‘, type=int, default=0, help=‘Redis Database ID Number default:0‘)
parser.add_argument(‘-w‘, ‘--warning‘, type=int, default=50, help=‘Threshold Warning Percent default:50‘)
parser.add_argument(‘-c‘, ‘--critical‘, type=int, default=80, help=‘Threshold Critical Percent default:80‘)
parser.add_argument(‘-v‘, ‘--verbose‘,action="count", help=‘Increase Output Verbosity Example:-v -vv‘)
args = parser.parse_args()
rHost = args.host
rPort = args.port
rWarn = float(args.warning)
rCrit = float(args.critical)
rDbid = args.dbid
#SET REDIS CONNECTION
rConn = redis.Redis(host=rHost, port=rPort, socket_timeout=3)
if len(sys.argv) < 2:
print "\033[31;1m Invalid operation Please use help - h \033[0m"
else:
if rConn.ping():
# GET SOME VALUES OF SERVER STATUS
rConf = rConn.config_get()
rConfMaxmem = rConf[‘maxmemory‘]
rInfo = rConn.info()
rInfoCurrmem = rInfo[‘used_memory‘]
rInfoCurrconns = rInfo[‘connected_clients‘]
#rInfoDbkeys = rInfo[‘db%s‘%(rDbid)][‘keys‘]
rInfoMemusage = float("%.2f"%(float(rInfoCurrmem) / float(rConfMaxmem) * 100))
#print "#############rrrrrrrrrrrr",rConf,rInfoMemusage
#print "#########",rInfoMemusage,type(rInfoMemusage),rCrit,type(rCrit)
# CHECK OUTPUT IF VERBOSITY IS CALLED
if args.verbose == 2:
rOutput = rInfo.items()
elif args.verbose == 1:
rOutput = "SET MAX MEMORY IS %0.2f%%; CURRENT USED MEMORY IS %0.2f%%; CURRENT CONNECTIONS IS %d; %sKEYS IN DB%d"%(rConfMaxmem, rInfoCurrmem,rInfoCurrconns, rInfoDbkeys, rDbid)
else:
rOutput = "MEMORY USEAGE %0.2f%%; CURRENT CONNECTTONS IS: %d"%(rInfoMemusage, rInfoCurrconns)
#DECIDE THE MEMORY USAGE STATUS
if rInfoMemusage >= rCrit:
print ‘CRITICAL - %s‘%rOutput
sys.exit(ST_CR)
elif rInfoMemusage >= rWarn:
print ‘WARNING - %s‘%(rOutput)
sys.exit(ST_WR)
else:
print ‘OK - %s‘%(rOutput)
sys.exit(ST_OK)
else:
print ‘PROBLEM IN CONNECTING SERVER‘
sys.exit(ST_UK)
执行示例
/usr/local/nagios/libexec/check_redis.py -H 192.168.1.10 -p 6379
时间: 2024-10-14 08:46:22