Skip to content

The API — Zabbix without the browser

Status: verified

Everything the web pages show can be asked in text, and everything they do can be done in text. This chapter is the proof tour: the same objects the earlier chapters built — a host, an item, a trigger, an alert, a user — created and read back through the API, finishing with its refusals, which are the part of an API you end up trusting most.

The shape never changes. One endpoint, api_jsonrpc.php; JSON-RPC 2.0 envelopes; a method, a params object, an id you choose. Methods come in families per object — host.get, host.create, host.update, host.delete — and the object names run across the whole system: item, trigger, problem, event, alert, action, template, user, usergroup, role, mediatype, history, and a hundred more. A get call's params carry the habits worth learning: output (which fields), filter (exact matches), search (substring matches), select… (pull related objects in the same call), and sortfield / limit — the vocabulary of asking precisely.

The guide's pocket helper condenses a call to a line, and the earlier chapters have already spent it: host.get found the host group Linux servers by name and the template Linux by Zabbix agent by search (templateid 10001); host.create built lab-server (hostid 10683) with its interface and the template in one call; item.get read the item count back as 82, and read live answers — lastvalue on agent.ping, system.cpu.load[all,avg1], agent.hostname — each value the database honestly held at that second. Nothing in that paragraph was simulation: the helper's output is in transcripts t05 to t08, and any of it can be replayed against a server of your own with curl alone.

Two creations earn their own mention, because scripts are where they belong.

A trigger, created programmatically — chapter 7's gauge rule arrived exactly like this:

$ zapi.py trigger.create '{"description": "Gauge reports trouble",
  "expression": "find(/lab-server/lab.gauge,#1,\"like\",\"ALARM\")=1",
  "priority": 3, "status": 0}'
{"result": {"triggerids": ["25287"]}, "id": 1}

Provision a hundred machines and their rules this way and the rules come out identical — which is the whole reason to create objects in text: the browser is for judgement, the API is for repetition.

A problem, read programmatically — problem.get filtered by the trigger's id (objectids) returned the firing event 46, then its recovery 47, then the alert chapter's 53 and 54, each a row with a clock and a resolution pointer. A script that watches problem.get watches exactly what a human watches on the dashboard; the difference is only the coffee.

And the refusals, collected in one place as the API's character reference: a wrong password gets Incorrect user name or password or account is temporarily blocked; a limited user's host.create gets No permissions to call "host.create"; an item created without its interfaceid is refused with the field named; a user whose password contains his own name is refused with the policy quoted; a find() given a parameter shape it does not accept is refused with the grammar corrected. An API that explains its no is an API you can automate against, because the failures are legible and final — no half-created hosts, no silent skips. Transcript t09 keeps the three terminal-level versions of this character: the wrong password, the unsupported key from zabbix_get (ZBX_NOTSUPPORTED: Unknown metric), and the connection refused where nothing listens.

The check. The API is working when a script can do the guide's own loop without a browser: log in, read the version, create a host from a template, watch an item's last value move, read the problem when a trigger fires, and get an honest, rule-quoting error when it overreaches. All six are in the transcripts; the helper that does them, zapi.py, is in zabbix-lab/ for you to read before you trust it.