前言

Zabbix是目前最为主流的开源监控方案之一,部署本身并不困难,难的是深入理解。根据在生产环境的实践从新版Zabbix 4.0 LTS开始全部使用Docker部署,我相信未来越来越多的开源组件都会以容器化的形式呈现在我们面前。

Zabbix安装和使用配置小结

更新历史

2018年11月01日 - 更新官方LAMP部署过程
2018年10月16日 - 更新Docker部署Zabbix 4.0
2018年08月06日 - 初稿

阅读原文 - https://wsgzao.github.io/post/zabbix/

扩展阅读

Zabbix - https://www.zabbix.com/


官方文档

https://www.zabbix.com/download
https://www.zabbix.com/documentation

Zabbix Server

基于官方的LAMP架构,按照最简单的原生方式来部署,不做任何多余优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 安装必要依赖包
yum install -y httpd mariadb-server mariadb php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash

# 修改apache配置
vim /etc/httpd/conf/httpd.conf
ServerName 192.168.56.103
DirectoryIndex index.html index.php

# 修改php时区
vim /etc/php.ini
date.timezone = Asia/Singapore

# 启动 httpd 服务
systemctl start httpd.service
# 启动 mariadb 服务
systemctl start mariadb.service

# 初始化 mysql 数据库,并配置 root 用户密码
mysql_secure_installation

# 万一新版本忘记随机密码可以通过日志获取
grep 'temporary password' /var/log/mysqld.log

# 创建一个测试页,测试 LAMP 是否搭建成功
cat > /var/www/html/index.php << EOF
<?php
phpinfo();
?>
EOF

# 创建zabbix数据库
mysql -uroot -p

mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
mysql> quit;

# 部署zabbix
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum clean all
yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

# 配置数据库用户及密码
vim /etc/zabbix/zabbix_server.conf
DBPassword=zabbix

# 修改时区
vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Singapore

# 启动zabbix并设置自启动服务
systemctl restart zabbix-server zabbix-agent httpd
systemctl enable zabbix-server zabbix-agent httpd mariadb

以下是基于LNMP手动编译安装Zabbix的过程,仅供参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

# hostname
hostnamectl set-hostname <host-name>

# firewall
systemctl stop firewalld
systemctl disable firewalld

# selinux
getenforce
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

# epel
yum install epel-release

# nginx
yum install nginx
systemctl enable nginx
systemctl start nginx
systemctl status nginx
nginx -s reload

# mysql | mariadb
yum -y install mariadb mariadb-server
systemctl enable mariadb.service
systemctl start mariadb.service
mysql_secure_installation

# php
wget http://cn2.php.net/distributions/php-7.2.8.tar.gz
tar xf php-7.2.8.tar.gz
cd ./php-7.2.8/
yum install -y libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
yum install -y libmcrypt libmcrypt-devel gcc
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache
make && make install

vi /etc/profile

PATH=$PATH:/usr/local/php/bin
export PATH

source /etc/profile
echo $PATH
php -v

# php-fpm
cp ./php.ini-production /etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

vi /etc/php.ini

max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 16M
date.timezone = Asia/Singapore

/etc/init.d/php-fpm start

# nginx
vi /etc/nginx/conf.d/default.conf

server{
listen 80;
server_name localhost;
root /data/www;
location / {
index index.php index.html index.htm;
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php/$1;
#rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
}
}
#proxy the php scripts to php-fpm
location ~ \.php {
include fastcgi_params;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}
}

nginx -s reload

vi /data/www/info.php

<?php
phpinfo();
?>

http://127.0.0.1/info.php

# zabbix-server
rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
yum -y install zabbix-server-mysql zabbix-agent

# create zabbix in db
mysql -u root -p
create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to 'zabbix'@'%' identified by 'zabbix';
flush privileges;


cd /usr/share/doc/zabbix-server-mysql-3.4.11/
zcat create.sql.gz | mysql -u root -p zabbix

vi /etc/zabbix/zabbix_server.conf

DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix

chown -R zabbix:zabbix /etc/zabbix
chown -R zabbix:zabbix /usr/lib/zabbix
systemctl enable zabbix-server
systemctl start zabbix-server

# zabbix-web
wget -O zabbix-3.4.11.tar.gz https://excellmedia.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.11/zabbix-3.4.11.tar.gz
tar zxvf zabbix-3.4.11.tar.gz
cp -rf ./zabbix-3.4.11/frontends/php/ /data/www/zabbix
mv /data/www/zabbix/conf/zabbix.conf.php.example /data/www/zabbix/conf/zabbix.conf.php
vi /data/www/zabbix/conf/zabbix.conf.php

