73 lines
2.2 KiB
Python
73 lines
2.2 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})
|
|
print("Logged in successfully.")
|
|
|
|
# Find templates related to time / ntp
|
|
templates = zbx_req("template.get", {
|
|
"output": ["templateid", "name", "host"],
|
|
"search": {"name": "time"},
|
|
"searchWildcardsEnabled": True
|
|
}, auth=token)
|
|
|
|
print(f"Found {len(templates)} templates matching 'time':")
|
|
for t in templates:
|
|
print(f" ID: {t['templateid']}, Name: {t['name']}")
|
|
|
|
# Find all templates
|
|
all_tpls = zbx_req("template.get", {
|
|
"output": ["templateid", "name"],
|
|
"search": {"name": "Linux"},
|
|
}, auth=token)
|
|
print(f"\nLinux templates:")
|
|
for t in all_tpls:
|
|
print(f" ID: {t['templateid']}, Name: {t['name']}")
|
|
|
|
# Check existing items on a sample host
|
|
hosts = zbx_req("host.get", {
|
|
"output": ["hostid", "host", "name"],
|
|
"filter": {"host": ["aerodrome-ctl01", "sninfr2lnxctl01", "shipyard-swm01"]}
|
|
}, auth=token)
|
|
print(f"\nSample hosts found in Zabbix: {len(hosts)}")
|
|
for h in hosts:
|
|
print(f" HostID: {h['hostid']}, Host: {h['host']}, Name: {h['name']}")
|
|
items = zbx_req("item.get", {
|
|
"output": ["itemid", "name", "key_"],
|
|
"hostids": h["hostid"],
|
|
"search": {"key_": "system.localtime"},
|
|
}, auth=token)
|
|
print(f" system.localtime items: {items}")
|
|
|