脚本 1
slots.sh
#!/bin/bash
# Connect to Redis Cluster and retrieve node information
nodes_info= echo "CLUSTER NODES" | rd 10.XX.33.202 6011
# Check if redis-cli command executed successfully
if [ $? -ne 0 ]; then
echo "Error: Unable to connect to Redis Cluster or retrieve node information."
exit 1
fi
# Loop through each line of node information
while IFS= read -r line; do
# Extract the slot information from the line
slots=$(echo "$line" | awk '{print $9}')
# Check if the line contains slot information (exclude empty lines)
if [ -n "$slots" ]; then
# Count the number of slots for each node
node_slots=$(echo "$slots" | tr -cd , | wc -c)
echo "$line" | awk -v slots="$node_slots" '{print $2 " has " slots " slots."}'
fi
done <<< "$nodes_info"
get_slots.py
import sys
def calculate_slot_count_from_file(file_path):
print(file_path)
try:
with open(file_path, 'r') as file:
cluster_info = file.read()
for line in cluster_info.splitlines():
if "master" not in line:
continue
node = line.split(" ")[1].split("@")[0]
slot_info = line.split("connected")[1]
slot_count = 0
for num in slot_info.split(" "):
if len(num.strip()) == 0:
continue
count = int(num.split('-')[1]) - int(num.split('-')[0]) + 1
slot_count += count
print("Node:", node, "Slot count:", slot_count)
except FileNotFoundError:
print("Error: File not found.")
# Check if the correct number of arguments is provided
if len(sys.argv) != 2:
print("Usage: python get_slots_info.py <file_path>")
sys.exit(1)
file_path = sys.argv[1]
calculate_slot_count_from_file(file_path)