cronhub.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import logging
  2. import requests
  3. from borgmatic.hooks.monitoring import monitor
  4. logger = logging.getLogger(__name__)
  5. MONITOR_STATE_TO_CRONHUB = {
  6. monitor.State.START: 'start',
  7. monitor.State.FINISH: 'finish',
  8. monitor.State.FAIL: 'fail',
  9. }
  10. def initialize_monitor(
  11. ping_url, config, config_filename, monitoring_log_level, dry_run
  12. ): # pragma: no cover
  13. '''
  14. No initialization is necessary for this monitor.
  15. '''
  16. pass
  17. def ping_monitor(hook_config, config, config_filename, state, monitoring_log_level, dry_run):
  18. '''
  19. Ping the configured Cronhub URL, modified with the monitor.State. Use the given configuration
  20. filename in any log entries. If this is a dry run, then don't actually ping anything.
  21. '''
  22. if state not in MONITOR_STATE_TO_CRONHUB:
  23. logger.debug(
  24. f'Ignoring unsupported monitoring {state.name.lower()} in Cronhub hook'
  25. )
  26. return
  27. dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
  28. formatted_state = f'/{MONITOR_STATE_TO_CRONHUB[state]}/'
  29. ping_url = (
  30. hook_config['ping_url']
  31. .replace('/start/', formatted_state)
  32. .replace('/ping/', formatted_state)
  33. )
  34. logger.info(f'Pinging Cronhub {state.name.lower()}{dry_run_label}')
  35. logger.debug(f'Using Cronhub ping URL {ping_url}')
  36. if not dry_run:
  37. logging.getLogger('urllib3').setLevel(logging.ERROR)
  38. try:
  39. response = requests.get(ping_url)
  40. if not response.ok:
  41. response.raise_for_status()
  42. except requests.exceptions.RequestException as error:
  43. logger.warning(f'Cronhub error: {error}')
  44. def destroy_monitor(
  45. ping_url_or_uuid, config, monitoring_log_level, dry_run
  46. ): # pragma: no cover
  47. '''
  48. No destruction is necessary for this monitor.
  49. '''
  50. pass