2019-11-07 18:08:44 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2019-11-12 23:31:07 +00:00
|
|
|
from borgmatic.hooks import monitor
|
|
|
|
|
2019-11-07 18:08:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-11-12 23:31:07 +00:00
|
|
|
MONITOR_STATE_TO_CRONHUB = {
|
|
|
|
monitor.State.START: 'start',
|
|
|
|
monitor.State.FINISH: 'finish',
|
|
|
|
monitor.State.FAIL: 'fail',
|
|
|
|
}
|
|
|
|
|
2019-11-07 18:08:44 +00:00
|
|
|
|
2020-06-02 21:33:41 +00:00
|
|
|
def initialize_monitor(ping_url, config_filename, monitoring_log_level, dry_run):
|
|
|
|
'''
|
|
|
|
No initialization is necessary for this monitor.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-01-22 23:10:47 +00:00
|
|
|
def ping_monitor(ping_url, config_filename, state, monitoring_log_level, dry_run):
|
2019-11-07 18:08:44 +00:00
|
|
|
'''
|
2019-11-12 23:31:07 +00:00
|
|
|
Ping the given Cronhub URL, modified with the monitor.State. Use the given configuration
|
2019-11-07 18:08:44 +00:00
|
|
|
filename in any log entries. If this is a dry run, then don't actually ping anything.
|
|
|
|
'''
|
|
|
|
dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
|
2019-11-12 23:31:07 +00:00
|
|
|
formatted_state = '/{}/'.format(MONITOR_STATE_TO_CRONHUB[state])
|
2019-11-07 18:08:44 +00:00
|
|
|
ping_url = ping_url.replace('/start/', formatted_state).replace('/ping/', formatted_state)
|
|
|
|
|
2019-11-12 23:31:07 +00:00
|
|
|
logger.info(
|
|
|
|
'{}: Pinging Cronhub {}{}'.format(config_filename, state.name.lower(), dry_run_label)
|
|
|
|
)
|
2019-11-07 18:08:44 +00:00
|
|
|
logger.debug('{}: Using Cronhub ping URL {}'.format(config_filename, ping_url))
|
|
|
|
|
|
|
|
if not dry_run:
|
|
|
|
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
|
|
|
requests.get(ping_url)
|