feat: template-based message display, remove all emoji
This commit is contained in:
1 parent
570eb0df70
commit
2e077eab33
11 files changed
+126
-68
No files matched your search
+15
-13
@@ -4,6 +4,7 @@ import logging
|
||||
import time
|
||||
|
||||
import monitor
|
||||
import templates
|
||||
|
||||
log = logging.getLogger("alerter")
|
||||
|
||||
@@ -60,8 +61,7 @@ class Alerter:
|
||||
self._consecutive = 0
|
||||
if self._alerting:
|
||||
self._alerting = False
|
||||
msg = (f"✅ [恢复] 服务器 CPU 已回落到 {cpu_percent:.1f}%,"
|
||||
f"负载恢复正常。")
|
||||
msg = templates.render("recovered", cpu=f"{cpu_percent:.1f}")
|
||||
log.info(msg)
|
||||
await self.ob.broadcast(msg, self.groups, self.friends)
|
||||
|
||||
@@ -69,16 +69,18 @@ class Alerter:
|
||||
top = monitor.get_top_processes(self.top_n)
|
||||
cpu = monitor.get_cpu()
|
||||
load = cpu["load"]
|
||||
lines = [
|
||||
f"⚠️ [高负载告警] CPU 持续超过 {self.threshold:.0f}%,当前 {cpu_percent:.1f}%",
|
||||
f"负载: {load[0]:.2f} / {load[1]:.2f} / {load[2]:.2f} 核心: {cpu['cores']}",
|
||||
"",
|
||||
f"TOP{self.top_n} 高负载进程:",
|
||||
]
|
||||
for i, p in enumerate(top, 1):
|
||||
lines.append(
|
||||
f"{i}. {p['name']} (PID {p['pid']}) CPU {p['cpu']}% 内存 {p['mem_mb']}MB"
|
||||
)
|
||||
msg = "\n".join(lines)
|
||||
top_list = "\n".join(
|
||||
f"{i}. {p['name']} (PID {p['pid']}) CPU {p['cpu']}% 内存 {p['mem_mb']}MB"
|
||||
for i, p in enumerate(top, 1)
|
||||
)
|
||||
msg = templates.render(
|
||||
"alert",
|
||||
threshold=f"{self.threshold:.0f}",
|
||||
cpu=f"{cpu_percent:.1f}",
|
||||
cores=cpu["cores"],
|
||||
load=f"{load[0]:.2f} / {load[1]:.2f} / {load[2]:.2f}",
|
||||
top_n=self.top_n,
|
||||
top_list=top_list or "无数据",
|
||||
)
|
||||
log.warning("触发高负载告警: %.1f%%", cpu_percent)
|
||||
await self.ob.broadcast(msg, self.groups, self.friends)
|
||||
Reference in new issue
Block a user