Uji Akses Zabbix API Guna Token: Panduan Santai Dari Satu Skrip Python

Apa Script Ni Buat

Script Python simple untuk test sama ada Zabbix API token kita valid atau tak. Run script, dia try authenticate, kalau ok dia pull sikit data untuk confirm access.

Code

import requests

ZABBIX_URL = "https://your-zabbix-server/api_jsonrpc.php"
API_TOKEN = "your-api-token-here"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_TOKEN}"
}

# Test 1: Check API version (no auth needed)
payload = {
    "jsonrpc": "2.0",
    "method": "apiinfo.version",
    "params": [],
    "id": 1
}

response = requests.post(ZABBIX_URL, json=payload, headers=headers)
print(f"API Version: {response.json()['result']}")

# Test 2: Get hosts (needs valid token)
payload = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid", "host", "name"],
        "limit": 5
    },
    "id": 2
}

response = requests.post(ZABBIX_URL, json=payload, headers=headers)
result = response.json()

if "error" in result:
    print(f"FAILED: {result['error']['data']}")
else:
    print(f"SUCCESS - Got {len(result['result'])} hosts")
    for host in result["result"]:
        print(f"  - {host['name']} ({host['host']})")

Expected Output

Kalau token valid:

API Version: 7.0.0
SUCCESS - Got 5 hosts
  - web-server-1 (web1.example.com)
  - db-server-1 (db1.example.com)
  ...

Kalau token invalid:

API Version: 7.0.0
FAILED: Not authorised.

Notes

  • Zabbix 5.4+ support API tokens (sebelum tu kena guna user/password auth)
  • Token boleh create kat Administration → General → API tokens
  • Kalau guna Zabbix behind reverse proxy, make sure API endpoint accessible
  • apiinfo.version tak perlu auth — good untuk test connectivity dulu sebelum test token

Leave a Comment