优化性能

This commit is contained in:
liuwei
2026-02-24 17:33:59 +08:00
parent b2cb1a9de7
commit 9d02b264e2

View File

@@ -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,34 +108,35 @@ 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)
response = requests.get(f"{self.api_url}/diskio") if self._loop_index % 6 == 0:
response.raise_for_status() response = requests.get(f"{self.api_url}/diskio")
disks = response.json() response.raise_for_status()
max_io_usage = 0 disks = response.json()
for disk in disks: max_io_usage = 0
read_bytes = disk.get('read_bytes', 0) for disk in disks:
write_bytes = disk.get('write_bytes', 0) read_bytes = disk.get('read_bytes', 0)
io_usage = (read_bytes + write_bytes) / (2048 * 1024) write_bytes = disk.get('write_bytes', 0)
max_io_usage = max(max_io_usage, io_usage) io_usage = (read_bytes + write_bytes) / (2048 * 1024)
if max_io_usage > self.io_threshold: max_io_usage = max(max_io_usage, io_usage)
self.send_alert_email("磁盘 I/OMB/s", max_io_usage, self.io_threshold) if max_io_usage > self.io_threshold:
self.send_alert_email("磁盘 I/OMB/s", max_io_usage, self.io_threshold)
response = requests.get(f"{self.api_url}/fs") response = requests.get(f"{self.api_url}/fs")
response.raise_for_status() response.raise_for_status()
filesystems = response.json() filesystems = response.json()
for fs in filesystems: for fs in filesystems:
disk_usage = fs.get('percent', 0) disk_usage = fs.get('percent', 0)
if disk_usage > self.disk_usage_threshold: if disk_usage > self.disk_usage_threshold:
self.send_alert_email(f"磁盘占用 ({fs.get('mnt_point')})", disk_usage, self.send_alert_email(f"磁盘占用 ({fs.get('mnt_point')})", disk_usage,
self.disk_usage_threshold) self.disk_usage_threshold)
response = requests.get(f"{self.api_url}/processcount") response = requests.get(f"{self.api_url}/processcount")
response.raise_for_status() response.raise_for_status()
handle_count = response.json().get('total', 0) handle_count = response.json().get('total', 0)
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)