Comprehensive Guide to Zabbix Monitoring Infrastructure and Advanced Configuration
Zabbix Architecture and Core Components
Zabbix is an enterprise-grade open-source monitoring solution designed for distributed systems and network monitoring. It provides a robust framework to track various network parameters, server health, and application integrity via a web-based management interface.
Key Components
- Zabbix Server: The central engine that handles data reporting from agents, calculates triggers, and manages notifications.
- Database Storage: A relational database (typically MySQL, PostgreSQL, or Oracle) that stores all configuration and historical performance data.
- Web Interface: A PHP-based GUI used for configuration, visualization, and administration.
- Proxy: An optional component that collects data from monitored devices on behalf of the server to reduce the server's load in distributed environments.
- Agent: Software deployed on monitoredd targets to collect local resources (CPU, Memory, Disk) and application metrics.
Essential Processes
zabbix_server: The main daemon of the Zabbix software.zabbix_agentd: The client-side daemon for local resource monitoring.zabbix_get: A utility used to pull data from an agent for troubleshooting purposes.zabbix_sender: A tool used to push data to the server, often used for long-running scripts or asynchronous tasks.zabbix_java_gateway: A dedicated gateway for monitoring Java applications via JMX.
System Environment Preparation
Before installing Zabbix, a standard LNMP (Linux, Nginx, MySQL, PHP) stack is required. Ensure essential libraries for image processing, XML handling, and network communication are present.
# Install mandatory dependencies
yum install -y pcre-devel zlib-devel libxml2-devel bzip2-devel openssl-devel \
net-snmp-devel curl-devel libjpeg-devel libpng-devel freetype-devel gcc make mysql-devel
PHP Configuration for Zabbix
The Zabbix frontend requires specific PHP settings to function correctly. Edit your php.ini with the following values:
max_execution_time = 300
max_input_time = 300
memory_limit = 256M
post_max_size = 32M
upload_max_filesize = 16M
date.timezone = Asia/Shanghai
always_populate_raw_post_data = -1
Installing and Configuring Zabbix Server
Compilation and Installation
./configure --prefix=/usr/local/zabbix \
--with-mysql \
--with-net-snmp \
--with-libcurl \
--enable-server \
--enable-agent \
--enable-proxy \
--with-libxml2 \
--enable-java
make && make install
Database Initialization
Create the Zabbix database and import the default schema and data:
mysql -u root -p
CREATE DATABASE zabbix CHARACTER SET utf8 COLLATE utf8_bin;
GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix_user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
# Import schema files in this specific order
mysql -uzabbix_user -p zabbix < database/mysql/schema.sql
mysql -uzabbix_user -p zabbix < database/mysql/images.sql
mysql -uzabbix_user -p zabbix < database/mysql/data.sql
Advanced Trigger Functions and Expressions
Triggers are logical expressions used to define problem states. Common functions include:
avg(sec|#num): Calculates the average value over a time period or number of samples.last(#num): Retrieves the N-th most recent value.last(0)is the current value.nodata(sec): Returns 1 if no data was received within the specified period (useful for availability checks).diff(): Returns 1 if the current value differs from the previuos one.max()/min(): Returns the maximum or minimum value in the evaluation window.
Example: Memory availability alert (< 20MB)
{Template_Linux_OS:vm.memory.size[available].last()}<20M
Service Monitoring Implementations
Nginx Monitoring
Enable the stub_status module in Nginx to expose internal metrics.
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
Example script to fetch Nginx metrics:
#!/bin/bash
METRIC=$1
URL="http://127.0.0.1/status"
case $METRIC in
active)
curl -s "$URL" | awk '/Active/ {print $NF}' ;;
requests)
curl -s "$URL" | awk 'NR==3 {print $3}' ;;
*)
echo "Usage: $0 {active|requests}" ;;
esac
MySQL Monitoring
Using mysqladmin to monitor database health:
#!/bin/bash
# mysql_health.sh
ACTION=$1
USER="zabbix_mon"
PASS="mon_password"
case $ACTION in
uptime)
mysqladmin -u$USER -p$PASS status | cut -f2 -d":" | cut -f1 -d"T" | xargs ;;
queries)
mysqladmin -u$USER -p$PASS status | cut -f4 -d":" | cut -f1 -d"S" | xargs ;;
ping)
mysqladmin -u$USER -p$PASS ping | grep -c alive ;;
esac
Java (Tomcat) Monitoring via JMX
Enable JMX in Tomcat by modifying catalina.sh:
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Djava.rmi.server.hostname=SERVER_IP"
Zabbix Server must have the JavaGateway parameter configured and pointing to the Zabbix Java Gateway service.
PHP-FPM Monitoring
Enable the status path in php-fpm.conf:
pm.status_path = /php-status
Nginx must proxy this request to the PHP-FPM socket:
location = /php-status {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
Active vs. Passive Agent Modes
Understanding communication modes is critical for performance tuning:
- Passive Mode (Default): Zabbix Server requests data from the Agent on port 10050. The server initiates the connection.
- Active Mode: The Agent initiates a connection to the Server (port 10051), requests the list of items to monitor, and pushes data. This is significantly more scalable for large environments.
To enable Active Mode, update zabbix_agentd.conf:
StartAgents=0 # Disables passive listening
ServerActive=zabbix.server.com
Hostname=Local_Server_Name
User Parameters (Custom Keys)
Custom metrics can be added using the UserParameter directive in the agent configuration:
# Syntax: UserParameter=key,command
UserParameter=custom.memory.used,free | awk '/Mem:/ {print $3}'
# Parameterized version
UserParameter=service.check[*],systemctl is-active $1
Notification and Alerts
Zabbbix supports various alerting mechanisms including custom scripts, Webhooks, and Email. Alert scripts should be placed in the AlertScriptsPath defined in zabbix_server.conf.
A typical alert script receives three arguments from Zabbix:
- Recipient address (Email/Phone/User ID)
- Subject (Trigger status)
- Message Body (Detailed event information)