63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
import paramiko
|
|
import sys
|
|
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect('140.44.4.11', username='root', key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=10)
|
|
|
|
def run_sql(pod, sql):
|
|
cmd = f"kubectl exec -i -n monitoring {pod} -- mariadb --socket=/app/data/run/mariadb.sock -u root"
|
|
stdin, stdout, stderr = c.exec_command(cmd)
|
|
stdin.write(sql)
|
|
stdin.close()
|
|
out = stdout.read().decode().strip()
|
|
err = stderr.read().decode().strip()
|
|
return out, err
|
|
|
|
_, o, _ = c.exec_command("kubectl get pod -n monitoring -l app.kubernetes.io/name=uptime-kuma -o jsonpath='{.items[0].metadata.name}'")
|
|
pod = o.read().decode().strip()
|
|
print("Kuma Pod:", pod)
|
|
|
|
monitors = [
|
|
("Spacedock Harvester Node 01 (140.44.4.81)", "140.44.4.81"),
|
|
("Spacedock Harvester Node 02 (140.44.4.82)", "140.44.4.82"),
|
|
("Spacedock Harvester Node 03 (140.44.4.83)", "140.44.4.83"),
|
|
("Spacedock Harvester Node 04 (140.44.4.84)", "140.44.4.84"),
|
|
("Dell PowerEdge R730 Node 1 iDRAC (140.44.44.121)", "140.44.44.121"),
|
|
("Dell PowerEdge R730 Node 2 iDRAC (140.44.44.122)", "140.44.44.122"),
|
|
("Dell PowerEdge R730 Node 3 iDRAC (140.44.44.123)", "140.44.44.123"),
|
|
("Dell PowerEdge R730 Node 4 iDRAC (140.44.44.124)", "140.44.44.124")
|
|
]
|
|
|
|
for name, host in monitors:
|
|
out, err = run_sql(pod, f"USE kuma; SELECT id FROM monitor WHERE hostname='{host}' AND type='ping';")
|
|
if "id" in out and len(out.splitlines()) > 1:
|
|
mon_id = out.splitlines()[1].strip()
|
|
print(f"Monitor already exists: {name} (ID: {mon_id})")
|
|
else:
|
|
insert_sql = f"""USE kuma;
|
|
INSERT INTO monitor (name, active, user_id, `interval`, type, weight, hostname, maxretries, retry_interval)
|
|
VALUES ('{name}', 1, 1, 60, 'ping', 2000, '{host}', 1, 60);
|
|
SELECT LAST_INSERT_ID() AS id;
|
|
"""
|
|
out, err = run_sql(pod, insert_sql)
|
|
if err:
|
|
print(f"Error inserting {name}: {err}")
|
|
continue
|
|
lines = out.splitlines()
|
|
mon_id = lines[-1].strip()
|
|
print(f"Created monitor: {name} (ID: {mon_id})")
|
|
|
|
# Link notifications (1 = Discord, 2 = Gotify)
|
|
for notif_id in [1, 2]:
|
|
link_check = f"USE kuma; SELECT id FROM monitor_notification WHERE monitor_id={mon_id} AND notification_id={notif_id};"
|
|
out_link, _ = run_sql(pod, link_check)
|
|
if "id" not in out_link or len(out_link.splitlines()) <= 1:
|
|
run_sql(pod, f"USE kuma; INSERT INTO monitor_notification (monitor_id, notification_id) VALUES ({mon_id}, {notif_id});")
|
|
print(f" Linked monitor {mon_id} to notification {notif_id}")
|
|
else:
|
|
print(f" Monitor {mon_id} already linked to notification {notif_id}")
|
|
|
|
c.close()
|