1
0

create_zabbix_dashboard.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import requests
  2. ZABBIX_API_URL = "http://192.168.130.170:8081/api_jsonrpc.php"
  3. ZABBIX_USER = "Admin"
  4. ZABBIX_PASSWORD = "zabbix"
  5. def authenticate():
  6. payload = {
  7. "jsonrpc": "2.0",
  8. "method": "user.login",
  9. "params": {"username": ZABBIX_USER, "password": ZABBIX_PASSWORD},
  10. "id": 1
  11. }
  12. try:
  13. response = requests.post(ZABBIX_API_URL, json=payload).json()
  14. return response.get('result')
  15. except Exception as e:
  16. print(f"Error connecting to Zabbix API: {e}")
  17. return None
  18. def get_snmp_item_id(auth_token):
  19. # Retrieve the SNMP fallback item which catches all traps
  20. payload = {
  21. "jsonrpc": "2.0",
  22. "method": "item.get",
  23. "params": {
  24. "output": ["itemid", "name", "key_"],
  25. "search": {
  26. "key_": "snmptrap.fallback"
  27. }
  28. },
  29. "id": 2,
  30. "auth": auth_token
  31. }
  32. response = requests.post(ZABBIX_API_URL, json=payload).json()
  33. items = response.get('result', [])
  34. if items:
  35. print(f"Found SNMP Trap Item: {items[0]['itemid']}")
  36. return items[0]['itemid']
  37. else:
  38. # Fallback to search any SNMP trap item
  39. payload["params"]["search"] = {"key_": "snmptrap["}
  40. response = requests.post(ZABBIX_API_URL, json=payload).json()
  41. items = response.get('result', [])
  42. if items:
  43. print(f"Found specific SNMP Trap Item: {items[0]['itemid']}")
  44. return items[0]['itemid']
  45. print("Could not find any SNMP Trap item in Zabbix.")
  46. return None
  47. def create_dashboard(auth_token, item_id):
  48. print("Creating Food AI RAG Telemetry Dashboard...")
  49. # A Plaintext widget needs the itemid passed correctly in fields
  50. widget_fields = []
  51. if item_id:
  52. widget_fields = [
  53. {"type": 4, "name": "itemids", "value": item_id},
  54. {"type": 0, "name": "show_lines", "value": 25}
  55. ]
  56. payload = {
  57. "jsonrpc": "2.0",
  58. "method": "dashboard.create",
  59. "params": {
  60. "name": "Food AI RAG Telemetry (Live)",
  61. "userid": "1",
  62. "pages": [
  63. {
  64. "name": "SNMP Trap Activity",
  65. "widgets": [
  66. {
  67. "type": "plaintext",
  68. "name": "Ingestion Row Count Log",
  69. "x": 0, "y": 0, "width": 12, "height": 8,
  70. "fields": widget_fields
  71. },
  72. {
  73. "type": "systeminfo",
  74. "name": "Server Status",
  75. "x": 12, "y": 0, "width": 12, "height": 8
  76. }
  77. ]
  78. }
  79. ]
  80. },
  81. "id": 3,
  82. "auth": auth_token
  83. }
  84. response = requests.post(ZABBIX_API_URL, json=payload).json()
  85. if 'result' in response:
  86. print(f"Dashboard Created successfully! ID: {response['result']['dashboardids'][0]}")
  87. else:
  88. print(f"Failed to create dashboard: {response}")
  89. if __name__ == "__main__":
  90. token = authenticate()
  91. if token:
  92. item_id = get_snmp_item_id(token)
  93. create_dashboard(token, item_id)
  94. else:
  95. print("❌ Could not authenticate to Zabbix. Ensure the server is fully started on port 8081.")