优化性能
This commit is contained in:
@@ -10,7 +10,7 @@ class GlancesMonitor:
|
||||
def __init__(self, email_sender, host='192.168.2.170', port=61208,
|
||||
cpu_threshold=80.0, load_threshold=None, io_threshold=80.0,
|
||||
disk_usage_threshold=80.0, handle_threshold=20000,
|
||||
recipient=None):
|
||||
monitor_interval=30, recipient=None):
|
||||
"""初始化 Glances 监控组件
|
||||
|
||||
Args:
|
||||
@@ -37,6 +37,8 @@ class GlancesMonitor:
|
||||
self.glances_process = None
|
||||
self.last_alert_times = {}
|
||||
self._running = False
|
||||
self.monitor_interval = monitor_interval
|
||||
self._loop_index = 0
|
||||
|
||||
def get_cpu_count(self):
|
||||
"""获取 CPU 核心数"""
|
||||
@@ -92,6 +94,8 @@ class GlancesMonitor:
|
||||
"""监控服务器指标并触发告警"""
|
||||
while self._running:
|
||||
try:
|
||||
self._loop_index += 1
|
||||
|
||||
response = requests.get(f"{self.api_url}/cpu/total")
|
||||
response.raise_for_status()
|
||||
cpu_usage = response.json().get('total', 0)
|
||||
@@ -104,6 +108,7 @@ class GlancesMonitor:
|
||||
if load_avg > self.load_threshold:
|
||||
self.send_alert_email("系统负载(1分钟)", load_avg, self.load_threshold)
|
||||
|
||||
if self._loop_index % 6 == 0:
|
||||
response = requests.get(f"{self.api_url}/diskio")
|
||||
response.raise_for_status()
|
||||
disks = response.json()
|
||||
@@ -131,7 +136,7 @@ class GlancesMonitor:
|
||||
if handle_count > self.handle_threshold:
|
||||
self.send_alert_email("句柄数", handle_count, self.handle_threshold)
|
||||
|
||||
time.sleep(10)
|
||||
time.sleep(self.monitor_interval)
|
||||
except requests.RequestException as e:
|
||||
logger.error(f"连接 Glances API 失败: {e}")
|
||||
time.sleep(60)
|
||||
|
||||
Reference in New Issue
Block a user