configure_zabbix_email.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import requests
  2. import json
  3. import os
  4. ZABBIX_API_URL = "http://localhost:8081/api_jsonrpc.php"
  5. ZABBIX_USER = "Admin"
  6. ZABBIX_PASSWORD = "zabbix"
  7. def get_email_from_env():
  8. if os.path.exists('.env'):
  9. with open('.env', 'r') as f:
  10. for line in f:
  11. if line.startswith('ADMIN_EMAIL='):
  12. return line.strip().split('=', 1)[1]
  13. return "lanfr144@gmail.com" # Default fallback
  14. def authenticate():
  15. payload = {"jsonrpc": "2.0", "method": "user.login", "params": {"user": ZABBIX_USER, "password": ZABBIX_PASSWORD}, "id": 1}
  16. try:
  17. response = requests.post(ZABBIX_API_URL, json=payload).json()
  18. return response.get('result')
  19. except Exception as e:
  20. print(f"Error connecting to Zabbix API: {e}")
  21. return None
  22. def configure_email(auth_token, email_address):
  23. # 1. Update Admin User (ID 1) Media
  24. print(f"Configuring Admin user to receive alerts at: {email_address}")
  25. user_payload = {
  26. "jsonrpc": "2.0",
  27. "method": "user.update",
  28. "params": {
  29. "userid": "1",
  30. "medias": [
  31. {
  32. "mediatypeid": "1", # Default Email media type
  33. "sendto": [email_address],
  34. "active": 0, # Enabled
  35. "severity": 63, # All severities
  36. "period": "1-7,00:00-24:00"
  37. }
  38. ]
  39. },
  40. "id": 2,
  41. "auth": auth_token
  42. }
  43. res = requests.post(ZABBIX_API_URL, json=user_payload).json()
  44. if 'result' in res:
  45. print("User media successfully updated.")
  46. else:
  47. print(f"Failed to update user media: {res}")
  48. # 2. Enable "Report problems to Zabbix administrators" Action
  49. # Usually ID 2 or 3. Let's find it.
  50. action_search = {
  51. "jsonrpc": "2.0",
  52. "method": "action.get",
  53. "params": {
  54. "output": ["actionid", "name"],
  55. "search": {"name": "Report problems to Zabbix administrators"}
  56. },
  57. "id": 3,
  58. "auth": auth_token
  59. }
  60. actions = requests.post(ZABBIX_API_URL, json=action_search).json().get('result', [])
  61. if actions:
  62. action_id = actions[0]['actionid']
  63. action_enable = {
  64. "jsonrpc": "2.0",
  65. "method": "action.update",
  66. "params": {"actionid": action_id, "status": 0}, # 0 is enabled
  67. "id": 4,
  68. "auth": auth_token
  69. }
  70. res_act = requests.post(ZABBIX_API_URL, json=action_enable).json()
  71. if 'result' in res_act:
  72. print(f"Alert Action '{actions[0]['name']}' successfully enabled.")
  73. else:
  74. print(f"Failed to enable action: {res_act}")
  75. else:
  76. print("Could not find default action 'Report problems to Zabbix administrators' to enable.")
  77. if __name__ == "__main__":
  78. email = get_email_from_env()
  79. token = authenticate()
  80. if token:
  81. configure_email(token, email)
  82. else:
  83. print("Could not authenticate to Zabbix on localhost:8081.")