forked from longtao.wu/droboticflow
65 lines
2.8 KiB
Python
65 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only snapshot metadata check. No login, refresh or retry loop.
|
|
Exit 0 healthy/idle, 1 warning, 2 unavailable, 3 unknown/check failed.
|
|
Use --cookie-jar with an existing Netscape-format session cookie file.
|
|
"""
|
|
import argparse
|
|
import http.cookiejar
|
|
import json
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
|
|
def classify(body):
|
|
if not isinstance(body, dict):
|
|
return 3
|
|
status = body.get("status")
|
|
if status == "healthy" and body.get("available") is True:
|
|
return 0
|
|
if status == "idle" and body.get("available") is False:
|
|
return 0 # demand-triggered refresh: inactivity is not an incident
|
|
if status == "degraded":
|
|
return 1
|
|
if status == "unavailable":
|
|
return 2
|
|
return 3 # missing schema, not built, building, HTML/login, unknown version
|
|
|
|
|
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
return None
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--url", required=True, help="Platform origin, without credentials/query")
|
|
parser.add_argument("--cookie-jar", required=True)
|
|
args = parser.parse_args()
|
|
parsed = urllib.parse.urlsplit(args.url)
|
|
if (parsed.scheme not in ("http", "https") or not parsed.hostname or parsed.username
|
|
or parsed.password or parsed.query or parsed.fragment or parsed.path not in ("", "/")):
|
|
parser.error("--url must be an HTTP(S) origin without credentials, query or path")
|
|
try:
|
|
jar = http.cookiejar.MozillaCookieJar(args.cookie_jar)
|
|
jar.load(ignore_discard=True)
|
|
opener = urllib.request.build_opener(NoRedirect(), urllib.request.HTTPCookieProcessor(jar))
|
|
request = urllib.request.Request(args.url.rstrip("/") + "/api/dashboard/annotation-output/health")
|
|
with opener.open(request, timeout=5) as response:
|
|
body = json.loads(response.read(65537))
|
|
code = classify(body)
|
|
# Allowlist metadata; never print response errors or authentication data.
|
|
result = {k: body[k] for k in ("status", "available", "refreshing", "snapshotAgeSeconds",
|
|
"consecutiveFailures", "warnings", "metrics", "lastFailure") if k in body} if isinstance(body, dict) else {}
|
|
print(json.dumps({"exitCode": code, "snapshotMetadata": result}, ensure_ascii=False))
|
|
return code
|
|
except urllib.error.HTTPError as exc:
|
|
print(json.dumps({"exitCode": 3, "error": "health_http_error", "httpStatus": exc.code}))
|
|
except (OSError, ValueError, http.cookiejar.LoadError, urllib.error.URLError):
|
|
print(json.dumps({"exitCode": 3, "error": "health_check_failed"}))
|
|
return 3
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|