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