uptimekuma.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import logging
  2. import requests
  3. logger = logging.getLogger(__name__)
  4. def initialize_monitor(
  5. push_url, config, config_filename, monitoring_log_level, dry_run
  6. ): # pragma: no cover
  7. '''
  8. No initialization is necessary for this monitor.
  9. '''
  10. pass
  11. def ping_monitor(hook_config, config, config_filename, state, monitoring_log_level, dry_run):
  12. '''
  13. Make a get request to the configured Uptime Kuma push_url.
  14. Use the given configuration filename in any log entries.
  15. If this is a dry run, then don't actually push anything.
  16. '''
  17. run_states = hook_config.get('states', ['start', 'finish', 'fail'])
  18. if state.name.lower() not in run_states:
  19. return
  20. dry_run_label = ' (dry run; not actually pushing)' if dry_run else ''
  21. status = 'down' if state.name.lower() == 'fail' else 'up'
  22. push_url = hook_config.get('push_url', 'https://example.uptime.kuma/api/push/abcd1234')
  23. query = f'status={status}&msg={state.name.lower()}'
  24. logger.info(
  25. f'{config_filename}: Pushing Uptime Kuma push_url {push_url}?{query} {dry_run_label}'
  26. )
  27. logger.debug(f'{config_filename}: Full Uptime Kuma state URL {push_url}?{query}')
  28. if dry_run:
  29. return
  30. logging.getLogger('urllib3').setLevel(logging.ERROR)
  31. try:
  32. response = requests.get(f'{push_url}?{query}')
  33. if not response.ok:
  34. response.raise_for_status()
  35. except requests.exceptions.RequestException as error:
  36. logger.warning(f'{config_filename}: Uptime Kuma error: {error}')
  37. def destroy_monitor(
  38. push_url_or_uuid, config, config_filename, monitoring_log_level, dry_run
  39. ): # pragma: no cover
  40. '''
  41. No destruction is necessary for this monitor.
  42. '''
  43. pass