This document describes the overall architecture of the zbx toolkit, its runtime flow, core components, and extension points.
- Git‑style CLI: a single
zbxdispatcher with manyzbx-*subcommands onPATH. - Small, composable Bash scripts that speak Zabbix JSON‑RPC via
curland shape results withjq. - Conventionally outputs TSV for easy piping; some commands can output raw JSON.
- Configuration resolved from environment and well‑known locations; managed via
zbx config.
-
bin/zbx(dispatcher)- Discovers subcommands by scanning
PATHfor executables namedzbx-*. - Delegates execution:
zbx <sub>→exec zbx-<sub>. - Help system groups commands by area (Hosts, Templates, Macros, Problems & Triggers, Maintenance, Items/History/Trends, Discovery & Inventory, Search, Config, Low‑level, Other).
- Extracts a one‑line description from each subcommand’s
-h/--helpoutput when available; falls back to a built‑in description map. - Utility flags:
zbx help,zbx help <cmd>,zbx --list,zbx --where <cmd>. - Global flags (
--insecure,--cacert,--capath) are parsed from anywhere on the CLI (before/after the subcommand) and applied prior to dispatch. - Unknown commands produce a concise error and advise
zbx help(no “closest matches” banner).
- Discovers subcommands by scanning
-
bin/zbx-lib(core library)- Config discovery (see Configuration) and sourcing of the active config file.
- Defaults for unset variables:
ZABBIX_URL,ZABBIX_USER,ZABBIX_PASS,ZABBIX_API_TOKEN,ZABBIX_VERIFY_TLS,ZABBIX_TOKEN_FILE,ZABBIX_CURL_TIMEOUT. - Minimal logging shims if
log-libwasn’t sourced first. - HTTP helper
_zbx_curl_commoncentralizes curl options (TLS verify, timeouts, headers). - Authentication:
- If
ZABBIX_API_TOKENis set → use Bearer token (nouser.login). - Else perform
user.loginwithZABBIX_USER/ZABBIX_PASSand cache the session token inZABBIX_TOKEN_FILE(umask 077). - On “Session terminated/Not authorised” errors, auto re‑login once and retry.
- If
- JSON‑RPC helpers:
zbx_call_raw <method>reads params JSON on stdin and returns raw response JSON.zbx_call <method>adds retry-on-expired‑session logic.
zbx_config_pathsurfaces which config file is active.
-
bin/log-lib(logging)- Environment‑controlled logging:
LOG_LEVEL(error|warn|info|debug) and optionalLOG_FILE. - Timestamped structured stderr/file output with helpers
log_error,log_warn,log_info,log_debug.
- Environment‑controlled logging:
-
Subcommands (
bin/zbx-*)- Small Bash scripts that source
log-libandzbx-lib, build a request object withjq -n, callzbx_call <api.method>, and post‑process withjq. - Typical outputs are TSV/CSV/JSON; many list/search commands support
--format {tsv|csv|json}and--headersand rely on a shared formatter inzbx-lib. - Examples:
- Hosts:
zbx-hosts-list,zbx-host-get,zbx-host-create,zbx-host-enable,zbx-host-disable,zbx-host-del,zbx-host-groups. - Templates:
zbx-template-list,zbx-template-link,zbx-template-unlink. - Macros:
zbx-macro-get,zbx-macro-set,zbx-macro-del,zbx-macro-bulk-set. - Problems/Triggers:
zbx-problems,zbx-triggers,zbx-trigger-enable,zbx-trigger-disable. - Maintenance:
zbx-maint-create,zbx-maint-list,zbx-maint-del. - Data:
zbx-item-find,zbx-history,zbx-trends,zbx-discovery,zbx-inventory. - Auth/Health:
zbx-login,zbx-ping,zbx-version. - Search:
zbx-search(multi‑entity fuzzy/substring search; supports--json).
- Hosts:
- Small Bash scripts that source
-
zbx-config(config management)- Self‑contained script (does not require
log-lib) that manages a single config file with a managed overrides block at the top. - Supports
list,get,set,unset,init,edit,pathand scopes: project (./config.sh), user (~/.config/zbx/config.sh), system (/etc/zbx/config.sh), or auto. - Redacts secrets by default for
get/listunless--rawis set.
- Self‑contained script (does not require
Resolution order (first existing file wins):
ZBX_CONFIGenv var → explicit path- Project:
../config.shrelative tobin/ - CWD:
./config.sh - User:
${XDG_CONFIG_HOME:-$HOME/.config}/zbx/config.sh - System:
/etc/zbx/config.sh
Variables of interest:
ZABBIX_URL— API endpoint URLZABBIX_USER,ZABBIX_PASS— credentials (if not using token)ZABBIX_API_TOKEN— static API token, enables Bearer authZABBIX_VERIFY_TLS—1(default) or0to disable certificate verificationZABBIX_CURL_TIMEOUT— connect and max timeouts (seconds)ZABBIX_TOKEN_FILE— path for cached session token (default:.zabbix_session.token)
-
zbx hosts-listzbxresolves andexecszbx-hosts-list.- Script sources
log-libandzbx-lib→ config load, auth ready. - Builds
{output:["hostid","host"], ...}and callszbx_call host.get. - Prints TSV:
hostid<TAB>hostper line.
-
zbx search items --host web01 nginx(regex default)- Resolves hostid via
host.get. - Requests items with
limitand optionalsearch(if--like). - Filters client‑side with
jq test(re, "i")when regex mode. - Outputs TSV or JSON depending on
--json.
- Resolves hostid via
- Shell: Bash,
set -euo pipefail, scripts#!/usr/bin/env bash. - Data shaping:
jqeverywhere; prefer numeric conversion (tonumber) for ids/times. - Output: TSV by default; raw JSON optional on some commands.
- Logging: stderr with levels; quiet by default.
- Extensibility: drop an executable
zbx-<name>onPATHto add a command; it will show up inzbx helpautomatically.
make installinstalls dispatcher,zbx-*scripts, and libraries to${PREFIX:-/usr/local}/binand ensures/etc/zbx/config.shexists.make install-userinstalls to~/.local/binand ensures~/.config/zbx/config.shexists; appends PATH helper to common shell RCs.make gen-completionsgenerates Bash completions undercompletions/;make install-completionsinstalls them to standard system/user paths.make checkruns ShellCheck (when available);make doctorrunszbx doctorwith localbin/onPATH.
- Integration tests require
ZABBIX_URLand auth set to pass; they are intentionally failing otherwise.