zabgrab.adapters.zabbix

Zabbix JSON-RPC API client.

class zabgrab.adapters.zabbix.HttpTransport(url, *, verify_ssl=True, timeout=30.0)[source]

Bases: object

Transport over HTTP(S) using aiohttp.

Network problems are raised as ConnectionFailed with a hint.

Parameters:
  • url (str) – The server’s frontend or API URL.

  • verify_ssl (bool, optional) – Whether to check the server’s TLS certificate.

  • timeout (float, optional) – Seconds to wait for each request.

async post(body, headers)[source]

Send a request body and return the decoded response.

Parameters:
  • body (dict) – The JSON-RPC request.

  • headers (dict of str to str) – Extra HTTP headers (e.g. Authorization).

Returns:

Any – The decoded JSON response.

Raises:
  • Redirected – If the endpoint redirects elsewhere.

  • ConnectionFailed – If the server can’t be reached or doesn’t respond like Zabbix.

Return type:

Any

async close()[source]

Close the HTTP session, if one was opened.

Return type:

None

class zabgrab.adapters.zabbix.Transport(*args, **kwargs)[source]

Bases: Protocol

Sends JSON-RPC request bodies and returns decoded responses.

async post(body, headers)[source]

Send a request body and return the decoded response.

Parameters:
  • body (dict) – The JSON-RPC request.

  • headers (dict of str to str) – Extra HTTP headers (e.g. Authorization).

Returns:

Any – The decoded JSON response.

Return type:

Any

async close()[source]

Release the connection, if any.

Return type:

None

class zabgrab.adapters.zabbix.ZabbixAPI(transport)[source]

Bases: object

A client for one Zabbix server.

Patterns passed to the search methods use the API’s wildcard search (searchWildcardsEnabled): they match the whole value, ignoring case, and * matches any text.

Parameters:

transport (zabgrab.adapters.zabbix.transport.Transport) – Sends the requests (HTTP in normal use; a fake in tests).

async call(method, params, *, auth=True)[source]

Call an API method and return its result.

Parameters:
  • method (str) – The API method, e.g. "item.get".

  • params (Any) – The method’s parameters.

  • auth (bool, optional) – Whether to send the token or session (if logged in).

Returns:

Any – The result of the response.

Raises:
Return type:

Any

async version()[source]

Get the server’s API version (e.g. "7.0.5").

Return type:

str

async check_version()[source]

Get the server’s version, checking zabgrab supports it.

Returns:

str – The version, e.g. "7.0.5".

Raises:

UnsupportedServer – If the server is older than MIN_VERSION.

Return type:

str

async login_token(token)[source]

Authenticate with an API token, checking it’s valid.

Parameters:

token (str) – The API token.

Raises:

AuthenticationFailed – If the server rejects the token.

Return type:

None

async login_password(username, password)[source]

Authenticate with a username and password.

The session is logged out again when the client is closed.

Parameters:
  • username (str) – The credentials.

  • password (str) – The credentials.

Raises:

AuthenticationFailed – If the server rejects them.

Return type:

None

async close()[source]

End any password session and close the connection.

Return type:

None

async hosts(*, pattern=None, group=None, limit=None)[source]

Find hosts.

Parameters:
  • pattern (str, optional) – A pattern for the technical or visible name (all hosts if not given).

  • group (str, optional) – Only hosts in a host group matching this pattern.

  • limit (int, optional) – The most hosts to return.

Returns:

list of Host – The hosts, sorted by visible name.

Return type:

list[Host]

async items(*, itemids=None, hostids=None, name=None, key=None, tag=None, limit=None)[source]

Find items on hosts (not templates) matching all of the given filters.

Parameters:
  • itemids (sequence of int, optional) – Only these items, or items on these hosts. Empty matches nothing.

  • hostids (sequence of int, optional) – Only these items, or items on these hosts. Empty matches nothing.

  • name (str, optional) – Patterns for the item’s name and key.

  • key (str, optional) – Patterns for the item’s name and key.

  • tag (str, optional) – "name" (the tag exists) or "name:value" (with exactly that value).

  • limit (int, optional) – The most items to return.

Returns:

list of Item – The items, sorted by name.

Return type:

list[Item]

async history(item, start, end)[source]

Get an item’s raw values.

Parameters:
  • item (Item) – The item.

  • start (int) – The time range in POSIX seconds (inclusive).

  • end (int) – The time range in POSIX seconds (inclusive).

Returns:

list of HistoryRow – The values, oldest first.

Return type:

list[HistoryRow]

async trends(item, start, end)[source]

Get an item’s hourly trends.

Parameters:
  • item (Item) – The item (numeric).

  • start (int) – The range of hour starts, in POSIX seconds (inclusive).

  • end (int) – The range of hour starts, in POSIX seconds (inclusive).

Returns:

list of TrendRow – The hours, oldest first.

Return type:

list[TrendRow]

zabgrab.adapters.zabbix.api_endpoint(url)[source]

Get the JSON-RPC endpoint for a server URL.

Parameters:

url (str) – The frontend’s base URL (https://example.org/zabbix) or the endpoint itself (.../api_jsonrpc.php).

Returns:

str – The endpoint URL.

Return type:

str

Modules

api

Zabbix 7 JSON-RPC API client.

transport

HTTP transport for the Zabbix JSON-RPC API.