<?php
// Zabbix GUI configuration file.
global $DB, $HISTORY;

$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = '127.0.0.1';
$DB['PORT'] = '0';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'zabbix';
// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = '';

$ZBX_SERVER = 'localhost';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = '';

$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;

// Elasticsearch url (can be string if same url is used for all types).
$HISTORY['url'] = [
'uint' => 'http://127.0.0.1:9200',
'text' => 'http://127.0.0.1:9200'
];
// Value types stored in Elasticsearch.
$HISTORY['types'] = ['uint', 'text'];


http://127.0.0.1/zabbix

## zabbix-agent
rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
yum -y install zabbix-agent
vi /etc/zabbix/zabbix_agentd.conf

Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server

systemctl enable zabbix-agent
systemctl start zabbix-agent

# CPU test

CPU use percent gt 80%
{Template OS Linux:system.cpu.util[,idle].avg(1m)}<80

echo "scale=5000; 4*a(1)" | bc -l -q

Zabbix表分区优化

Zabbix MySQL Database Partitioning 表分区优化禁用housekeeping提升历史数据清理性能

https://wsgzao.github.io/post/zabbix-mysql-partition/

docker

https://www.zabbix.com/documentation/3.4/zh/manual/installation/containers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# install docker-ce
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce
sudo systemctl start docker

# docker: Error response from daemon: Get https://registry-1.docker.io/v2/: x509: certificate has expired or is not yet valid.
ntpdate 0.pool.ntp.org

# Install mysql, zabbix, nginx in docker
docker run --name mysql-server -t \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
-p 127.0.0.1:3306:3306 \
-d mysql:5.7 \
--character-set-server=utf8 --collation-server=utf8_bin

docker run --name zabbix-server-mysql -t \
--link mysql-server:mysql \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
-p 10051:10051 \
-d \
zabbix/zabbix-server-mysql:centos-4.0-latest

docker run --name zabbix-web-nginx-mysql -t \
--link mysql-server:mysql \
--link zabbix-server-mysql:zabbix-server \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
-e PHP_TZ="Asia/Singapore" \
-p 80:80 \
-d \
zabbix/zabbix-web-nginx-mysql:centos-4.0-latest


