云数据库Redis版集群有多个节点,用户需要查看每个子节点的内存以及Key的数目,目前阿里云提供了iinfo命令用于查看某个节点的性能数据,也可以通过控制台查看相关数据。本文主要介绍通过Python脚本如何查看Redis集群子实例内存。
wget "https://pypi.python.org/packages/68/44/5efe9e98ad83ef5b742ce62a15bea609ed5a0d1caf35b79257ddb324031a/redis-2.10.5.tar.gz#md5=3b26c2b9703b4b56b30a1ad508e31083"
tar -xvf redis-2.10.5.tar.gz
cd redis-2.10.5
sudo python setup.py install
import sys
import redis
from redis._compat import nativestr
def parse_info(response):
"Parse the result of Redis's INFO command into a Python dict"
info = {}
response = nativestr(response)
def get_value(value):
if ',' not in value or '=' not in value:
try:
if '.' in value:
return float(value)
else:
return int(value)
except ValueError:
return value
else:
sub_dict = {}
for item in value.split(','):
k, v = item.rsplit('=', 1)
sub_dict[k] = get_value(v)
return sub_dict
for line in response.splitlines():
if line and not line.startswith('#'):
if line.find(':') != -1:
key, value = line.split(':', 1)
info[key] = get_value(value)
else:
# if the line isn't splittable, append it to the "__raw__" key
info.setdefault('__raw__', []).append(line)
return info
if __name__ == '__main__':
if len(sys.argv) != 4:
print 'Usage: python ', sys.argv[0], ' host port password '
exit(1)
db_host = sys.argv[1]
db_port = sys.argv[2]
db_password = sys.argv[3]
r = redis.StrictRedis(host=db_host, port=int(db_port), password=db_password)
nodecount = r.info()['nodecount']
for node in range(0, nodecount):
info = r.execute_command("iinfo", str(node))
info_res = parse_info(info)
print "============ node ", str(node), " ================"
print 'used_memory_human:', info_res['used_memory_human']
print r.execute_command("iinfo", str(node), "keyspace")
info_res = r.info()
print "============ total ================"
print 'used_memory_human:', info_res['used_memory_human']
print r.info('keyspace')
python check_sharding_db [$DB_Host] [$DB_Port] [$DB_Pass]
注:系统显示类似如下。
- [$DB_Host]:Redis数据库的主机名或IP地址。
- [$DB_Port]:Redis数据库的端口号,默认为“6379”。
- [$DB_Pass]:Redis数据库的密码。
============ node 0 ================
used_memory_human: 37.56M
# Keyspace
db0:keys=9887,expires=0,avg_ttl=0
============ node 1 ================
used_memory_human: 37.58M
# Keyspace
db0:keys=9835,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
============ node 2 ================
used_memory_human: 41.24M
# Keyspace
db0:keys=9956,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
============ node 3 ================
used_memory_human: 37.58M
# Keyspace
db0:keys=9863,expires=0,avg_ttl=0
============ node 4 ================
used_memory_human: 37.61M
# Keyspace
db0:keys=10045,expires=0,avg_ttl=0
============ node 5 ================
used_memory_human: 37.58M
# Keyspace
db0:keys=10038,expires=0,avg_ttl=0
============ node 6 ================
used_memory_human: 37.58M
# Keyspace
db0:keys=10055,expires=0,avg_ttl=0
============ node 7 ================
used_memory_human: 37.57M
# Keyspace
db0:keys=9969,expires=0,avg_ttl=0
============ total ================
used_memory_human: 304.31M
{'db1': {'keys': 2, 'expires': 0, 'avg_ttl': 0}, 'db0': {'keys': 79648, 'expires': 0, 'avg_ttl': 0}}