feat: add homelab deployment configs, documentation, and automation scripts
This commit is contained in:
118
scripts/add_zabbix_devices.py
Normal file
118
scripts/add_zabbix_devices.py
Normal file
@@ -0,0 +1,118 @@
|
||||
import json
|
||||
import ssl
|
||||
import urllib.request
|
||||
|
||||
ZABBIX_URL = "http://140.44.4.60:8888/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_call(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("utf-8"))
|
||||
if "error" in resp:
|
||||
raise RuntimeError(f"Zabbix error in {method}: {resp['error']}")
|
||||
return resp["result"]
|
||||
|
||||
auth = zbx_call("user.login", {"username": ZABBIX_USER, "password": ZABBIX_PASS})
|
||||
|
||||
# Check existing host names
|
||||
existing_hosts = zbx_call("host.get", {"output": ["hostid", "host", "name"]}, auth)
|
||||
existing_names = {h["host"].lower(): h["hostid"] for h in existing_hosts}
|
||||
|
||||
# Create host groups if not exist
|
||||
groups = zbx_call("hostgroup.get", {"output": ["groupid", "name"]}, auth)
|
||||
group_map = {g["name"]: g["groupid"] for g in groups}
|
||||
|
||||
for gname in ["Network Devices", "Environmental & Sensors", "Management & IPMI"]:
|
||||
if gname not in group_map:
|
||||
res = zbx_call("hostgroup.create", {"name": gname}, auth)
|
||||
group_map[gname] = res["groupids"][0]
|
||||
print(f"Created Host Group: {gname} (ID: {group_map[gname]})")
|
||||
|
||||
devices_to_add = [
|
||||
{
|
||||
"host": "idrac-spacedock4",
|
||||
"name": "Dell PowerEdge R730 Node 4 iDRAC (idrac-spacedock4)",
|
||||
"ip": "140.44.44.124",
|
||||
"group": "Management & IPMI"
|
||||
},
|
||||
{
|
||||
"host": "usw-agg-core",
|
||||
"name": "UniFi Switch Aggregation 10G Core",
|
||||
"ip": "14.44.4.26",
|
||||
"group": "Network Devices"
|
||||
},
|
||||
{
|
||||
"host": "usw-24-poe-closet",
|
||||
"name": "UniFi Switch 24 PoE Closet",
|
||||
"ip": "14.44.4.61",
|
||||
"group": "Network Devices"
|
||||
},
|
||||
{
|
||||
"host": "usw-pro-24-poe-compute",
|
||||
"name": "UniFi Switch Pro 24 PoE Compute",
|
||||
"ip": "14.44.4.107",
|
||||
"group": "Network Devices"
|
||||
},
|
||||
{
|
||||
"host": "usw-flex-attic",
|
||||
"name": "UniFi Switch Flex Attic",
|
||||
"ip": "14.44.4.98",
|
||||
"group": "Network Devices"
|
||||
},
|
||||
{
|
||||
"host": "usw-flex-garage",
|
||||
"name": "UniFi Switch Flex Garage",
|
||||
"ip": "14.44.4.96",
|
||||
"group": "Network Devices"
|
||||
},
|
||||
{
|
||||
"host": "tempest-weather-station",
|
||||
"name": "Tempest Weather Station Gateway",
|
||||
"ip": "14.4.4.47",
|
||||
"group": "Environmental & Sensors"
|
||||
},
|
||||
{
|
||||
"host": "sense-energy-monitor",
|
||||
"name": "Sense Home Energy Gateway",
|
||||
"ip": "14.4.4.48",
|
||||
"group": "Environmental & Sensors"
|
||||
}
|
||||
]
|
||||
|
||||
for dev in devices_to_add:
|
||||
hname = dev["host"]
|
||||
if hname.lower() in existing_names:
|
||||
print(f"[EXISTS] {hname}")
|
||||
continue
|
||||
params = {
|
||||
"host": dev["host"],
|
||||
"name": dev["name"],
|
||||
"interfaces": [
|
||||
{
|
||||
"type": 1, # Agent / ICMP
|
||||
"main": 1,
|
||||
"useip": 1,
|
||||
"ip": dev["ip"],
|
||||
"dns": "",
|
||||
"port": "10050"
|
||||
}
|
||||
],
|
||||
"groups": [{"groupid": group_map[dev["group"]]}],
|
||||
"templates": [{"templateid": "10564"}] # ICMP Ping
|
||||
}
|
||||
try:
|
||||
res = zbx_call("host.create", params, auth)
|
||||
print(f"[CREATED] {dev['name']} ({dev['ip']}) -> HostID {res['hostids'][0]}")
|
||||
except Exception as e:
|
||||
print(f"[FAILED] {dev['name']}: {e}")
|
||||
|
||||
Reference in New Issue
Block a user