#!/usr/bin/env python3 """healthcheck.py — проверка пула chat.co и шлюза. python3 healthcheck.py # health локального шлюза python3 healthcheck.py --pool pool.jsonl # сколько живых аккаунтов в пуле python3 healthcheck.py --test claude-opus-5-5 # тестовый чат через шлюз """ import argparse, json, os, sys, time, urllib.request, urllib.error def get(url, timeout=30): with urllib.request.urlopen(url, timeout=timeout) as r: return json.loads(r.read().decode()) def post(url, body, timeout=180): req = urllib.request.Request(url, data=json.dumps(body).encode(), headers={"content-type": "application/json"}) try: with urllib.request.urlopen(req, timeout=timeout) as r: return r.status, json.loads(r.read().decode()) except urllib.error.HTTPError as e: return e.code, e.read().decode()[:300] def main(): ap = argparse.ArgumentParser() ap.add_argument("--base", default="http://127.0.0.1:8833") ap.add_argument("--pool", default=os.environ.get("CHATCO_POOL", "pool.jsonl")) ap.add_argument("--test", default="") a = ap.parse_args() if os.path.exists(a.pool): ok = bad = 0 for line in open(a.pool, encoding="utf-8"): line = line.strip() if not line: continue try: r = json.loads(line) except Exception: continue ok += 1 if r.get("ok") else 0 bad += 0 if r.get("ok") else 1 print("pool: %s | ok=%d fail=%d" % (a.pool, ok, bad)) else: print("pool: %s не найден" % a.pool) try: h = get(a.base + "/health") print("health: accounts=%s models=%s" % (h.get("accounts"), h.get("models"))) credits = h.get("credits") or {} if credits: top = sorted(credits.items(), key=lambda kv: -float(kv[1]))[:3] print("top credits:", ", ".join("%s=%.0f" % (k.split("@")[0], float(v)) for k, v in top)) except Exception as e: print("health: FAIL %s" % e) return 1 if a.test: t0 = time.time() code, body = post(a.base + "/v1/chat/completions", {"model": a.test, "max_tokens": 30, "messages": [{"role": "user", "content": "Say OK"}]}) if code == 200 and isinstance(body, dict): txt = (body.get("choices") or [{}])[0].get("message", {}).get("content", "") print("test %-18s 200 %.1fs | %s" % (a.test, time.time() - t0, " ".join(txt.split())[:60])) else: print("test %-18s %s %s" % (a.test, code, str(body)[:160])) return 1 return 0 if __name__ == "__main__": sys.exit(main())