cronitor.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import logging
  2. import requests
  3. from borgmatic.hooks 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. def initialize_monitor(
  11. ping_url, 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_filename, state, monitoring_log_level, dry_run):
  18. '''
  19. Ping the configured Cronitor 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. dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
  23. ping_url = '{}/{}'.format(hook_config['ping_url'], MONITOR_STATE_TO_CRONITOR[state])
  24. logger.info(
  25. '{}: Pinging Cronitor {}{}'.format(config_filename, state.name.lower(), dry_run_label)
  26. )
  27. logger.debug('{}: Using Cronitor ping URL {}'.format(config_filename, ping_url))
  28. if not dry_run:
  29. logging.getLogger('urllib3').setLevel(logging.ERROR)
  30. try:
  31. requests.get(ping_url)
  32. except requests.exceptions.RequestException as error:
  33. logger.warning(f'{config_filename}: Cronitor error: {error}')
  34. def destroy_monitor(
  35. ping_url_or_uuid, config_filename, monitoring_log_level, dry_run
  36. ): # pragma: no cover
  37. '''
  38. No destruction is necessary for this monitor.
  39. '''
  40. pass