zabbix.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import logging
  2. import requests
  3. import json
  4. logger = logging.getLogger(__name__)
  5. def initialize_monitor(
  6. ping_url, config, config_filename, monitoring_log_level, dry_run
  7. ): # pragma: no cover
  8. '''
  9. No initialization is necessary for this monitor.
  10. '''
  11. pass
  12. def ping_monitor(hook_config, config, config_filename, state, monitoring_log_level, dry_run):
  13. '''
  14. Update the configured Zabbix item using either the itemid, or a host and key.
  15. If this is a dry run, then don't actually update anything.
  16. '''
  17. run_states = hook_config.get('states', ['fail'])
  18. if state.name.lower() not in run_states:
  19. return
  20. dry_run_label = ' (dry run; not actually updating)' if dry_run else ''
  21. state_config = hook_config.get(state.name.lower(), {'value': f'invalid',},)
  22. base_url = hook_config.get('server', 'https://cloud.zabbix.com/zabbix/api_jsonrpc.php')
  23. username = hook_config.get('username')
  24. password = hook_config.get('password')
  25. api_key = hook_config.get('api_key')
  26. itemid = hook_config.get('itemid')
  27. host = hook_config.get('host')
  28. key = hook_config.get('key')
  29. value = state_config.get('value')
  30. headers = {'Content-Type': 'application/json-rpc'}
  31. logger.info(f'{config_filename}: Updating Zabbix {dry_run_label}')
  32. logger.debug(f'{config_filename}: Using Zabbix URL: {base_url}')
  33. # Determine the zabbix method used to store the value: itemid or host/key
  34. if itemid is not None:
  35. logger.info(f'{config_filename}: Updating {itemid} on Zabbix')
  36. data = {"jsonrpc":"2.0", "method":"history.push", "params":{"itemid":itemid, "value":value}, "id":1}
  37. elif host and key is not None:
  38. logger.info(f'{config_filename}: Updating Host:{host} and Key:{key} on Zabbix')
  39. data = {"jsonrpc":"2.0", "method":"history.push", "params":{"host":host, "key":key,"value":value}, "id":1}
  40. elif host is not None:
  41. logger.warning( f'{config_filename}: Key missing for Zabbix authentication' )
  42. return
  43. elif key is not None:
  44. logger.warning( f'{config_filename}: Host missing for Zabbix authentication' )
  45. return
  46. # Determine the authentication method: API key or username/password
  47. if api_key is not None:
  48. logger.info(f'{config_filename}: Using API key auth for Zabbix')
  49. headers['Authorization'] = 'Bearer ' + api_key
  50. elif username and password is not None:
  51. logger.info(f'{config_filename}: Using user/pass auth with user {username} for Zabbix')
  52. if not dry_run:
  53. response = requests.post(base_url, headers=headers, data=f'{{"jsonrpc":"2.0","method":"user.login","params":{{"username":"{username}","password":"{password}"}},"id":1}}')
  54. data['auth'] = response.json().get('result')
  55. elif username is not None:
  56. logger.warning( f'{config_filename}: Password missing for Zabbix authentication' )
  57. return
  58. elif password is not None:
  59. logger.warning( f'{config_filename}: Username missing for Zabbix authentication' )
  60. return
  61. if not dry_run:
  62. logging.getLogger('urllib3').setLevel(logging.ERROR)
  63. try:
  64. response = requests.post(base_url, headers=headers, data=json.dumps(data))
  65. if not response.ok:
  66. response.raise_for_status()
  67. except requests.exceptions.RequestException as error:
  68. logger.warning(f'{config_filename}: Zabbix error: {error}')
  69. def destroy_monitor(
  70. ping_url_or_uuid, config, config_filename, monitoring_log_level, dry_run
  71. ): # pragma: no cover
  72. '''
  73. No destruction is necessary for this monitor.
  74. '''
  75. pass