2021-11-05 14:20:47 -04:00
|
|
|
#
|
|
|
|
# Used to fetch the stats url and determine if
|
|
|
|
# aprsd server is 'healthy'
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# python included libs
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import click
|
2022-12-31 16:31:40 -05:00
|
|
|
from oslo_config import cfg
|
2021-12-10 10:45:51 -05:00
|
|
|
from rich.console import Console
|
2021-11-05 14:20:47 -04:00
|
|
|
|
|
|
|
import aprsd
|
2024-04-05 12:42:50 -04:00
|
|
|
from aprsd import cli_helper
|
2022-12-31 16:31:40 -05:00
|
|
|
from aprsd import conf # noqa
|
2021-11-05 14:20:47 -04:00
|
|
|
# local imports here
|
2023-07-08 17:30:22 -04:00
|
|
|
from aprsd.main import cli
|
2024-04-05 12:42:50 -04:00
|
|
|
from aprsd.threads import stats as stats_threads
|
2021-11-05 14:20:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
# setup the global logger
|
2023-07-16 16:28:15 -04:00
|
|
|
# log.basicConfig(level=log.DEBUG) # level=10
|
2022-12-31 16:31:40 -05:00
|
|
|
CONF = cfg.CONF
|
2021-11-05 14:20:47 -04:00
|
|
|
LOG = logging.getLogger("APRSD")
|
2021-12-10 10:45:51 -05:00
|
|
|
console = Console()
|
2021-11-05 14:20:47 -04:00
|
|
|
|
2021-11-05 14:38:23 -04:00
|
|
|
|
2021-11-05 14:20:47 -04:00
|
|
|
@cli.command()
|
2021-11-08 11:52:41 -05:00
|
|
|
@cli_helper.add_options(cli_helper.common_options)
|
2021-11-05 14:20:47 -04:00
|
|
|
@click.option(
|
|
|
|
"--timeout",
|
|
|
|
show_default=True,
|
|
|
|
default=3,
|
|
|
|
help="How long to wait for healtcheck url to come back",
|
|
|
|
)
|
|
|
|
@click.pass_context
|
2022-12-31 16:31:40 -05:00
|
|
|
@cli_helper.process_standard_options
|
|
|
|
def healthcheck(ctx, timeout):
|
2021-11-05 14:20:47 -04:00
|
|
|
"""Check the health of the running aprsd server."""
|
2024-04-05 12:42:50 -04:00
|
|
|
ver_str = f"APRSD HealthCheck version: {aprsd.__version__}"
|
|
|
|
console.log(ver_str)
|
2022-12-31 16:31:40 -05:00
|
|
|
|
2024-04-05 12:42:50 -04:00
|
|
|
with console.status(ver_str):
|
2021-12-10 10:45:51 -05:00
|
|
|
try:
|
2024-04-05 12:42:50 -04:00
|
|
|
stats_obj = stats_threads.StatsStore()
|
|
|
|
stats_obj.load()
|
|
|
|
stats = stats_obj.data
|
|
|
|
# console.print(stats)
|
2021-12-10 10:45:51 -05:00
|
|
|
except Exception as ex:
|
2024-04-05 12:42:50 -04:00
|
|
|
console.log(f"Failed to load stats: '{ex}'")
|
2021-12-10 10:45:51 -05:00
|
|
|
sys.exit(-1)
|
|
|
|
else:
|
2024-04-05 12:42:50 -04:00
|
|
|
now = datetime.datetime.now()
|
2022-12-31 16:31:40 -05:00
|
|
|
if not stats:
|
|
|
|
console.log("No stats from aprsd")
|
|
|
|
sys.exit(-1)
|
2021-11-05 14:20:47 -04:00
|
|
|
|
2024-04-05 12:42:50 -04:00
|
|
|
email_stats = stats.get("EmailStats")
|
|
|
|
if email_stats:
|
|
|
|
email_thread_last_update = email_stats["last_check_time"]
|
|
|
|
|
|
|
|
if email_thread_last_update != "never":
|
|
|
|
d = now - email_thread_last_update
|
|
|
|
max_timeout = {"hours": 0.0, "minutes": 5, "seconds": 0}
|
|
|
|
max_delta = datetime.timedelta(**max_timeout)
|
|
|
|
if d > max_delta:
|
|
|
|
console.log(f"Email thread is very old! {d}")
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
client_stats = stats.get("APRSClientStats")
|
|
|
|
if not client_stats:
|
|
|
|
console.log("No APRSClientStats")
|
|
|
|
sys.exit(-1)
|
|
|
|
else:
|
|
|
|
aprsis_last_update = client_stats["server_keepalive"]
|
|
|
|
d = now - aprsis_last_update
|
2021-12-10 10:45:51 -05:00
|
|
|
max_timeout = {"hours": 0.0, "minutes": 5, "seconds": 0}
|
|
|
|
max_delta = datetime.timedelta(**max_timeout)
|
|
|
|
if d > max_delta:
|
2024-04-05 12:42:50 -04:00
|
|
|
LOG.error(f"APRS-IS last update is very old! {d}")
|
2021-12-10 10:45:51 -05:00
|
|
|
sys.exit(-1)
|
2021-11-05 14:20:47 -04:00
|
|
|
|
2024-04-05 12:42:50 -04:00
|
|
|
console.log("OK")
|
2021-12-10 10:45:51 -05:00
|
|
|
sys.exit(0)
|