本文共 3217 字,大约阅读时间需要 10 分钟。
阿里云Redis集群有多个节点,用户需要查看每个子节点的内存还有key数目,目前阿里云Redis提供了iinfo命令用于查看某个节点的性能数据,后续会在控制台展示每个节点的数据。
下载python客户端
wget “”tar -xvf redis-2.10.5.tar.gz
cd redis-2.10.5sudo python setup.py installiinfo node [section](node为数字,node取值为从0到节点数目-1, section为info官方一致的值)
import sysimport redisfrom redis._compat import nativestrdef 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 host port password之后会输出如下内容
============ node 0 ================used_memory_human: 37.56M# Keyspacedb0:keys=9887,expires=0,avg_ttl=0============ node 1 ================used_memory_human: 37.58M# Keyspacedb0:keys=9835,expires=0,avg_ttl=0db1:keys=1,expires=0,avg_ttl=0============ node 2 ================used_memory_human: 41.24M# Keyspacedb0:keys=9956,expires=0,avg_ttl=0db1:keys=1,expires=0,avg_ttl=0============ node 3 ================used_memory_human: 37.58M# Keyspacedb0:keys=9863,expires=0,avg_ttl=0============ node 4 ================used_memory_human: 37.61M# Keyspacedb0:keys=10045,expires=0,avg_ttl=0============ node 5 ================used_memory_human: 37.58M# Keyspacedb0:keys=10038,expires=0,avg_ttl=0============ node 6 ================used_memory_human: 37.58M# Keyspacedb0:keys=10055,expires=0,avg_ttl=0============ node 7 ================used_memory_human: 37.57M# Keyspacedb0: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}}
转载地址:http://kubga.baihongyu.com/