configure_zabbix_email.py 3.0 KB

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