优化性能
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,34 +108,35 @@ class GlancesMonitor:
|
||||
if load_avg > self.load_threshold:
|
||||
self.send_alert_email("系统负载(1分钟)", load_avg, self.load_threshold)
|
||||
|
||||
response = requests.get(f"{self.api_url}/diskio")
|
||||
response.raise_for_status()
|
||||
disks = response.json()
|
||||
max_io_usage = 0
|
||||
for disk in disks:
|
||||
read_bytes = disk.get('read_bytes', 0)
|
||||
write_bytes = disk.get('write_bytes', 0)
|
||||
io_usage = (read_bytes + write_bytes) / (2048 * 1024)
|
||||
max_io_usage = max(max_io_usage, io_usage)
|
||||
if max_io_usage > self.io_threshold:
|
||||
self.send_alert_email("磁盘 I/O(MB/s)", max_io_usage, self.io_threshold)
|
||||
if self._loop_index % 6 == 0:
|
||||
response = requests.get(f"{self.api_url}/diskio")
|
||||
response.raise_for_status()
|
||||
disks = response.json()
|
||||
max_io_usage = 0
|
||||
for disk in disks:
|
||||
read_bytes = disk.get('read_bytes', 0)
|
||||
write_bytes = disk.get('write_bytes', 0)
|
||||
io_usage = (read_bytes + write_bytes) / (2048 * 1024)
|
||||
max_io_usage = max(max_io_usage, io_usage)
|
||||
if max_io_usage > self.io_threshold:
|
||||
self.send_alert_email("磁盘 I/O(MB/s)", max_io_usage, self.io_threshold)
|
||||
|
||||
response = requests.get(f"{self.api_url}/fs")
|
||||
response.raise_for_status()
|
||||
filesystems = response.json()
|
||||
for fs in filesystems:
|
||||
disk_usage = fs.get('percent', 0)
|
||||
if disk_usage > self.disk_usage_threshold:
|
||||
self.send_alert_email(f"磁盘占用 ({fs.get('mnt_point')})", disk_usage,
|
||||
self.disk_usage_threshold)
|
||||
response = requests.get(f"{self.api_url}/fs")
|
||||
response.raise_for_status()
|
||||
filesystems = response.json()
|
||||
for fs in filesystems:
|
||||
disk_usage = fs.get('percent', 0)
|
||||
if disk_usage > self.disk_usage_threshold:
|
||||
self.send_alert_email(f"磁盘占用 ({fs.get('mnt_point')})", disk_usage,
|
||||
self.disk_usage_threshold)
|
||||
|
||||
response = requests.get(f"{self.api_url}/processcount")
|
||||
response.raise_for_status()
|
||||
handle_count = response.json().get('total', 0)
|
||||
if handle_count > self.handle_threshold:
|
||||
self.send_alert_email("句柄数", handle_count, self.handle_threshold)
|
||||
response = requests.get(f"{self.api_url}/processcount")
|
||||
response.raise_for_status()
|
||||
handle_count = response.json().get('total', 0)
|
||||
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