python3.10監控redis例項

z597011036發表於2023-03-02

       使用python3.10監控redis單例項,例項可用,連線數,埠.

import sys
try:
    import redis
except Exception as e:
    print('pip install redis')   #安裝redis模組
    sys.exit()
class Redis:
    def __init__(self,host,port,password=None):
        self.host = host
        self.port = port
        self.password = password
        if self.password:
            self.rds = redis.StrictRedis(host=host,port=port,password=self.password)
        else:
            self.rds = redis.StrictRedis(host=host, port=port)
        try:
            self.info = self.rds.info()
        except Exception as e:
            self.info = None
    #返回redis版本
    def redis_version(self):
        try:
            return "Redis版本:%s" % self.info['redis_version']
        except Exception as e:
            return e
    def redis_ping(self):
        if self.rds.ping():
            return "Redis例項可用"
        else:
            return "Redis例項不可用"
            sys.exit()
    def redis_connections(self):
        try:
            return "Redis連線數:%s" % self.info['connected_clients']
        except Exception as e:
            return e
    def redis_port(self):
        try:
            return "Redis埠:%s" % self.info['tcp_port']
        except Exception as e:
            return e
    def blocked_clients(self):
        try:
            return "Redis正在等待阻塞客房端的數量:%s" % self.info['blocked_clients']
        except Exception as e:
            return e
    def redis_used_memory(self):
        memory = self.info['used_memory']
        if memory:
            return "Redis記憶體使用:%.2fM" % (memory/1024)
    def redis_memory_usage(self):
        used_memory = self.info['used_memory']
        max_memory = self.info['maxmemory']
        system_memory = self.info['total_system_memory']
        rate = 0
        if max_memory:
            rate = float(used_memory) / float(max_memory)
        else:
            if system_memory:
                rate = float(used_memory) / float(system_memory)
        return "%.2f" % (rate * 100)
    def rejected_connections(self):
        try:
            return "Redis拒絕連線數:%s" % self.info['rejected_connections']
        except Exception as e:
            return e
    def ops(self):
        try:
            return "Redis OPS值:%s" % self.info['instantaneous_ops_per_sec']
        except Exception as e:
            return e
    def redis_expired(self):
        try:
            return "Redis命中率:%s" % self.info['expired_keys']
        except Exception as e:
            return e
    def check(self):
        print(self.redis_version())
        print(self.redis_ping())
        print(self.redis_connections())
        print(self.redis_port())
        print(self.blocked_clients())
        print(self.redis_used_memory())
        print(self.redis_memory_usage())
        print(self.rejected_connections())
        print(self.ops())
        print(self.redis_expired())
if __name__ == '__main__':
    a=Redis('127.0.0.1','6379','System135')
    a.check()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25854343/viewspace-2937940/,如需轉載,請註明出處,否則將追究法律責任。

相關文章