| 1 | #!/usr/bin/env python3 |
| 2 | """Pushes each pm2 app's state to vmagent as Snow Globe service metrics.""" |
| 3 | import json |
| 4 | import subprocess |
| 5 | import time |
| 6 | from urllib.request import Request, urlopen |
| 7 | |
| 8 | while True: |
| 9 | apps = json.loads(subprocess.run(["pm2", "jlist"], capture_output=True, check=True, text=True).stdout) |
| 10 | lines = [] |
| 11 | for app in apps: |
| 12 | env, monit = app["pm2_env"], app.get("monit") or {} |
| 13 | labels = "{service=%s}" % json.dumps("vps." + app["name"]) |
| 14 | lines += [ |
| 15 | f"snowglobe_service_up{labels} {int(env['status'] == 'online')}", |
| 16 | f"snowglobe_service_cpu_cores{labels} {monit.get('cpu', 0) / 100}", |
| 17 | f"snowglobe_service_memory_bytes{labels} {monit.get('memory', 0)}", |
| 18 | f"snowglobe_service_restarts_total{labels} {env.get('restart_time', 0)}", |
| 19 | f"snowglobe_service_started_seconds{labels} {env.get('pm_uptime', 0) / 1000}", |
| 20 | ] |
| 21 | urlopen(Request("http://127.0.0.1:8429/api/v1/import/prometheus", data="\n".join(lines).encode() + b"\n"), timeout=10).read() |
| 22 | time.sleep(15) |