41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
import urllib.request
|
|
import json
|
|
import ssl
|
|
|
|
ctx = ssl.create_default_context()
|
|
ctx.check_hostname = False
|
|
ctx.verify_mode = ssl.CERT_NONE
|
|
|
|
def zabbix_api(method, params, auth=None):
|
|
payload = {"jsonrpc": "2.0", "method": method, "params": params, "id": 1}
|
|
if auth: payload["auth"] = auth
|
|
req = urllib.request.Request("https://zabbix.snarfnet.net/api_jsonrpc.php",
|
|
data=json.dumps(payload).encode('utf-8'),
|
|
headers={"Content-Type": "application/json-rpc"})
|
|
resp = urllib.request.urlopen(req, context=ctx, timeout=10)
|
|
return json.loads(resp.read().decode('utf-8'))
|
|
|
|
auth = zabbix_api("user.login", {"username": "Admin", "password": "zabbix"}).get("result")
|
|
|
|
# Search hosts with idrac
|
|
hosts = zabbix_api("host.get", {
|
|
"output": ["hostid", "host", "name", "status"],
|
|
"selectInterfaces": ["ip", "port", "type"],
|
|
"selectParentTemplates": ["templateid", "name"],
|
|
"search": {"host": "idrac"}
|
|
}, auth=auth).get("result", [])
|
|
|
|
for h in hosts:
|
|
ips = [i["ip"] for i in h.get("interfaces", [])]
|
|
tpls = [t["name"] for t in h.get("parentTemplates", [])]
|
|
print(f"Host: {h['host']} | Name: {h['name']} | IPs: {ips} | Templates: {tpls}")
|
|
|
|
# Find ICMP template
|
|
tpls = zabbix_api("template.get", {
|
|
"output": ["templateid", "name"]
|
|
}, auth=auth).get("result", [])
|
|
|
|
for t in tpls:
|
|
if "icmp" in t["name"].lower() or "ping" in t["name"].lower() or "idrac" in t["name"].lower() or "harvester" in t["name"].lower():
|
|
print(f"Template {t['templateid']}: {t['name']}")
|