Install Zabbix with Docker and configure custom monitoring items
1. Introduction to Zabbix
Zabbix can be used to monitor various network parameters to ensure the safe operation of servers and systems. And Zabbix also provides a flexible notification mechanism to allow system administrators to quickly locate/solve various problems. It is an enterprise-level open source solution that provides distributed system monitoring and network monitoring functions based on a Web interface.
1. Monitoring function
- Host performance monitoring, network device performance monitoring, database performance monitoring, multiple alarm methods, and detailed report graphs.
- To monitor the host, we can use the Agent software provided by Zabbix to monitor Linux, Windows, FreeBSD and other systems.
- To monitor network devices, we can monitor through the SNMP protocol supported by Zabbix (SSH protocol is not commonly used)
1) Monitoring objects
- Devices: Servers, routers, switches.
- Software: OS, Network, Apps.
2) Monitoring of host performance indicators
- Fault monitoring: downtime, service unavailable, host unreachable.
2. How Zabbix works
First, we need to install the Agent software on the monitored host to collect various data information of the current server and send it to the monitoring terminal. When the monitoring terminal receives the information sent by the Agent, it will store the data in the database. , and then display and plot the data on the front-end page through the Web interface.
Here the agent collects data into two modes: active and passive:
- Active: The client actively pushes the collected information to the monitoring terminal by connecting to port 10051 of the monitoring terminal.
- Passive: The monitoring terminal communicates regularly by connecting to the 10050 port of the client to collect various data information of the client.
The default mode used by Zabbix is passive mode , which means that when we need to monitor a large number of hosts, it may affect the performance of the monitoring terminal. Because the monitoring terminal will periodically collect data through the port connected to the client, we can selectively configure some hosts to be in active mode to reduce the performance pressure of the monitoring terminal.
3. Zabbix components
components | effect |
---|---|
Zabbix Server |
It is used to receive the information sent by the Agent. All configuration, data statistics, and data operations are organized by it. |
Database Storage |
Used to store all configuration information and collected data information |
Web Interface |
Zabbix's GUI interface, which can be used for interface display (generally and Server run on the same host) |
Agent |
Used to collect local data information, that is, the so-called monitored terminal |
Proxy |
Optional component, usually used in distributed monitoring architecture; it is equivalent to an agent server, which is used to collect part of the data of the agent and forward it to the monitoring terminal |
4. Zabbix process
Sender
: used to send data to Server or Proxy, usually used for time-consuming checks;Get
: Zabbix command, mainly used for troubleshooting, can be executed on the monitoring terminal to obtain the information of the monitored terminal;Agent
: Client daemon, used to collect data information of the current host, such as: CPU load, memory usage, etc.;Proxy
: The agent daemon, which is equivalent to a transfer station, can actively /passively submit the collected data information to the monitoring terminal;Server
: The monitoring side daemon for receiving dataAgent
Get
Sender
Proxy
Java_Gateway
provided by .
Because Zabbix cannot monitor Java applications directly, the Java_Gateway
agent , allowing us to monitor Java applications.
2. Use Zabbix to configure custom monitoring items
- We use the following
docker-compose
method to install, the physical installation can be seen in this article I wrote earlier: Portal
1. Install Zabbix
1) Install Docker
[root@Zabbix ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@Zabbix ~]# yum -y install epel-release
[root@Zabbix ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@Zabbix ~]# yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
[root@Zabbix ~]# yum -y install docker-ce-19.03.12 docker-ce-cli-19.03.12
[root@Zabbix ~]# systemctl enable docker --now
2) Install Docker-Compose
[root@Zabbix ~]# wget "https://github.com/docker/compose/releases/download/v2.3.2/docker-compose-$(uname -s)-$(uname -m)" -O /usr/local/bin/docker-compose
[root@Zabbix ~]# chmod +x /usr/local/bin/docker-compose
[root@Zabbix ~]# docker-compose --version
3) Install Agent
[root@Zabbix ~]# rpm -ivh http://repo.zabbix.com/zabbix/5.4/rhel/7/x86_64/zabbix-agent-5.4.8-1.el7.x86_64.rpm
[root@Zabbix ~]# egrep -v '^$|#' zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@Zabbix ~]# systemctl enable zabbix-agent --now
4) Install Zabbix
[root@Zabbix ~]# mkdir -p /app/zabbix
[root@Zabbix ~]# cd /app/zabbix/
[root@Zabbix zabbix]# cat <<END > docker-compose.yml
version: "3"
services:
mysql-server:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- "/app/mysql:/var/lib/mysql" # 配置数据卷 (防止监控数据丢失)
environment:
MYSQL_ROOT_PASSWORD: 123123
TZ: Asia/Shanghai
command: --character-set-server=utf8 --collation-server=utf8_bin
restart: always
zabbix-server:
image: zabbix/zabbix-server-mysql:5.4.8-centos
environment:
DB_SERVER_HOST: 127.0.0.1
DB_SERVER_PORT: 3306
MYSQL_DATABASE: zabbix
MYSQL_USER: root
MYSQL_PASSWORD: 123123
TZ: Asia/Shanghai
depends_on:
- mysql-server
restart: always
network_mode: "host" # 使用主机网络
zabbix-web:
image: zabbix/zabbix-web-nginx-mysql:5.4.8-centos
ports:
- "8000:8080" # Zabbix WebUI 映射端口
volumes: # 当在 Web 界面配置成中文后,监控界面会出现乱码
- "./simkai.ttf:/usr/share/zabbix/assets/fonts/DejaVuSans.ttf"
environment:
DB_SERVER_HOST: mysql-server
DB_SERVER_PORT: 3306
MYSQL_DATABASE: zabbix
MYSQL_USER: root
MYSQL_PASSWORD: 123123
TZ: Asia/Shanghai
ZBX_SERVER_HOST: 192.168.1.1
depends_on:
- mysql-server
- zabbix-server
restart: always
END
[root@Zabbix zabbix]# docker-compose up -d
[root@Zabbix zabbix]# docker-compose ps
simkai.ttf
Fonts, you can find them in thisC:\Windows\Fonts
path .
5) Verification: http://192.168.1.1:8000
(Account: Admin/zabbix
)
- The interface appears:
zabbix server is not running: the information displayed may not be current
error; - The reason may be
docker-compose
thatZBX_SERVER_HOST
the value of the variable configuration in our file is not the IP address of the Zabbix Server.
I reported the error above to let you see the effect. If you have the above configuration, you can modify it according to my configuration.
Configure Chinese
2. Enable custom monitoring items
[root@Zabbix ~]# echo "UnsafeUserParameters=1" >> /etc/zabbix/zabbix_agentd.conf
3. Write Nginx custom monitoring script
[root@Zabbix ~]# yum -y install nginx
[root@Zabbix ~]# cat <<END > /etc/nginx/conf.d/status.conf
server {
listen 80;
server_name 127.0.0.1;
location /status {
stub_status on;
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
}
}
END
1) Write a custom monitoring script
[root@Zabbix ~]# mkdir /etc/zabbix/scripts
[root@Zabbix ~]# vim /etc/zabbix/scripts/nginx_status.sh
#!/bin/bash
HOST="127.0.0.1"
PORT="80"
# 监控 Nginx 进程是否存在
function Ping {
/sbin/pidof nginx | wc -l
}
# 监控 Nginx 状态信息
function Active {
/usr/bin/curl "http://$HOST:$PORT/status" 2> /dev/null | grep "Active" | awk '{print $NF}'
}
function Reading {
/usr/bin/curl "http://$HOST:$PORT/status" 2> /dev/null | grep "Reading" | awk '{print $2}'
}
function Writing {
/usr/bin/curl "http://$HOST:$PORT/status" 2> /dev/null | grep "Writing" | awk '{print $4}'
}
function Waiting {
/usr/bin/curl "http://$HOST:$PORT/status" 2> /dev/null | grep "Waiting" | awk '{print $6}'
}
function Server {
/usr/bin/curl "http://$HOST:$PORT/status" 2> /dev/null | awk NR==3 | awk '{print $1}'
}
function Accepts {
/usr/bin/curl "http://$HOST:$PORT/status" 2> /dev/null | awk NR==3 | awk '{print $2}'
}
function Requests {
/usr/bin/curl "http://$HOST:$PORT/status" 2> /dev/null | awk NR==3 | awk '{print $3}'
}
# 执行 function
$1
[root@Zabbix ~]# chmod +x /etc/zabbix/scripts/nginx_status.sh
2) Define the monitoring script
[root@Zabbix ~]# cat <<"END" > /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_status.sh $1
END
[root@Zabbix ~]# systemctl restart zabbix-agent
3) Verify
[root@Zabbix ~]# docker exec -it zabbix-zabbix-server-1 /bin/bash
Create a template
Create an app set
Create monitoring items
Because in our monitoring script, a total of 8 monitoring items are defined, so we also need to create 8 monitoring items above, repeat the above operation, and create the corresponding ones.
Create graphs for our monitoring items
Add the Nginx template to the host
View the monitoring graph
In fact, Zabbix's custom monitoring items are composed key/value
of the form. That is, we only need to define the data to be monitored as one value
, key
and the value in it is the data we want to monitor. That is to say, when we regularly look up the key
corresponding value, we can get the monitoring data.