Operations 14 min read

Automate Database Monitoring with Zabbix and Ansible – A Complete Guide

This article walks through building an automated database monitoring system using Zabbix and Ansible, covering Zabbix core automation features (LLD, API, trapper), standardizing monitoring conventions, deploying with Ansible, and implementing a Python‑based DBA_Monitor project for Oracle, MySQL, Redis, and MongoDB.

dbaplus Community
dbaplus Community
dbaplus Community
Automate Database Monitoring with Zabbix and Ansible – A Complete Guide

Introduction

Database servers such as Oracle, MySQL, Redis and MongoDB require automated monitoring to reduce manual effort. Zabbix provides monitoring capabilities while Ansible enables automated deployment.

Zabbix Automation Core Features

Low‑Level Discovery (LLD)

LLD discovers dynamic objects (e.g., newly deployed Redis instances) and automatically creates Items and Triggers. The discovery payload is a JSON document sent to the Zabbix server. Example Redis Standalone discovery JSON: {"data":[{"{#REDIS_PORT}":6379}]} After linking the Redis template, zabbix_sender transmits the data and the Item is created automatically.

JSON‑RPC API

Zabbix exposes a JSON‑RPC API for creating or updating Hosts, Items, Triggers, etc. A typical request includes the fields jsonrpc , method , params , id and auth . Example user.login request:

{
  "jsonrpc": "2.0",
  "method": "user.login",
  "params": {"user": "admin", "password": "secret"},
  "id": 1,
  "auth": null
}

Python projects often use py‑zabbix (https://github.com/adubkov/py-zabbix) or custom wrappers based on urllib / requests to call the API.

Zabbix Trapper

The trapper method sends data directly to the Zabbix server without a Zabbix agent, reducing load on monitored databases. Use zabbix_sender with a JSON payload or the py‑zabbix wrapper.

Monitoring Automation

Automation aims to eliminate repetitive, low‑risk monitoring tasks. Key practices include:

Standard host‑naming conventions.

Consistent IP selection for monitoring.

Unified deployment directories for Zabbix agents and scripts.

Defined alarm levels for routing and automated handling.

Ansible‑Based Deployment

Ansible (Python‑based) executes playbooks over SSH from a control machine. Core components:

Ansible : engine.

Core Modules : built‑in modules.

Custom Modules : user‑defined modules.

Plugins : mail, log, connection plugins.

Playbooks : YAML files defining ordered tasks.

Host Inventory : list of managed hosts with connection details.

Example main.yml playbook that pushes the DBA_Monitor package:

- hosts: "{{ host_list }}"
  gather_facts: true
  roles:
    - role: "{{ role_name }}"

After deployment, Auto‑Register binds hosts to the appropriate Zabbix templates.

Database Monitoring Project (DBA_Monitor)

The project is a unified Python package for monitoring Oracle, MySQL, Redis and MongoDB. Directory layout:

DBA_Monitor/
├─ authzabbix.py        # shared Zabbix API wrapper
├─ Lib/                # global variables (API URLs, credentials, paths)
├─ Log/                # monitoring logs
├─ Template/           # Zabbix template files
├─ oracle/pyora.py     # Oracle monitoring script (uses Pyora)
├─ mysql/mysql_db_status.py   # MySQL monitoring script (uses MySQLdb)
├─ redis/redis_db_status.py   # Redis monitoring script (uses python‑redis)
└─ mongo/mongo_db_status.py   # MongoDB monitoring script (uses PyMongo)

Oracle Monitoring

Implemented with Pyora (https://github.com/bicofino/Pyora), which uses cx_Oracle for connections and Python reflection to add custom metrics. Adding a new check (e.g., user_status) only requires a new method in the Checks class.

MySQL Monitoring

Uses MySQLdb to query SHOW GLOBAL VARIABLES, SHOW GLOBAL STATUS and SHOW SLAVE STATUS. Results are stored in a Python dict and sent to Zabbix as trapper items via zabbix_sender.

Redis Monitoring

Python‑Redis’s INFO command retrieves metrics for both Standalone and Cluster modes. Sentinel monitoring requires LLD to discover Sentinel instances, then extracting master information with INFO sentinel. Macros {#REDISPORT} and {#SENTINELMASTER} are used to create Items.

MongoDB Monitoring

Targets sharded clusters (mongos, mongod, config server). LLD discovers instances, then the appropriate template is applied. Data is collected via PyMongo using serverStatus and replSetGetStatus (filtered for mongos). Instance discovery parses netstat output, assembles a list of hosts, and sends data with zabbix_sender.

Conclusion

Combining Zabbix with Ansible enables one‑click deployment of monitoring agents, configuration, host registration and template assignment. The workflow can be integrated with a CMDB for fully automated database monitoring, significantly improving DBA efficiency and reducing manual effort.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonautomationAnsibleZabbixDatabase MonitoringLLD
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.