configure_zabbix_dependencies.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import requests
  2. import json
  3. import time
  4. ZABBIX_API_URL = "http://localhost:8080/api_jsonrpc.php"
  5. ZABBIX_USER = "Admin"
  6. ZABBIX_PASSWORD = "zabbix" # Default zabbix admin password
  7. def authenticate():
  8. payload = {
  9. "jsonrpc": "2.0",
  10. "method": "user.login",
  11. "params": {
  12. "user": ZABBIX_USER,
  13. "password": ZABBIX_PASSWORD
  14. },
  15. "id": 1,
  16. "auth": None
  17. }
  18. response = requests.post(ZABBIX_API_URL, json=payload).json()
  19. if 'result' in response:
  20. return response['result']
  21. else:
  22. print(f"Authentication failed: {response}")
  23. return None
  24. def get_triggers(auth_token, description_search):
  25. payload = {
  26. "jsonrpc": "2.0",
  27. "method": "trigger.get",
  28. "params": {
  29. "output": ["triggerid", "description"],
  30. "search": {
  31. "description": description_search
  32. }
  33. },
  34. "id": 2,
  35. "auth": auth_token
  36. }
  37. response = requests.post(ZABBIX_API_URL, json=payload).json()
  38. return response.get('result', [])
  39. def set_dependency(auth_token, trigger_id, depends_on_trigger_id):
  40. payload = {
  41. "jsonrpc": "2.0",
  42. "method": "trigger.update",
  43. "params": {
  44. "triggerid": trigger_id,
  45. "dependencies": [
  46. {"triggerid": depends_on_trigger_id}
  47. ]
  48. },
  49. "id": 3,
  50. "auth": auth_token
  51. }
  52. response = requests.post(ZABBIX_API_URL, json=payload).json()
  53. if 'result' in response:
  54. print(f"Successfully added dependency! Trigger {trigger_id} now depends on {depends_on_trigger_id}")
  55. else:
  56. print(f"Failed to add dependency: {response}")
  57. if __name__ == "__main__":
  58. print("Waiting for Zabbix server to start...")
  59. time.sleep(10) # Simple wait
  60. try:
  61. auth_token = authenticate()
  62. if not auth_token:
  63. print("Cannot proceed without authentication.")
  64. exit(1)
  65. # Example logic to find DB and App triggers (Names will depend on actual Zabbix config)
  66. db_triggers = get_triggers(auth_token, "MySQL is down")
  67. app_triggers = get_triggers(auth_token, "Application Food AI Down")
  68. if not db_triggers or not app_triggers:
  69. print("Could not find the necessary triggers. They might need to be created first in Zabbix.")
  70. print(f"DB Triggers found: {db_triggers}")
  71. print(f"App Triggers found: {app_triggers}")
  72. else:
  73. db_trigger_id = db_triggers[0]['triggerid']
  74. app_trigger_id = app_triggers[0]['triggerid']
  75. set_dependency(auth_token, app_trigger_id, db_trigger_id)
  76. except Exception as e:
  77. print(f"Error configuring Zabbix: {e}")