"""Настраивает Kuma: уведомления в Matrix через hub-core.""" import json import subprocess CT = "107" DB = "/opt/network-lxc/uptime/data/kuma.db" cfg = { "type": "webhook", "webhookURL": "http://192.168.1.10:9120/notify", "webhookContentType": "json", "webhookJSONBody": '{"title": "{{ name }}: {{ status }}", "message": "{{ msg }}"}', "webhookAdditionalHeaders": "", } body = json.dumps(cfg, ensure_ascii=False) def sh(c, timeout=60): r = subprocess.run(["pct", "exec", CT, "--", "bash", "-c", c], capture_output=True, text=True, timeout=timeout) return (r.stdout or r.stderr or "").strip() sql = f""" insert into notification (name, active, user_id, is_default, config) values ('Matrix (hub)', 1, 1, 0, '{body.replace("'", "''")}'); insert into monitor_notification (id, monitor_id, notification_id) select (select coalesce(max(id),0) from monitor_notification) + row_number() over (), m.id, (select id from notification where name='Matrix (hub)') from monitor m where not exists ( select 1 from monitor_notification mn where mn.monitor_id = m.id and mn.notification_id = (select id from notification where name='Matrix (hub)') ); """ open("/tmp/kuma_notify.sql", "w").write(sql) subprocess.run(["pct", "push", CT, "/tmp/kuma_notify.sql", "/tmp/kuma_notify.sql", "--perms", "600"], capture_output=True, text=True) print("insert:", sh(f"sqlite3 {DB} < /tmp/kuma_notify.sql")) print("уведомления:", sh(f"sqlite3 {DB} 'select id,name,active from notification;'")) print("связок:", sh(f"sqlite3 {DB} 'select count(*) from monitor_notification;'"))