153a592e
tangwang
redis统计脚本
|
1
2
|
#!/usr/bin/env python3
"""
|
7746376c
tangwang
日志统一用中文
|
3
|
Real-time monitoring of Redis cache eviction events
|
153a592e
tangwang
redis统计脚本
|
4
|
|
7746376c
tangwang
日志统一用中文
|
5
|
Continuously monitor evicted_keys statistics and warn when new evictions occur
|
153a592e
tangwang
redis统计脚本
|
6
7
8
9
10
11
12
13
14
|
"""
import redis
import time
import sys
from pathlib import Path
from datetime import datetime
# 添加项目路径
|
32e9b30c
tangwang
scripts/ 根目录主要保留启...
|
15
|
project_root = Path(__file__).resolve().parents[2]
|
153a592e
tangwang
redis统计脚本
|
16
17
18
19
20
|
sys.path.insert(0, str(project_root))
from config.env_config import REDIS_CONFIG
def get_redis_client():
|
7746376c
tangwang
日志统一用中文
|
21
|
"""Get Redis client"""
|
153a592e
tangwang
redis统计脚本
|
22
23
24
25
26
27
28
29
30
31
|
return redis.Redis(
host=REDIS_CONFIG.get('host', 'localhost'),
port=REDIS_CONFIG.get('port', 6479),
password=REDIS_CONFIG.get('password'),
decode_responses=True,
socket_timeout=5,
socket_connect_timeout=5,
)
def monitor_eviction(interval=5):
|
7746376c
tangwang
日志统一用中文
|
32
|
"""Continuously monitor eviction events"""
|
153a592e
tangwang
redis统计脚本
|
33
|
print("=" * 60)
|
7746376c
tangwang
日志统一用中文
|
34
|
print("Redis Cache Eviction Real-time Monitoring")
|
153a592e
tangwang
redis统计脚本
|
35
|
print("=" * 60)
|
7746376c
tangwang
日志统一用中文
|
36
37
|
print(f"Monitoring interval: {interval} seconds")
print("Press Ctrl+C to stop monitoring")
|
153a592e
tangwang
redis统计脚本
|
38
39
40
41
42
43
44
|
print("=" * 60)
print()
try:
client = get_redis_client()
client.ping()
except Exception as e:
|
7746376c
tangwang
日志统一用中文
|
45
|
print(f"❌ Redis connection failed: {e}")
|
153a592e
tangwang
redis统计脚本
|
46
47
48
49
50
51
52
53
54
55
56
57
|
return
last_evicted = 0
try:
while True:
info = client.info('stats')
current_evicted = info.get('evicted_keys', 0)
if current_evicted > last_evicted:
new_evictions = current_evicted - last_evicted
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
7746376c
tangwang
日志统一用中文
|
58
59
|
print(f"[{timestamp}] ⚠️ Detected {new_evictions} new eviction events!")
print(f" Total evictions: {current_evicted:,}")
|
153a592e
tangwang
redis统计脚本
|
60
61
62
63
64
65
66
|
# 检查内存使用情况
mem_info = client.info('memory')
maxmemory = mem_info.get('maxmemory', 0)
used_memory = mem_info.get('used_memory', 0)
if maxmemory > 0:
usage_percent = (used_memory / maxmemory) * 100
|
7746376c
tangwang
日志统一用中文
|
67
|
print(f" Current memory usage: {usage_percent:.2f}%")
|
153a592e
tangwang
redis统计脚本
|
68
69
70
71
|
last_evicted = current_evicted
else:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
7746376c
tangwang
日志统一用中文
|
72
|
print(f"[{timestamp}] ✅ No new eviction events (Total: {current_evicted:,})")
|
153a592e
tangwang
redis统计脚本
|
73
74
75
76
|
time.sleep(interval)
except KeyboardInterrupt:
|
7746376c
tangwang
日志统一用中文
|
77
|
print("\n\nMonitoring stopped")
|
153a592e
tangwang
redis统计脚本
|
78
|
except Exception as e:
|
7746376c
tangwang
日志统一用中文
|
79
|
print(f"\n❌ Monitoring error: {e}")
|
153a592e
tangwang
redis统计脚本
|
80
81
82
83
84
|
import traceback
traceback.print_exc()
if __name__ == "__main__":
import argparse
|
7746376c
tangwang
日志统一用中文
|
85
86
|
parser = argparse.ArgumentParser(description='Real-time monitoring of Redis cache eviction events')
parser.add_argument('--interval', type=int, default=5, help='Monitoring interval in seconds (default: 5)')
|
153a592e
tangwang
redis统计脚本
|
87
88
89
|
args = parser.parse_args()
monitor_eviction(interval=args.interval)
|