fix(health-agent): fix uk_tokens.yml load race and LogRecord msg conflict

- config.py: Replace exists()+open() with try/except open() to avoid TOCTOU race on SSHFS mounts where stat can succeed but open can fail with FileNotFoundError.
- uptime_kuma.py: Rename msg key to push_msg in logger extra dicts. Python LogRecord reserves the msg field; passing it in extra raises ValueError which was being silently swallowed by the except block, masking successful pushes as errors.
This commit is contained in:
Murat ÖZDEMİR 2026-06-26 20:37:42 +03:00
parent 8d5fe55b14
commit d51c073556
2 changed files with 6 additions and 6 deletions

View File

@ -16,10 +16,10 @@ EXTERNAL_DOMAIN = os.getenv("EXTERNAL_DOMAIN", "iklim.co")
EXTERNAL_SUBDOMAIN_SUFFIX = os.getenv("EXTERNAL_SUBDOMAIN_SUFFIX", "")
def load_uk_tokens():
token_file = Path("config/generated/uk_tokens.yml")
if not token_file.exists():
try:
with open("config/generated/uk_tokens.yml", "r") as f:
return yaml.safe_load(f) or {}
except (FileNotFoundError, OSError):
return {}
with open(token_file, "r") as f:
return yaml.safe_load(f) or {}
UK_TOKENS = load_uk_tokens()

View File

@ -15,7 +15,7 @@ def push(monitor_name: str, status: str, msg: str, ping_ms: int):
return
if DRY_RUN:
logger.info(f"[DRY-RUN] Would push {monitor_name} status={status} msg={msg} ping={ping_ms}ms", extra={"check": monitor_name, "status": status, "msg": msg, "ping_ms": ping_ms, "source": "uptime_kuma"})
logger.info(f"[DRY-RUN] Would push {monitor_name} status={status} msg={msg} ping={ping_ms}ms", extra={"check": monitor_name, "status": status, "push_msg": msg, "ping_ms": ping_ms, "source": "uptime_kuma"})
return
url = f"{UK_PUSH_URL_BASE}/{token}"
@ -28,6 +28,6 @@ def push(monitor_name: str, status: str, msg: str, ping_ms: int):
try:
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
logger.info(f"Pushed {monitor_name} status={status}", extra={"check": monitor_name, "status": status, "msg": msg, "ping_ms": ping_ms, "source": "uptime_kuma"})
logger.info(f"Pushed {monitor_name} status={status}", extra={"check": monitor_name, "status": status, "push_msg": msg, "ping_ms": ping_ms, "source": "uptime_kuma"})
except Exception as e:
logger.error(f"Failed to push {monitor_name}: {e}", extra={"check": monitor_name, "status": "push_failed", "error": str(e), "source": "uptime_kuma"})