Files
SnarfCode/scripts/adjust_zabbix_thresholds.py

68 lines
3.1 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")
print("Zabbix Auth OK:", bool(auth))
# 1. Update Template Macros for 10001 (Agent passive), 10343 (Agent active), 10285 (Prom)
# Find hostmacroids for these templates
template_ids = ["10001", "10343", "10285"]
for tid in template_ids:
macros = zabbix_api("usermacro.get", {
"output": ["hostmacroid", "macro", "value"],
"hostids": [tid]
}, auth=auth).get("result", [])
for m in macros:
mid = m["hostmacroid"]
macro_name = m["macro"]
if macro_name == "{$CPU.UTIL.CRIT}":
res = zabbix_api("usermacro.update", {"hostmacroid": mid, "value": "95"}, auth=auth)
print(f"Template {tid} macro {macro_name} updated to 95: {res.get('result')}")
elif macro_name == "{$MEMORY.UTIL.MAX}":
res = zabbix_api("usermacro.update", {"hostmacroid": mid, "value": "95"}, auth=auth)
print(f"Template {tid} macro {macro_name} updated to 95: {res.get('result')}")
elif macro_name == "{$LOAD_AVG_PER_CPU.MAX.WARN}":
res = zabbix_api("usermacro.update", {"hostmacroid": mid, "value": "2.5"}, auth=auth)
print(f"Template {tid} macro {macro_name} updated to 2.5: {res.get('result')}")
# 2. Update trigger expressions from 5m to 15m
# Triggers: 22370 (Passive CPU), 22376 (Passive Mem), 22415 (Active CPU), 22421 (Active Mem)
# In Zabbix 6.0/7.0, expression format is min(/<template_name>/<item_key>,15m)>{$MACRO}
# Let's inspect the exact expression syntax for trigger update
for tr_id in ["22370", "22376", "22415", "22421"]:
tr = zabbix_api("trigger.get", {
"output": ["triggerid", "description", "expression"],
"triggerids": [tr_id]
}, auth=auth).get("result", [{}])[0]
expr = tr.get("expression", "")
print(f"\nTrigger {tr_id} ({tr.get('description')}): current expr = {expr}")
# Replace 5m with 15m in the expression
if "5m" in expr:
new_expr = expr.replace("5m", "15m")
# In trigger.update, Zabbix expects expression with host/item syntax if updated
# Or description update: "Linux: High CPU utilization (>95% for 15m)"
desc = tr.get("description", "")
# Update trigger
up_res = zabbix_api("trigger.update", {
"triggerid": tr_id,
"expression": new_expr
}, auth=auth)
print(f" Updated expr to 15m: {up_res}")
print("\nFinished updating Zabbix noise reduction thresholds!")