zabgrab.services.session

Connecting and logging in to a Zabbix server.

zabgrab.services.session.UpdateServerUrl

Changes a configured server’s URL (name, new URL) in the config file that defines it.

Type:

type

zabgrab.services.session.MAX_REDIRECTS

The most redirects followed in a row.

Type:

int

Functions

ask_for_token(client, ui, *[, fix])

Ask for a token until the server accepts one.

connect(server, *, factory, ui, secrets, env)

Connect to a server and log in.

find_token(server, secrets, env)

Find the token to use for a server.

non_empty(text)

Parse an answer that can't be empty, stripping spaces.

open_client(server, *, factory, ui[, update_url])

Connect to a server (without logging in) and check its version.

token_fix(server)

Explain how to give zabgrab a token for a server without being asked.

Classes

TokenLookup(token, source)

A server's token, and where it comes from.

class zabgrab.services.session.TokenLookup(token, source)[source]

Bases: object

A server’s token, and where it comes from.

Parameters:
  • token (str | None)

  • source (str)

token

The token, if there is one.

Type:

str or None

source

Where from, for messages: $VARIABLE or your keyring.

Type:

str

zabgrab.services.session.find_token(server, secrets, env)[source]

Find the token to use for a server.

Parameters:
  • server (Server) – The server.

  • secrets (SecretStore) – The stored tokens.

  • env (mapping of str to str) – Environment variables.

Returns:

TokenLookup – Its token_env variable’s value if set, otherwise the keyring’s token.

Return type:

TokenLookup

zabgrab.services.session.token_fix(server)[source]

Explain how to give zabgrab a token for a server without being asked.

Parameters:

server (Server)

Return type:

str

async zabgrab.services.session.ask_for_token(client, ui, *, fix='')[source]

Ask for a token until the server accepts one.

Parameters:
  • client (ZabbixClient) – The server’s client; it’s logged in when this returns.

  • ui (UI) – Asks for the token.

  • fix (str, optional) – How to give the token without being asked.

Returns:

str – The accepted token.

Raises:

InputRequired – If the session isn’t interactive.

Return type:

str

zabgrab.services.session.non_empty(text)[source]

Parse an answer that can’t be empty, stripping spaces.

Parameters:

text (str)

Return type:

str

async zabgrab.services.session.connect(server, *, factory, ui, secrets, env, update_url=None)[source]

Connect to a server and log in.

If the server redirects (e.g. from HTTP to HTTPS), interactive users are asked whether to follow, and whether to update the server’s URL in their config via update_url; otherwise the redirect is an error.

Tokens come from the server’s token_env variable, then the keyring. If there is none (or it’s rejected), interactive users can paste a token or log in with a password; otherwise an error explains how to provide one.

Parameters:
  • server (Server) – The server.

  • factory (ClientFactory) – Creates the client.

  • ui (UI) – Asks about redirects and logging in.

  • secrets (SecretStore) – The stored tokens.

  • env (mapping of str to str) – Environment variables (for token_env).

  • update_url (callable, optional) – Offered to update a configured server’s URL after a redirect.

Returns:

ZabbixClient – The logged-in client (close it when done).

Raises:
Return type:

ZabbixClient

async zabgrab.services.session.open_client(server, *, factory, ui, update_url=None)[source]

Connect to a server (without logging in) and check its version.

Redirects are handled as described for connect().

Parameters:
  • server (Server) – The server.

  • factory (ClientFactory) – Creates the client.

  • ui (UI) – Asks about redirects.

  • update_url (callable, optional) – Offered to update a configured server’s URL after a redirect.

Returns:

  • ZabbixClient – The client, not logged in.

  • Server – The server, with its new URL if redirected.

  • str – The server’s Zabbix version.

Raises:
  • ConnectionFailed – If the server can’t be reached, redirects too often, or redirects non-interactively.

  • UnsupportedServer – If its Zabbix version is too old.

Return type:

tuple[ZabbixClient, Server, str]