def stats_redis(port=6379): redis = RedisClient(port=port) info = redis.info() ret = {} #ret['port'] = port for k,v in info.iteritems(): if k in ['instantaneous_ops_per_sec','total_commands_processed','keyspace_misses','keyspace_hits','connected_clients','total_connections_received','used_memory','maxmemory','mem_fragmentation_ratio','keyspace']: ret[k] = v ret['maxmemory'] = redis.config_mem()['maxmemory'] return ret
def connect_test(self): self.ensure_one() try: redis = self._get_strict_redis() _logger.info("Redis Test Instance: %s" % redis) result = redis.info() _logger.info("Redis Test Info: %s" % result) except Exception, e: _logger.error("Redis Test Error: %s" % e) raise UserError(u"Redis连接测试错误,请检查配置:%s" % e)
def get_discovery(redis, hostname): """ Discover 'dynamic' items like http://redis.io/commands/info replication: depends on slave number cluster: empty if not applicable keyspace: depends on database number """ data = {} data[hostname] = {} section_list = { 'keyspace': 'REDISDB' } data[hostname] = { "redis.cluster.discovery":[] } for section, lldvalue in section_list.iteritems(): data[hostname]["redis.%s.discovery" % section] = [] result = redis.info(section) for key, value in result.iteritems(): dsc_data = {"{#%s}" % lldvalue: "%s" % key } data[hostname][("redis.%s.discovery" % (section))].append(dsc_data) return data
def strings_redis(): import redis #charset="utf-8", decode_responses=True => avoid b' in redis python redis = redis.Redis(host='127.0.0.1', port=6379, db=0, charset="utf-8", decode_responses=True) print("-------------") print("STRINGS") print("-------------") #info() print(redis.info()) print("-------------") #monitor() print(redis.monitor()) print("-------------") #set() redis.set("name", "javier") redis.set("name", "jaime") print("key: ", redis.get("name")) print("-------------") print("all keys: ", redis.keys()) print("keys with a 'name...': ", redis.keys("name*")) print("keys with a 'e': ", redis.keys("*e*")) print("-------------") #setnx(name, value) redis.set("name", "javier") #mset(name, value) redis.mset({"name": "peter", "name": "david"}) print("name: ", redis.mget("name")) print("-------------") #getrange(name, start, end) - substrings of the value print("range : ", redis.getrange("name", 0, 3)) #delete all keys for key in redis.scan_iter("prefix:*"): redis.delete(key)
def get_metrics(redis, hostname): """ http://redis.io/commands/info server: General information about the Redis server clients: Client connections section memory: Memory consumption related information persistence: RDB and AOF related information stats: General statistics replication: Master/slave replication information cpu: CPU consumption statistics commandstats: Redis command statistics cluster: Redis Cluster section keyspace: Database related statistics """ data = {} data[hostname] = {} section_list = [ 'server', 'clients', 'memory', 'persistence', 'stats', 'replication', 'cpu', 'cluster', 'keyspace' ] for section in section_list: result = redis.info(section) data[hostname].update(get_data_from_dict(result, ("redis.%s[" % section))) return data
#!/usr/bin/env python # -*- coding: utf-8 -*- import redis print((redis.__file__)) # 连接,可选不同数据库 redis = redis.Redis(host='localhost', port=6379, db=0) info = redis.info() print(info) keys = redis.keys("*") print(keys) keys = redis.hkeys("keys") print(keys) print((len(keys))) key__value = redis.hget("key", 'hashkey') print(key__value)