32 lines
999 B
Python
32 lines
999 B
Python
"""Проверки состояния для Docker HEALTHCHECK: `healthcheck.py api` или `healthcheck.py collector`."""
|
|||
|
|
import datetime
|
||
|
|
import json
|
||
|
|
import sys
|
||
|
|
import urllib.request
|
||
|
|
|
||
|
|
import cidr_collector as cc
|
||
|
|
|
||
|
|
|
||
|
|
def check_api(url="http://127.0.0.1:8000/health"):
|
||
|
|
with urllib.request.urlopen(url, timeout=5) as response:
|
||
|
|
return response.status == 200
|
||
|
|
|
||
|
|
|
||
|
|
def check_collector():
|
||
|
|
"""Демон жив, пока heartbeat в status.json не старше STATUS_STALE_AFTER."""
|
||
|
|
with open(cc.STATUS_FILE) as f:
|
||
|
|
updated_at = datetime.datetime.fromisoformat(json.load(f)["updated_at"])
|
||
|
|
return (datetime.datetime.now() - updated_at).total_seconds() < cc.STATUS_STALE_AFTER
|
||
|
|
|
||
|
|
|
||
|
|
CHECKS = {"api": check_api, "collector": check_collector}
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
target = sys.argv[1] if len(sys.argv) > 1 else ""
|
||
|
|
try:
|
||
|
|
ok = CHECKS[target]()
|
||
|
|
except Exception as e:
|
||
|
|
print(f"{target} unhealthy: {e}")
|
||
|
|
ok = False
|
||
|
|
sys.exit(0 if ok else 1)
|