Zabbix Maintenance Window: Check via Python Script

Apa Script Ni

Python script yang call Zabbix API untuk check maintenance windows. Nak tengok schedule maintenance yang active, upcoming, atau dah lepas.

How It Works

import requests
from datetime import datetime

ZABBIX_URL = "https://your-zabbix/api_jsonrpc.php"
API_TOKEN = "your-token"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_TOKEN}"
}

# Get all maintenance windows
payload = {
    "jsonrpc": "2.0",
    "method": "maintenance.get",
    "params": {
        "output": "extend",
        "selectGroups": "extend",
        "selectHosts": "extend",
        "selectTimeperiods": "extend"
    },
    "id": 1
}

response = requests.post(ZABBIX_URL, json=payload, headers=headers)
maintenances = response.json()["result"]

for m in maintenances:
    name = m["name"]
    active_since = datetime.fromtimestamp(int(m["active_since"]))
    active_till = datetime.fromtimestamp(int(m["active_till"]))
    mtype = "With data collection" if m["maintenance_type"] == "0" else "No data collection"
    
    print(f"Name: {name}")
    print(f"  From: {active_since}")
    print(f"  Till: {active_till}")
    print(f"  Type: {mtype}")
    print(f"  Hosts: {[h['host'] for h in m.get('hosts', [])]}")
    print()

Output

Name: Wiki Weekly Maintenance
  From: 2025-12-01 02:00:00
  Till: 2025-12-01 04:00:00
  Type: No data collection
  Hosts: ['wiki-prod1', 'wiki-prod2']

Name: Wiki DB Upgrade Window
  From: 2025-12-07 22:00:00
  Till: 2025-12-08 02:00:00
  Type: With data collection
  Hosts: ['wiki-db1']

Useful Filters

Nak filter by host group je:

"params": {
    "groupids": ["15"],  # host group ID
    "output": "extend",
    "selectTimeperiods": "extend"
}

Nak active maintenance je (yang tengah jalan sekarang):

import time
now = int(time.time())

active = [m for m in maintenances 
          if int(m["active_since"]) <= now <= int(m["active_till"])]

Notes

  • maintenance_type: 0 = with data collection (monitoring still runs, just no alerts), 1 = no data collection
  • timeperiods define the actual schedule within the active window
  • Useful untuk verify maintenance dah set betul sebelum patching

Script macam ni eventually jadi part of my MCP server — where AI assistants boleh query Zabbix maintenance windows directly tanpa perlu run script manual.

Leave a Comment