1
0

setup_nginx_zabbix.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import requests
  2. import json
  3. import os
  4. ZABBIX_API_URL = "http://zabbix-web:8080/api_jsonrpc.php"
  5. ZABBIX_USER = "Admin"
  6. ZABBIX_PASSWORD = "zabbix"
  7. def authenticate():
  8. payload = {"jsonrpc": "2.0", "method": "user.login", "params": {"username": ZABBIX_USER, "password": ZABBIX_PASSWORD}, "id": 1}
  9. try:
  10. response = requests.post(ZABBIX_API_URL, json=payload).json()
  11. return response.get('result')
  12. except Exception as e:
  13. print(f"Error connecting to Zabbix API: {e}")
  14. return None
  15. def configure_nginx_web_scenario(auth_token):
  16. # Get Zabbix server host ID
  17. host_search = {
  18. "jsonrpc": "2.0",
  19. "method": "host.get",
  20. "params": {
  21. "filter": {"host": ["Zabbix server"]}
  22. },
  23. "id": 2,
  24. "auth": auth_token
  25. }
  26. hosts = requests.post(ZABBIX_API_URL, json=host_search).json().get('result', [])
  27. if not hosts:
  28. print("Could not find Zabbix server host.")
  29. return
  30. host_id = hosts[0]['hostid']
  31. print("Checking if Nginx Web Scenario already exists...")
  32. scenario_search = {
  33. "jsonrpc": "2.0",
  34. "method": "httptest.get",
  35. "params": {
  36. "filter": {"name": ["Nginx Streamlit Proxy Check"]}
  37. },
  38. "id": 3,
  39. "auth": auth_token
  40. }
  41. scenarios = requests.post(ZABBIX_API_URL, json=scenario_search).json().get('result', [])
  42. if scenarios:
  43. print("Nginx Web Scenario already exists.")
  44. return
  45. print("Creating Nginx Web Scenario...")
  46. create_payload = {
  47. "jsonrpc": "2.0",
  48. "method": "httptest.create",
  49. "params": {
  50. "name": "Nginx Streamlit Proxy Check",
  51. "hostid": host_id,
  52. "delay": "1m",
  53. "retries": 3,
  54. "steps": [
  55. {
  56. "name": "Check Proxy Root",
  57. "url": "http://nginx:80",
  58. "status_codes": "200",
  59. "no": 1
  60. }
  61. ]
  62. },
  63. "id": 4,
  64. "auth": auth_token
  65. }
  66. res = requests.post(ZABBIX_API_URL, json=create_payload).json()
  67. if 'result' in res:
  68. print(f"Successfully created Nginx Web Scenario.")
  69. else:
  70. print(f"Failed to create Web Scenario: {res}")
  71. if __name__ == "__main__":
  72. token = authenticate()
  73. if token:
  74. configure_nginx_web_scenario(token)
  75. else:
  76. print("Failed to authenticate to Zabbix API.")