2019-11-01 18:33:15 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2019-11-12 23:31:07 +00:00
|
|
|
from borgmatic.hooks import monitor
|
|
|
|
|
2019-11-01 18:33:15 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-11-12 23:31:07 +00:00
|
|
|
MONITOR_STATE_TO_CRONITOR = {
|
|
|
|
monitor.State.START: 'run',
|
|
|
|
monitor.State.FINISH: 'complete',
|
|
|
|
monitor.State.FAIL: 'fail',
|
|
|
|
}
|
|
|
|
|
2019-11-01 18:33:15 +00:00
|
|
|
|
2020-06-06 21:33:06 +00:00
|
|
|
def initialize_monitor(
|
|
|
|
ping_url, config_filename, monitoring_log_level, dry_run
|
|
|
|
): # pragma: no cover
|
2020-06-02 21:33:41 +00:00
|
|
|
'''
|
|
|
|
No initialization is necessary for this monitor.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-05-24 03:02:10 +00:00
|
|
|
def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_run):
|
2019-11-01 18:33:15 +00:00
|
|
|
'''
|
2022-05-24 03:02:10 +00:00
|
|
|
Ping the configured Cronitor URL, modified with the monitor.State. Use the given configuration
|
2019-11-12 23:31:07 +00:00
|
|
|
filename in any log entries. If this is a dry run, then don't actually ping anything.
|
2019-11-01 18:33:15 +00:00
|
|
|
'''
|
2023-03-06 14:28:57 +00:00
|
|
|
if state not in MONITOR_STATE_TO_CRONITOR:
|
|
|
|
logger.debug(
|
2023-03-07 22:00:12 +00:00
|
|
|
f'{config_filename}: Ignoring unsupported monitoring {state.name.lower()} in Cronitor hook'
|
2023-03-06 14:28:57 +00:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2019-11-01 18:33:15 +00:00
|
|
|
dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
|
2023-03-24 06:11:14 +00:00
|
|
|
ping_url = f"{hook_config['ping_url']}/{MONITOR_STATE_TO_CRONITOR[state]}"
|
2019-11-01 18:33:15 +00:00
|
|
|
|
2023-03-24 06:11:14 +00:00
|
|
|
logger.info(f'{config_filename}: Pinging Cronitor {state.name.lower()}{dry_run_label}')
|
|
|
|
logger.debug(f'{config_filename}: Using Cronitor ping URL {ping_url}')
|
2019-11-01 18:33:15 +00:00
|
|
|
|
2019-11-07 18:08:44 +00:00
|
|
|
if not dry_run:
|
|
|
|
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
2022-05-24 22:50:04 +00:00
|
|
|
try:
|
2022-06-30 04:19:40 +00:00
|
|
|
response = requests.get(ping_url)
|
|
|
|
if not response.ok:
|
|
|
|
response.raise_for_status()
|
2022-05-24 22:50:04 +00:00
|
|
|
except requests.exceptions.RequestException as error:
|
|
|
|
logger.warning(f'{config_filename}: Cronitor error: {error}')
|
2020-06-26 03:23:25 +00:00
|
|
|
|
|
|
|
|
2020-06-26 03:25:29 +00:00
|
|
|
def destroy_monitor(
|
|
|
|
ping_url_or_uuid, config_filename, monitoring_log_level, dry_run
|
|
|
|
): # pragma: no cover
|
2020-06-26 03:23:25 +00:00
|
|
|
'''
|
|
|
|
No destruction is necessary for this monitor.
|
|
|
|
'''
|
|
|
|
pass
|