Skip to main content

Programmatic Zabbix template generation — Monitoring as Code

Project description

zbxtemplar

A Pythonic framework for programmatic Zabbix configuration generation — Monitoring as Code.

Define templates, hosts, user groups, users, and actions as Python code. Generate Zabbix-native YAML (importable via UI or API) and decree YAML (applied by the executor). The goal is to cover the essential Zabbix configuration primitives — not every possible option. If you need a field that isn't exposed, raw dicts and string expressions give you an escape hatch.

Aimed at teams that want:

  • monitoring configuration in git, reviewable in PRs
  • readable, programmable definitions instead of large generated exports
  • a lightweight way to manage users, permissions, and alert-routing state
  • confidence that deploying monitoring configuration cannot accidentally leak credentials, partially apply state, or silently ignore misconfiguration

Why

Zabbix is powerful, but its configuration is not pleasant to version, review, or evolve in code. Terraform and Ansible can manage Zabbix — they bring ceremony and templating complexity that often outweigh the problem. Monitoring is combinatorial: an application across a dozen regions, each with a set of queues, each queue needing several items, a dashboard per region, an overview graph. Hundreds of objects, nearly identical but each distinct. In HCL or Jinja, the template becomes harder to read than the output. In zbxtemplar, it is a loop and a set of parameters — plain Python any developer on the team can read.

The Zabbix UI handles one-off setup fine. The trouble starts when you need the same action across dev, staging, and prod. When someone edits an alert filter and nobody notices until production goes silent. When "for each team, create a scoped alert" means N manual repetitions with N chances to get it wrong. That does not scale — which forces you into code.

Once you are there, secrets need handling. ${ENV_VAR} placeholders keep credentials out of git; a missing variable is a hard abort, not an empty string applied to a live instance. Zabbix secret and vault macro types are first-class. Host encryption (PSK, TLS certificates) and token provisioning — things that are clunky or impossible to automate from the web interface — are managed declaratively with the same strict contract (doc/security.md).

Actions are where the Zabbix API gets awkward and error-prone: numeric codes for everything, manual formula labels, invalid operator-condition combinations accepted without complaint. zbxtemplar replaces that with typed Python — HostGroupCondition("Production") & SeverityCondition("HIGH"). Names, not IDs. Wrong operator on the wrong condition type? Type error at write time, not a silent misfire during an incident (doc/actions.md).

On top of all this, Context validates references at generation time — against previously generated or exported YAML — so a typo in a host group name or a missing template is caught before any artifact is written. Deterministic UUIDs prevent import duplicates. Mistakes break against your code, not against production (doc/generator.md).

What It Does

zbxtemplar has three main pieces:

  • TemplarModule generates Zabbix-native YAML for templates and hosts
  • DecreeModule generates decree YAML for users, user groups, and actions
  • zbxtemplar-exec applies generated artifacts to a live Zabbix instance

The split is intentional:

  • monitoring objects fit well into Zabbix's native import/export model
  • user management and action state often need API-driven apply logic
  • both outputs stay reviewable as plain YAML artifacts

Quick Example

from zbxtemplar.core import TemplarModule
from zbxtemplar.zabbix import Template, Item, Host, TriggerPriority
from zbxtemplar.zabbix.Template import TemplateGroup
from zbxtemplar.zabbix.Host import HostGroup, AgentInterface


class MyModule(TemplarModule):
    def __init__(self, alert_threshold: int = 90):
        super().__init__()

        template = Template(
            name="My Service",
            groups=[TemplateGroup("Custom Templates")],
        )
        template.add_macro("THRESHOLD", alert_threshold, "Alert threshold")

        item = Item("CPU Usage", "system.cpu.util", template.name)
        item.add_trigger(
            "High CPU",
            "last",
            ">",
            template.get_macro("THRESHOLD"),
            priority=TriggerPriority.HIGH,
        )
        template.add_item(item)

        host = Host("My Server", groups=[HostGroup("Linux Servers")])
        host.add_template(template)
        host.add_interface(AgentInterface(ip="192.168.1.10"))

        self.add_template(template)
        self.add_host(host)

Generate YAML:

zbxtemplar my_module.py -o monitoring.yml

Apply it:

zbxtemplar-exec apply monitoring.yml \
  --url https://zabbix.example.com \
  --token "$ZABBIX_TOKEN"

Installation

Install the generator:

pip install .

Install the executor as well:

pip install '.[executor]'

Python 3.9+ is required.

Typical Workflow

  1. Write a Python module using TemplarModule or DecreeModule.
  2. Generate YAML with zbxtemplar.
  3. Review the generated artifacts in git.
  4. Validate against a test Zabbix instance.
  5. Apply to production with zbxtemplar-exec when ready.

This project assumes a test-environment-first workflow rather than a separate dry-run engine.

Documentation

The structured docs live in doc/:

Current Scope

zbxtemplar is already useful, but it is still a working tool rather than a polished platform.

Good fit:

  • teams that want reviewable monitoring definitions in Python
  • repositories where monitoring config should live close to service code
  • environments where users, permissions, and alert-routing changes need to be scripted cleanly

Things to know:

  • the docs in doc/ are the public technical reference
  • the project is intentionally opinionated about the main workflow
  • the executor is practical, but not presented as a fully hardened unattended deployment system

License

MIT

Project details


Download files

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

Source Distribution

zbxtemplar-0.8.0.tar.gz (49.9 kB view details)

Uploaded Source

Built Distribution

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

zbxtemplar-0.8.0-py3-none-any.whl (46.3 kB view details)

Uploaded Python 3

File details

Details for the file zbxtemplar-0.8.0.tar.gz.

File metadata

  • Download URL: zbxtemplar-0.8.0.tar.gz
  • Upload date:
  • Size: 49.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zbxtemplar-0.8.0.tar.gz
Algorithm Hash digest
SHA256 533d0cfa5a10141046bae29e97442ba05d2ec2fa6e4ff79198a4edf61cd647da
MD5 01206e82165246ded7d77b3206745615
BLAKE2b-256 7d4428efbad79cf1fd68ff14c5e0400eba46d2233a142901d40ad4c51633af3c

See more details on using hashes here.

File details

Details for the file zbxtemplar-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: zbxtemplar-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 46.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zbxtemplar-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 020c0175d5b4eb64967a70647f37debc7a5b31efd871f90b0d22dc569f2af8d4
MD5 211995c57d466a8bb24c1afbc27ed40c
BLAKE2b-256 89ed8ef77883a3e3af3edd1115319465047b70c06e209babc286fdad1f15dee5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page