[root@zabbix_server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
98cbe8d8a6bd zabbix/zabbix-web-nginx-mysql:latest "docker-entrypoint.sh" 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:8080->80/tcp zabbix-web-nginx-mysql
de040d43d60f zabbix/zabbix-server-mysql:latest "docker-entrypoint.sh" 59 seconds ago Up 59 seconds 0.0.0.0:10051->10051/tcp zabbix-server-mysql
3276f18def8d mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 3306/tcp mysql-server

[root@zabbix_server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zabbix/zabbix-web-nginx-mysql latest 4db891b4393a 10 hours ago 177MB
zabbix/zabbix-server-mysql latest f5e58dafe9ac 10 hours ago 62.2MB
mysql 5.7 f0f3956a9dd8 7 days ago 409MB

http://127.0.0.1:8080

Admin/zabbix



Zabbix自定义监控

以监控文件系统目录的权限为例

Zabbix用户自定义参数
https://www.zabbix.com/documentation/3.4/zh/manual/config/items/userparameters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 新建自定义监控配置文件userparameter
cd /etc/zabbix/zabbix_agentd.d
vi userparameter_tmp.conf
UserParameter=check.tmp[*],stat -c %a /tmp
# 重启zabbix agent
service zabbix-agent restart

# 在Zabbix Server服务端验证
zabbix_get -s 10.65.200.90 -k check.tmp
1777
zabbix_get -s 10.65.200.90 -k agent.version
3.0.9

# 在WebUI中创建新的Template或者使用已有新增Items
Items: Key为check.tmp
Triggers: 定义Serverity,Expression如下所示
{Template Sea Ops WangAo:check.tmp.last()}<>1777

# 点击Monitoring Latest data查看Item最新数据
Moniroting -> Latest data -> Hosts -> check /tmp permission

如果需要配置Actions可以查看Zabbix使用企业微信告警配置小结

https://wsgzao.github.io/post/zabbix-alert-wechat/

Zabbix备份恢复

mysql数据库备份通常使用mysqldump或者xtrabackup

因为一般历史数据较大,如果需要保留所有数据可以考虑主从同步,如果不需要保留数据直接过滤历史数据备份相关的告警配置即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 查看当前版本Zabbix的数据库表结构
mysql -uzabbix -pzabbix zabbix -e "show tables"|egrep -v "(Tables_in_zabbix)"
mysql -uzabbix -pzabbix zabbix -e "show tables"|egrep -v "(Tables_in_zabbix|history*|trends*|acknowledges|alerts|auditlog|events|service_alarms)"

# 以下表中标记+号为需要过滤的表
+acknowledges
actions
+alerts
application_discovery
application_prototype
application_template
applications
+auditlog
+auditlog_details
autoreg_host
conditions
config
corr_condition
corr_condition_group
corr_condition_tag
corr_condition_tagpair
corr_condition_tagvalue
corr_operation
correlation
dashboard
dashboard_user
dashboard_usrgrp
dbversion
dchecks
dhosts
drules
dservices
escalations
event_recovery
event_tag
+events
expressions
functions
globalmacro
globalvars
graph_discovery
graph_theme
graphs
graphs_items
group_discovery
group_prototype
groups
+history
+history_log
+history_str
+history_text
+history_uint
host_discovery
host_inventory
hostmacro
hosts
hosts_groups
hosts_templates
housekeeper
httpstep
httpstep_field
httpstepitem
httptest
httptest_field
httptestitem
icon_map
icon_mapping
ids
images
interface
interface_discovery
item_application_prototype
item_condition
item_discovery
item_preproc
items
items_applications
maintenances
maintenances_groups
maintenances_hosts
maintenances_windows
mappings
media
media_type
opcommand
opcommand_grp
opcommand_hst
opconditions
operations
opgroup
opinventory
opmessage
opmessage_grp
opmessage_usr
optemplate
problem
problem_tag
profiles
proxy_autoreg_host
+proxy_dhistory
+proxy_history
regexps
rights
screen_user
screen_usrgrp
screens
screens_items
scripts
+service_alarms
services
services_links
services_times
sessions
slides
slideshow_user
slideshow_usrgrp
slideshows
sysmap_element_trigger
sysmap_element_url
sysmap_shape
sysmap_url
sysmap_user
sysmap_usrgrp
sysmaps
sysmaps_elements
sysmaps_link_triggers
sysmaps_links
task
task_acknowledge
task_close_problem
task_remote_command
task_remote_command_result
timeperiods
+trends
+trends_uint
trigger_depends
trigger_discovery
trigger_tag
triggers
users
users_groups
usrgrp
valuemaps
widget
widget_field

# 我们直接提取需要过滤的表
acknowledges
alerts
auditlog
auditlog_details
events
history
history_log
history_str
history_text
history_uint
proxy_dhistory
proxy_history
service_alarms
trends
trends_uint

# 使用--ignore-table跳过不需要备份的表

# 过滤最简单的表只需要history*和trends*
mysqldump -uzabbix -pzabbix --databases zabbix \
--ignore-table=zabbix.history \
--ignore-table=zabbix.history_log \
--ignore-table=zabbix.history_str \
--ignore-table=zabbix.history_text \
--ignore-table=zabbix.history_uint \
--ignore-table=zabbix.trends \
--ignore-table=zabbix.trends_uint > /tmp/zabbix_config.sql

# 如果需要做更细化的过滤,可以参考下面的过滤表
mysqldump -uzabbix -pzabbix --databases zabbix \
--ignore-table=zabbix.acknowledges \
--ignore-table=zabbix.alerts \
--ignore-table=zabbix.auditlog \
--ignore-table=zabbix.auditlog_details \
--ignore-table=zabbix.events \
--ignore-table=zabbix.history \
--ignore-table=zabbix.history_log \
--ignore-table=zabbix.history_str \
--ignore-table=zabbix.history_text \
--ignore-table=zabbix.history_uint \
--ignore-table=zabbix.proxy_dhistory \
--ignore-table=zabbix.proxy_history \
--ignore-table=zabbix.service_alarms \
--ignore-table=zabbix.services_times \
--ignore-table=zabbix.trends \
--ignore-table=zabbix.trends_uint > /tmp/zabbix_config.sql

# 如果数据量交大,可以考虑使用gzip压缩
--ignore-table=zabbix.trends_uint | gzip > zabbix_`date +'%Y%m%d%H%M%S'`.sql.gz

# 上传至新的数据库执行导入
mysql -uzabbix -pzabbix zabbix < zabbix_config.sql

itnihao的书中分享了一部分代码,可以做些许参考吧
https://github.com/itnihao/zabbix-book/tree/master/03-chapter

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. 官方文档
  4. 4. Zabbix Server
  5. 5. Zabbix表分区优化
  6. 6. docker
  7. 7. Zabbix自定义监控
  8. 8. Zabbix备份恢复