Files
buryan-infra/ops/scripts/check_ollama.py
T

67 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Сторож Ollama: ловит «сервер жив, но генерация висит».
Симптом: /api/tags отвечает быстро, а /api/generate уходит в таймаут —
runner завис. Именно так ломался RAG и LLM-сбор. Раньше это замечали
только по жалобам; теперь будет событие в шине.
"""
import json
import sys
import urllib.request as u
OLLAMA = "http://192.168.1.229:11434"
HUB = "http://127.0.0.1:9120"
GEN_TIMEOUT = 20
def get(path, timeout=8):
with u.urlopen(OLLAMA + path, timeout=timeout) as r:
return json.loads(r.read())
def post(path, payload, timeout):
body = json.dumps(payload).encode()
req = u.Request(OLLAMA + path, data=body, headers={"Content-Type": "application/json"})
with u.urlopen(req, timeout=timeout) as r:
return json.loads(r.read())
def notify(text, priority="high"):
# шлюз принимает title/message (поле text он игнорирует → «empty»)
body = json.dumps({"title": "Ollama", "message": text,
"priority": priority}).encode()
req = u.Request(HUB + "/notify", data=body,
headers={"Content-Type": "application/json"})
try:
with u.urlopen(req, timeout=10) as r:
return r.status
except Exception:
return 0
def main():
try:
tags = get("/api/tags")
except Exception as e:
print("Ollama недоступна:", str(e)[:80])
notify("🔴 Ollama (192.168.1.229) не отвечает: %s" % str(e)[:60])
return 1
models = [m.get("name") for m in (tags.get("models") or [])]
try:
post("/api/generate", {"model": "qwen2.5:3b", "prompt": "1",
"stream": False, "options": {"num_predict": 5}},
GEN_TIMEOUT)
print("Ollama в порядке, моделей:", len(models))
return 0
except Exception as e:
print("Ollama зависла на генерации:", str(e)[:80])
notify("🔴 Ollama (192.168.1.229): сервер жив, но генерация висит "
"(таймаут %ss). Нужен перезапуск на 229 — от этого зависит "
"RAG и LLM-сбор." % GEN_TIMEOUT)
return 1
if __name__ == "__main__":
sys.exit(main())