import redis
redis_hosts = [{"域名或ip1", "port": "端口号1"},
{"域名或ip2", "port": "端口号2"},
]
all_used_port=[端口号1,端口号2]
def bytes_to_gb(bytes_value):
return bytes_value / (1024 ** 3)
def parse_nodes_info(nodes_info):
instances = []
for address, node_data in nodes_info.items():
if node_data['connected']:
host, port_info = address.split(":")
port = port_info.split("@")[0]
instances.append((host, int(port)))
return instances
def get_memory_info_and_total(instances):
total_system_memory_bytes = 0
total_used_memory_bytes = 0
all_port={}
for host, port in instances:
try:
r = redis.StrictRedis(host=host, port=port)
info = r.info("memory")
instance_system_memory_bytes = info.get('total_system_memory', 0)
instance_used_memory_bytes = info.get('used_memory', 0)
if port not in all_port and port in all_used_port:
total_system_memory_bytes += instance_system_memory_bytes
all_port[port]=1
total_used_memory_bytes += instance_used_memory_bytes
instance_system_memory_gb = bytes_to_gb(instance_system_memory_bytes)
instance_used_memory_gb = bytes_to_gb(instance_used_memory_bytes)
except Exception as e:
print(f"Could not connect to instance {host}:{port}")
print(f"Error: {e}")
print("-" * 40)
total_system_memory_gb = bytes_to_gb(total_system_memory_bytes)/1.7
total_used_memory_gb = bytes_to_gb(total_used_memory_bytes)/2
print("Total Memory Information Across All Instances")
print(f"Total System Memory: {total_system_memory_gb:.2f} GB")
print(f"Total Used Memory: {total_used_memory_gb:.2f} GB")
redis_hosts=redis_hosts[:1]
for cluster in redis_hosts:
host = cluster["host"]
port = cluster["port"]
try:
r = redis.StrictRedis(host=host, port=port)
nodes_info = r.cluster("NODES")
instances = parse_nodes_info(nodes_info)
print("",len(instances))
get_memory_info_and_total(instances)
except Exception as e:
print(f"Could not connect to cluster node {host}:{port}")
print(f"Error: {e}")
print("-" * 40)