62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
import json, ssl, urllib.request
|
|
|
|
ctx = ssl.create_default_context()
|
|
ctx.check_hostname = False
|
|
ctx.verify_mode = ssl.CERT_NONE
|
|
|
|
ZABBIX_URL = "http://140.44.4.60:8888/api_jsonrpc.php"
|
|
|
|
def zbx(method, params, auth=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())
|
|
if "error" in resp:
|
|
raise RuntimeError(f"Error in {method}: {resp['error']}")
|
|
return resp["result"]
|
|
|
|
token = zbx("user.login", {"username": "Admin", "password": "zabbix"})
|
|
print("Logged in!")
|
|
|
|
# 1. Create or get hostgroup 'Storage / NAS'
|
|
groups = zbx("hostgroup.get", {"filter": {"name": "Storage / NAS"}}, token)
|
|
if not groups:
|
|
res = zbx("hostgroup.create", {"name": "Storage / NAS"}, token)
|
|
groupid = res["groupids"][0]
|
|
print(f"Created hostgroup 'Storage / NAS': {groupid}")
|
|
else:
|
|
groupid = groups[0]["groupid"]
|
|
print(f"Found hostgroup 'Storage / NAS': {groupid}")
|
|
|
|
# 2. Register SN-NAS and SnarfNAS
|
|
nas_devices = [
|
|
{"host": "SN-NAS", "name": "SN-NAS (DS920+)", "ip": "14.10.10.71"},
|
|
{"host": "SnarfNAS", "name": "SnarfNAS (DS1821+)", "ip": "14.10.10.72"}
|
|
]
|
|
|
|
for nas in nas_devices:
|
|
existing = zbx("host.get", {"filter": {"host": nas["host"]}}, token)
|
|
if not existing:
|
|
res = zbx("host.create", {
|
|
"host": nas["host"],
|
|
"name": nas["name"],
|
|
"groups": [{"groupid": groupid}],
|
|
"templates": [{"templateid": "10564"}], # ICMP Ping
|
|
"interfaces": [{
|
|
"type": 1, # Agent / ICMP
|
|
"main": 1,
|
|
"useip": 1,
|
|
"ip": nas["ip"],
|
|
"dns": "",
|
|
"port": "10050"
|
|
}]
|
|
}, token)
|
|
print(f"Registered {nas['name']}: hostid={res['hostids'][0]}")
|
|
else:
|
|
print(f"{nas['name']} is already registered.")
|
|
|
|
print("All NAS devices registered in Zabbix!")
|