[P0] memory alert: MemAvailable/swap, Hermes every 5m
This commit is contained in:
1 parent
01dbf6792f
commit
d4bf156c43
1 file changed
+61
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Алерт по памяти: MemAvailable < 600MB или swap used > 8GB."""
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
ROOM = "!puEdcZCc87kKoCm3mX5bbjrTS-Skv2XCZg7JJBKOMw0"
|
||||
HS = "http://192.168.1.155:8008"
|
||||
STATE = "/var/lib/memory-alert.last"
|
||||
|
||||
|
||||
def mem():
|
||||
d = {}
|
||||
for line in open("/proc/meminfo"):
|
||||
k, v = line.split(":", 1)
|
||||
d[k] = int(v.strip().split()[0])
|
||||
return d
|
||||
|
||||
|
||||
def send(text):
|
||||
tok = open("/root/.matrix-admin-token").read().strip()
|
||||
txn = "mem-%d" % int(time.time() * 1000)
|
||||
body = json.dumps({"msgtype": "m.text", "body": text}).encode()
|
||||
url = "%s/_matrix/client/v3/rooms/%s/send/m.room.message/%s?access_token=%s" % (HS, ROOM, txn, tok)
|
||||
r = urllib.request.Request(url, data=body, headers={"Content-Type": "application/json"}, method="PUT")
|
||||
with urllib.request.urlopen(r, timeout=20) as resp:
|
||||
return resp.status
|
||||
|
||||
|
||||
def main():
|
||||
m = mem()
|
||||
avail_mb = m.get("MemAvailable", 0) // 1024
|
||||
swap_used_gb = (m.get("SwapTotal", 0) - m.get("SwapFree", 0)) / 1024 / 1024
|
||||
bad = avail_mb < 600 or swap_used_gb > 8
|
||||
if not bad:
|
||||
print("ok avail=%dMB swap=%.1fGB" % (avail_mb, swap_used_gb))
|
||||
return 0
|
||||
now = time.time()
|
||||
try:
|
||||
last = float(open(STATE).read().strip())
|
||||
except Exception:
|
||||
last = 0
|
||||
if now - last < 1800:
|
||||
print("alert cooldown")
|
||||
return 0
|
||||
open(STATE, "w").write(str(int(now)))
|
||||
text = ("⚠️ **Память хоста**\n"
|
||||
"• MemAvailable: **%d МБ**\n"
|
||||
"• Swap used: **%.1f ГБ**\n"
|
||||
"• top CT: 103 (Jellyfin/netbox/docmost), 115 (OpenClaw)\n"
|
||||
"Проверить: `systemd-cgtop -b -n1 --order=memory`" % (avail_mb, swap_used_gb))
|
||||
try:
|
||||
print("sent", send(text))
|
||||
except Exception as e:
|
||||
print("send failed", e)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user