阿里云Redis集群子实例内存查看-阿里云开发者社区

开发者社区> 阿里云数据库> 正文
登录阅读全文

阿里云Redis集群子实例内存查看

简介: 阿里云Redis集群有多个节点,用户需要查看每个子节点的内存还有key数目,本文主要介绍了云数据库Redis如何使用python查看子节点性能数据情况。

阿里云Redis集群有多个节点,用户需要查看每个子节点的内存还有key数目,目前阿里云Redis提供了iinfo命令用于查看某个节点的性能数据,后续会在控制台展示每个节点的数据。

初始化环境

安装python客户端

下载python客户端
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

命令介绍

iinfo node [section](node为数字,node取值为从0到节点数目-1, section为info官方一致的值)

扫描脚本

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 host port password之后会输出如下内容

============ 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}}

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
阿里云数据库
使用钉钉扫一扫加入圈子
+ 订阅

帮用户承担一切数据库风险,给您何止是安心!

官方博客
最新文章
相关文章
链接