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
|
2021-11-08 11:52:41 -05:00
|
|
|
from aprsd import cli_helper, utils
|
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
|
2022-12-31 16:31:40 -05:00
|
|
|
from aprsd.rpc import client as aprsd_rpc_client
|
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."""
|
2021-12-10 10:45:51 -05:00
|
|
|
console.log(f"APRSD HealthCheck version: {aprsd.__version__}")
|
2022-12-31 16:31:40 -05:00
|
|
|
if not CONF.rpc_settings.enabled:
|
|
|
|
LOG.error("Must enable rpc_settings.enabled to use healthcheck")
|
|
|
|
sys.exit(-1)
|
|
|
|
if not CONF.rpc_settings.ip:
|
|
|
|
LOG.error("Must enable rpc_settings.ip to use healthcheck")
|
|
|
|
sys.exit(-1)
|
|
|
|
if not CONF.rpc_settings.magic_word:
|
|
|
|
LOG.error("Must enable rpc_settings.magic_word to use healthcheck")
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2021-12-10 10:45:51 -05:00
|
|
|
with console.status(f"APRSD HealthCheck version: {aprsd.__version__}") as status:
|
|
|
|
try:
|
2022-12-31 16:31:40 -05:00
|
|
|
status.update(f"Contacting APRSD via RPC {CONF.rpc_settings.ip}")
|
|
|
|
stats = aprsd_rpc_client.RPCClient().get_stats_dict()
|
2021-12-10 10:45:51 -05:00
|
|
|
except Exception as ex:
|
2022-12-31 16:31:40 -05:00
|
|
|
console.log(f"Failed to fetch healthcheck : '{ex}'")
|
2021-12-10 10:45:51 -05:00
|
|
|
sys.exit(-1)
|
|
|
|
else:
|
2022-12-31 16:31:40 -05:00
|
|
|
if not stats:
|
|
|
|
console.log("No stats from aprsd")
|
|
|
|
sys.exit(-1)
|
|
|
|
email_thread_last_update = stats["email"]["thread_last_update"]
|
2021-11-05 14:20:47 -04:00
|
|
|
|
2021-12-10 10:45:51 -05:00
|
|
|
if email_thread_last_update != "never":
|
|
|
|
delta = utils.parse_delta_str(email_thread_last_update)
|
|
|
|
d = datetime.timedelta(**delta)
|
|
|
|
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)
|
2021-11-05 14:20:47 -04:00
|
|
|
|
2022-12-31 16:31:40 -05:00
|
|
|
aprsis_last_update = stats["aprs-is"]["last_update"]
|
2021-12-10 10:45:51 -05:00
|
|
|
delta = utils.parse_delta_str(aprsis_last_update)
|
|
|
|
d = datetime.timedelta(**delta)
|
|
|
|
max_timeout = {"hours": 0.0, "minutes": 5, "seconds": 0}
|
|
|
|
max_delta = datetime.timedelta(**max_timeout)
|
|
|
|
if d > max_delta:
|
|
|
|
LOG.error(f"APRS-IS last update is very old! {d}")
|
|
|
|
sys.exit(-1)
|
2021-11-05 14:20:47 -04:00
|
|
|
|
2021-12-10 10:45:51 -05:00
|
|
|
sys.exit(0)
|