This commit is contained in:
Mark Gregory
2026-10-21 14:54:07 +01:00
parent 8c7b29d50f
commit 578f0218a5
6 changed files with 79 additions and 8 deletions
+10 -1
View File
@@ -2,6 +2,15 @@
"""Read-only JSON adapter for an existing pegasus-health --verbose command."""
import datetime
import json
def system_uptime_seconds():
"""Read Linux kernel uptime without privilege or external commands."""
try:
with open('/proc/uptime', encoding='ascii') as handle:
return max(0, int(float(handle.read().split()[0])))
except (OSError, ValueError, IndexError):
return None
import re
import subprocess
import sys
@@ -56,7 +65,7 @@ def output():
if not checks:
counts['fail'] = max(1, counts['fail'])
overall = 'fail' if counts['fail'] or any(g['status']=='fail' for g in groups) else 'warn' if counts['warn'] or any(g['status']=='warn' for g in groups) else 'ok'
print(json.dumps({'schema':1, 'host':'pegasus', 'generatedAt':datetime.datetime.now(datetime.timezone.utc).isoformat(), 'overall':overall, 'counts':counts,'groups':groups,'checks':checks, 'diagnostics':plain[-24000:], 'commandExitCode':p.returncode}, ensure_ascii=False))
print(json.dumps({'schema':1, 'host':'pegasus', 'uptimeSeconds':system_uptime_seconds(), 'generatedAt':datetime.datetime.now(datetime.timezone.utc).isoformat(), 'overall':overall, 'counts':counts,'groups':groups,'checks':checks, 'diagnostics':plain[-24000:], 'commandExitCode':p.returncode}, ensure_ascii=False))
return {'ok':0,'warn':1,'fail':2}[overall]
if __name__ == '__main__':
+11 -1
View File
@@ -5,6 +5,15 @@ No third-party Python packages required.
"""
import datetime
import json
def system_uptime_seconds():
"""Read Linux kernel uptime without privilege or external commands."""
try:
with open('/proc/uptime', encoding='ascii') as handle:
return max(0, int(float(handle.read().split()[0])))
except (OSError, ValueError, IndexError):
return None
import os
import shutil
import socket
@@ -115,4 +124,5 @@ for section in dict.fromkeys(c['section'] for c in checks):
counts = {s: sum(c['status'] == s for c in checks) for s in ('ok', 'warn', 'fail')}
overall = 'fail' if counts['fail'] else 'warn' if counts['warn'] else 'ok'
print(json.dumps(dict(schema=1, host=host, generatedAt=datetime.datetime.now(datetime.timezone.utc).isoformat(),
overall=overall, counts=counts, groups=groups, checks=checks, diagnostics=None, services=services)))
overall=overall, counts=counts, groups=groups, checks=checks, diagnostics=None, services=services,
uptimeSeconds=system_uptime_seconds())))