feat(health-agent): add --once and --dry-run flags to main.py
This commit is contained in:
parent
c49616ac10
commit
7ab186b961
@ -1,5 +1,7 @@
|
||||
import argparse
|
||||
import time
|
||||
import logging
|
||||
import json
|
||||
from health_agent.checks import swarm
|
||||
from health_agent.checks.http import run_all_http_checks
|
||||
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.filesystem import check_storagebox_mount
|
||||
from health_agent.events.docker_events import start_docker_event_listener
|
||||
import json
|
||||
import health_agent.uptime_kuma as uk
|
||||
|
||||
class JSONFormatter(logging.Formatter):
|
||||
def format(self, record):
|
||||
@ -68,8 +70,23 @@ def run_checks():
|
||||
logger.error(f"Error running filesystem checks: {e}")
|
||||
|
||||
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...")
|
||||
start_docker_event_listener()
|
||||
while True:
|
||||
|
||||
if not args.dry_run:
|
||||
start_docker_event_listener()
|
||||
|
||||
if args.once:
|
||||
run_checks()
|
||||
time.sleep(60)
|
||||
else:
|
||||
while True:
|
||||
run_checks()
|
||||
time.sleep(60)
|
||||
|
||||
@ -4,21 +4,27 @@ import logging
|
||||
from health_agent.config import UK_TOKENS
|
||||
|
||||
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):
|
||||
token = UK_TOKENS.get(monitor_name)
|
||||
if not token:
|
||||
logger.warning(f"No token found for monitor {monitor_name}")
|
||||
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}"
|
||||
params = {
|
||||
"status": status,
|
||||
"msg": msg,
|
||||
"ping": int(ping_ms)
|
||||
}
|
||||
|
||||
|
||||
try:
|
||||
response = requests.get(url, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user