cronitor.py 1.8 KB

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