Files
SnarfCode/scripts/check_zabbix_hosts_items.py

63 lines
1.9 KiB
Python

import json
import ssl
import urllib.request
ZABBIX_URL = "https://zabbix.snarfnet.net/api_jsonrpc.php"
ZABBIX_USER = "Admin"
ZABBIX_PASS = "zabbix"
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
def zbx_req(method: str, params: dict, auth: str = None):
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": 1,
}
headers = {"Content-Type": "application/json-rpc"}
if auth:
headers["Authorization"] = f"Bearer {auth}"
req = urllib.request.Request(
ZABBIX_URL,
data=json.dumps(payload).encode("utf-8"),
headers=headers,
method="POST",
)
with urllib.request.urlopen(req, timeout=10, context=ctx) as r:
resp = json.loads(r.read().decode("utf-8"))
if "error" in resp:
raise RuntimeError(f"Zabbix API error in {method}: {resp['error']}")
return resp["result"]
token = zbx_req("user.login", {"username": ZABBIX_USER, "password": ZABBIX_PASS})
# List all hosts in Zabbix
hosts = zbx_req("host.get", {
"output": ["hostid", "host", "name", "status"],
}, auth=token)
print(f"Total Hosts in Zabbix: {len(hosts)}")
for h in hosts[:25]:
print(f" {h['hostid']}: {h['host']} ({h['name']}) [status={h['status']}]")
# Inspect items in Linux by Zabbix agent (10001)
items = zbx_req("item.get", {
"templateids": "10001",
"output": ["itemid", "name", "key_"],
"search": {"key_": "localtime"},
}, auth=token)
print(f"\nItems matching 'localtime' in Template 10001: {items}")
triggers = zbx_req("trigger.get", {
"templateids": "10001",
"output": ["triggerid", "description", "expression", "priority"],
"search": {"description": "time"},
}, auth=token)
print(f"\nTriggers matching 'time' in Template 10001:")
for tr in triggers:
print(f" ID: {tr['triggerid']}, Desc: {tr['description']}, Priority: {tr['priority']}")