Skip to main content

zapi-lib

Minimal Zabbix JSON-RPC API client for Python — a thin, httpx-only wrapper around the single /api_jsonrpc.php endpoint.

Spun out of zapi-mcp so that tools which only need the API client — e.g. speedtest-z — can depend on it without pulling in the MCP server stack (mcp, starlette, uvicorn, …).

Features

  • Version-adaptive auth: user + auth field (Zabbix ≤ 6.2) and username + Authorization: Bearer (6.4 / 7.0), degrading to the proven path automatically.
  • Read helpers: get_hosts, get_items, get_problems, count_problems, get_events, get_maintenances.
  • Write helpers: set_host_tag (idempotent host-tag upsert that preserves other tags), acknowledge_problem, set_maintenance / set_maintenance_for_hosts (idempotent maintenance windows, by location tag or by exact host name).
  • Provisioning (ZapiProvisioner): config-driven client that auto-creates trapper hosts/items stamped with a managed-by tag — create_host, update_host, create_item, update_item, plus ensure_group / get_host_ids / get_item_ids.
  • Escape hatch: call(method, params) invokes any JSON-RPC method directly.
  • A single runtime dependency: httpx.

Install

pip install zapi-lib

Usage

from zapi_lib import ZapiClient, tag_filter

with ZapiClient("https://zabbix.example.com", "api-user", "api-pass") as z:
    hosts = z.get_hosts(tags=[tag_filter("speedtest-z")])
    z.set_host_tag("eduroam", "speedtest-z", "0.10.0")

The URL may be given with or without the /api_jsonrpc.php suffix.

set_maintenance(location, since, till, name, description) opens an idempotent maintenance window over hosts carrying a matching location tag; set_maintenance_for_hosts(hosts, since, till, name, description) does the same over an explicit list of exact host (technical) names instead, for when the affected hosts don't share a tag or precise host-level control is wanted. Both are plain ZapiClient methods — no provisioning config required.

Both accept a keyword-only overwrite=False. The window name is name + the start time, so re-announcing the same planned outage with a corrected end time collides with the window already created for it. By default that collision is a no-op (the historical idempotent behaviour) and the correction is silently dropped. Pass overwrite=True to treat the collision as a correction and update active_since/active_till/timeperiods/ description in place instead. hostids is never sent on update, so an overwrite moves the schedule but never re-targets the window, and a repeat call with identical values still issues no write at all.

Note what overwrite does not cover: the start time is part of the window name, so a re-announcement that moves the start produces a different name, creates a second window, and leaves the earlier one in place. Two overlapping windows over-suppress rather than under-suppress, so it is the milder failure, but the stale window has to be removed by hand.

Only enable it where name + start time provably identifies one real-world event — a name generated from a site and its start time qualifies; a free-text name chosen per call does not, because two unrelated maintenances can then share a name and overwriting would silently reschedule someone else's window. get_maintenances() reads all maintenance windows back (hosts, host groups, time periods, tags) as raw API rows; deciding whether a given window is currently active is left to the caller.

Provisioning (config-driven)

ZapiProvisioner extends ZapiClient for metric-collection scripts that register the targets they push values to. It reads connection and provisioning defaults from a config.ini [zabbix] section and auto-creates trapper hosts/items tagged with a managed-by marker:

[zabbix]
url      = https://zabbix.example.com/api_jsonrpc.php
id       = api-user        ; or `user`
pw       = api-pass        ; or `password`
group    = DefaultGroup    ; default host group for created/updated hosts
location = tokyo           ; optional; added as a `location` tag
tag      = my-collector    ; optional; managed-by marker tag on hosts/items
from zapi_lib import ZapiProvisioner

with ZapiProvisioner.from_config() as z:  # ./config.ini, then ~/.config.ini
    z.show_version()
    host_ids = z.get_host_ids("pool-a") or z.create_host("pool-a", location="tokyo")
    host_id = host_ids[0]
    if not z.get_item_ids(host_id, "usage"):
        z.create_item(host_id, "usage", value_type=0)

update_host replaces the host's groups and tags (use set_host_tag to upsert a single tag instead).

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zapi_lib-0.8.1.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zapi_lib-0.8.1-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file zapi_lib-0.8.1.tar.gz.

File metadata

  • Download URL: zapi_lib-0.8.1.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for zapi_lib-0.8.1.tar.gz
Algorithm Hash digest
SHA256 8cffab1206e8ab1b9626ee9ac7b0a7bc43d6db90d41a733a28b03d96df82c008
MD5 617c3719c1c963dbc7e79da34b6d8d39
BLAKE2b-256 6c97324a950010acdef372c4fb6b26bd6df95bfb2640c3d8b8c9b2555e8ccf18

See more details on using hashes here.

Provenance

The following attestation bundles were made for zapi_lib-0.8.1.tar.gz:

Publisher: release.yml on shigechika/zapi-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zapi_lib-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: zapi_lib-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for zapi_lib-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ac7e662ba756995f5822fc69938fd98cb6a4f1d1aaff44c5f882dc5de57d7daf
MD5 f01ab986932afb028ed3f7fd68118465
BLAKE2b-256 ce13ef67ac822cf3fa22c4ea89b51fce8c49f86f4fec27c5fb9064a8dad2e1fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for zapi_lib-0.8.1-py3-none-any.whl:

Publisher: release.yml on shigechika/zapi-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 files

0.8.0

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page