feat(health-agent): add --once and --dry-run flags to main.py

This commit is contained in:
Murat ÖZDEMİR 2026-06-26 16:43:21 +03:00
parent c49616ac10
commit 7ab186b961
2 changed files with 30 additions and 7 deletions

View File

@ -1,5 +1,7 @@
import argparse
import time import time
import logging import logging
import json
from health_agent.checks import swarm from health_agent.checks import swarm
from health_agent.checks.http import run_all_http_checks from health_agent.checks.http import run_all_http_checks
from health_agent.checks.tcp import check_etcd_cluster from health_agent.checks.tcp import check_etcd_cluster
@ -8,7 +10,7 @@ from health_agent.checks.redis_sentinel import check_redis_sentinel
from health_agent.checks.mongodb import check_mongodb from health_agent.checks.mongodb import check_mongodb
from health_agent.checks.filesystem import check_storagebox_mount from health_agent.checks.filesystem import check_storagebox_mount
from health_agent.events.docker_events import start_docker_event_listener from health_agent.events.docker_events import start_docker_event_listener
import json import health_agent.uptime_kuma as uk
class JSONFormatter(logging.Formatter): class JSONFormatter(logging.Formatter):
def format(self, record): def format(self, record):
@ -68,8 +70,23 @@ def run_checks():
logger.error(f"Error running filesystem checks: {e}") logger.error(f"Error running filesystem checks: {e}")
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="iklim.co Health Agent")
parser.add_argument("--once", action="store_true", help="Run checks once and exit")
parser.add_argument("--dry-run", action="store_true", help="Run checks but skip Uptime Kuma push")
args = parser.parse_args()
if args.dry_run:
uk.DRY_RUN = True
logger.info("Dry-run mode enabled — Uptime Kuma pushes will be skipped")
logger.info("Starting health-agent...") logger.info("Starting health-agent...")
start_docker_event_listener()
while True: if not args.dry_run:
start_docker_event_listener()
if args.once:
run_checks() run_checks()
time.sleep(60) else:
while True:
run_checks()
time.sleep(60)

View File

@ -4,21 +4,27 @@ import logging
from health_agent.config import UK_TOKENS from health_agent.config import UK_TOKENS
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
UK_PUSH_URL_BASE = os.getenv("UK_PUSH_URL_BASE", "https://status.iklim.co/api/push") UK_PUSH_URL_BASE = os.getenv("UK_PUSH_URL_BASE", "https://uptime.tarla.io/api/push")
DRY_RUN = False
def push(monitor_name: str, status: str, msg: str, ping_ms: int): def push(monitor_name: str, status: str, msg: str, ping_ms: int):
token = UK_TOKENS.get(monitor_name) token = UK_TOKENS.get(monitor_name)
if not token: if not token:
logger.warning(f"No token found for monitor {monitor_name}") logger.warning(f"No token found for monitor {monitor_name}")
return 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"})
return
url = f"{UK_PUSH_URL_BASE}/{token}" url = f"{UK_PUSH_URL_BASE}/{token}"
params = { params = {
"status": status, "status": status,
"msg": msg, "msg": msg,
"ping": int(ping_ms) "ping": int(ping_ms)
} }
try: try:
response = requests.get(url, params=params, timeout=10) response = requests.get(url, params=params, timeout=10)
response.raise_for_status() response.raise_for_status()