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: {resp['error']}") return resp["result"] token = zbx_req("user.login", {"username": ZABBIX_USER, "password": ZABBIX_PASS}) # Check if trigger already exists existing = zbx_req("trigger.get", { "templateids": "10343", "search": {"description": "System time is out of sync"}, "output": ["triggerid", "description", "priority", "expression"] }, auth=token) if existing: print(f"Trigger already exists: {existing}") else: print("Creating trigger 'Linux: System time is out of sync (> 5s)' on Template 10343...") res = zbx_req("trigger.create", { "description": "Linux: System time is out of sync (> 5s)", "expression": "fuzzytime(/Linux by Zabbix agent active/system.localtime, 5s)=0", "priority": "4", # High priority "comments": "The host system time differs from Zabbix server time by more than 5 seconds. This is critical as it risks split-brain or consensus loss in K3s etcd and Docker Swarm Raft.", "manual_close": "1" }, auth=token) print("Created trigger:", res) # Verify trigger created trigs = zbx_req("trigger.get", { "templateids": "10343", "search": {"description": "System time is out of sync"}, "output": ["triggerid", "description", "priority", "expression"] }, auth=token) print("Verified triggers on Template 10343:", json.dumps(trigs, indent=2))