62 lines
1.6 KiB
Python
62 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
from app.checker import check_all
|
|
from app.config import ConfigError, load_config
|
|
from app.influx_writer import write_results
|
|
|
|
logger = logging.getLogger("uptime_monitor")
|
|
|
|
|
|
def setup_logging() -> None:
|
|
logging.basicConfig(
|
|
level=os.environ.get("LOG_LEVEL", "INFO"),
|
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
|
stream=sys.stdout,
|
|
)
|
|
|
|
|
|
def run_cycle(cfg) -> list:
|
|
timestamp_ns = time.time_ns()
|
|
results = check_all(cfg.endpoints, cfg.check.timeout_seconds, timestamp_ns)
|
|
write_results(cfg.influx, results)
|
|
return results
|
|
|
|
|
|
def main() -> None:
|
|
setup_logging()
|
|
config_path = os.environ.get("CONFIG_PATH", "/config/config.yaml")
|
|
|
|
try:
|
|
cfg = load_config(config_path)
|
|
except ConfigError as e:
|
|
logger.error("configuration error: %s", e)
|
|
sys.exit(1)
|
|
|
|
logger.info(
|
|
"loaded %d endpoint(s) from %s; interval=%ss, immediate recheck on failure enabled",
|
|
len(cfg.endpoints), config_path, cfg.check.interval_seconds,
|
|
)
|
|
|
|
while True:
|
|
results = run_cycle(cfg)
|
|
|
|
if any(not r.success for r in results):
|
|
logger.warning(
|
|
"at least one endpoint failed this cycle; "
|
|
"re-checking all endpoints immediately in %.0fs",
|
|
cfg.check.immediate_recheck_delay_seconds,
|
|
)
|
|
time.sleep(cfg.check.immediate_recheck_delay_seconds)
|
|
run_cycle(cfg)
|
|
|
|
time.sleep(cfg.check.interval_seconds)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|