Zabbix 5.4 在CentOS 8.2上的快速搭建

2021-09-15 17:47:53 浏览数 (3)

1. 前期准备工作

  • 下载Zabbix源码安装包,官网地址:https://www.zabbix.com/download_sources
  • 下载Zabbix源码Agent,官网地址:https://www.zabbix.com/download_agents,下载Linux操作系统的Agent请注意查看内核版本,可以使用uname -a命令查看内核版本
  • 安装CentOS 8.x的操作系统,因为CentOS 8.x的操作系统可以一键部署LAMP环境

2. 开始安装LAMP环境

  • 关闭防火墙
代码语言:javascript复制
[root@zabbix54 ~]# systemctl disable --now firewalld
  • 关闭SELINUX
代码语言:javascript复制
[root@zabbix54 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
  • 配置本地yum源
代码语言:javascript复制
# 对如下repo进行重命名
[root@zabbix54 yum.repos.d]# mv CentOS-AppStream.repo CentOS-AppStream.repo.bak
[root@zabbix54 yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@zabbix54 yum.repos.d]# mv CentOS-Extras.repo CentOS-Extras.repo.bak
[root@zabbix54 yum.repos.d]# mv CentOS-Vault.repo CentOS-Vault.repo.bak

# 编辑CentOS-Media.repo
[root@zabbix54 yum.repos.d]# vi CentOS-Media.repo
[root@zabbix54 yum.repos.d]# cat CentOS-Media.repo 
# CentOS-Media.repo
#
#  This repo can be used with mounted DVD media, verify the mount point for
#  CentOS-8.  You can use this repo and yum to install items directly off the
#  DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
#  yum --enablerepo=c8-media [command]
#
# or for ONLY the media repo, do this:
#
#  yum --disablerepo=* --enablerepo=c8-media [command]

[c8-media-BaseOS]
name=CentOS-BaseOS-$releasever - Media
baseurl=file:///media/CentOS/BaseOS
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[c8-media-AppStream]
name=CentOS-AppStream-$releasever - Media
baseurl=file:///media/CentOS/AppStream
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

# 在/media目录下创建CentOS目录
[root@zabbix54 yum.repos.d]# mkdir -p /media/CentOS
[root@zabbix54 yum.repos.d]# ll /media/
total 0
drwxr-xr-x. 2 root root 6 Sep 15 11:17 CentOS

# 操作系统镜像挂载
[root@zabbix54 yum.repos.d]# mount /dev/cdrom /media/CentOS/
mount: /media/CentOS: WARNING: device write-protected, mounted read-only.
  • 安装LAMP
代码语言:javascript复制
[root@zabbix54 yum.repos.d]# dnf install -y httpd php php-gd php-ldap php-mysqlnd php-json php-bcmath php-mbstring php-xml mysql mysql-server mysql-devel libevent-devel pcre-devel gcc gcc-c   make libcurl-devel curl-* net-snmp* libxml2-* wget tar
CentOS-BaseOS-8 - Media                                                                                                                                     95 MB/s | 2.2 MB     00:00    
CentOS-AppStream-8 - Media                                                                                                                                  99 MB/s | 5.7 MB     00:00    
Package curl-7.61.1-12.el8.x86_64 is already installed.
Package net-snmp-libs-1:5.8-14.el8.x86_64 is already installed.
Package wget-1.19.5-8.el8_1.1.x86_64 is already installed.
Package tar-2:1.30-4.el8.x86_64 is already installed.
Dependencies resolved.
  • 启动LAMP并设置为开机启动
代码语言:javascript复制
[root@zabbix54 yum.repos.d]# systemctl enable --now httpd mysqld php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

[root@zabbix54 yum.repos.d]# systemctl start httpd mysqld php-fpm

3. Zabbix编译安装

  • 创建zabbix用户
代码语言:javascript复制
[root@zabbix54 yum.repos.d]# useradd zabbix
  • 解压并编译安装Zabbix 5.4源码
代码语言:javascript复制
[root@zabbix54 home]# tar -zxvf zabbix-5.4.4.tar.gz
[root@zabbix54 home]# cd zabbix-5.4.4/
[root@zabbix54 zabbix-5.4.4]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql  --with-net-snmp --with-libcurl --with-libxml2

# 执行安装
***********************************************************
*            Now run 'make install'                       *
*                                                         *
*            Thank you for using Zabbix!                  *
*              <http://www.zabbix.com>                    *
***********************************************************
[root@zabbix54 zabbix-5.4.4]# make install
  • 登录MySQL数据库创建zabbix用户并授权
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 9
Server version: 8.0.17 Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mysql              |
| performance_schema |
| sys                |
 -------------------- 
4 rows in set (0.01 sec)

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected, 2 warnings (0.01 sec)

mysql> create user zabbix@localhost identified by 'welcome1';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.01 sec)

mysql> 
  • 导入Zabbix基表
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# mysql -u zabbix -p zabbix < /home/zabbix-5.4.4/database/mysql/schema.sql
Enter password: 
[root@zabbix54 zabbix-5.4.4]# mysql -u zabbix -p zabbix < /home/zabbix-5.4.4/database/mysql/images.sql 
Enter password: 
[root@zabbix54 zabbix-5.4.4]# mysql -u zabbix -p zabbix < /home/zabbix-5.4.4/database/mysql/data.sql 
Enter password: 
[root@zabbix54 zabbix-5.4.4]# 
  • 修改Zabbix数据库密码配置文件
代码语言:javascript复制
# 配置文件路径/usr/local/zabbix/etc/zabbix_server.conf

### Option: DBPassword
#       Database password.
#       Comment this line if no password is used.
#
# Mandatory: no
# Default:
DBPassword=welcome1
  • Zabbix Server添加systemd启动文件
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# vi /lib/systemd/system/zabbix-server.service

# 文件内容
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target
After=mysql.service
After=mysqld.service
After=mariadb.service
After=postgresql.service

[Service]
Environment="CONFFILE=/usr/local/zabbix/etc/zabbix_server.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-server
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_server.pid
KillMode=control-group
ExecStart=/usr/local/zabbix/sbin/zabbix_server -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=0

[Install]
WantedBy=multi-user.target
  • Zabbix Agent添加systemd启动文件
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# vi /lib/systemd/system/zabbix-agent.service
# 文件内容
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=/usr/local/zabbix/etc/zabbix_agentd.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_agentd.pid
KillMode=control-group
ExecStart=/usr/local/zabbix/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
  • 启动Zabbix Server和Zabbix Agent并设置为开机启动
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# systemctl enable --now zabbix-server.service
[root@zabbix54 zabbix-5.4.4]# systemctl enable --now zabbix-agent.service
  • 检查Zabbix Server服务状态
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# systemctl status zabbix-server.service 
● zabbix-server.service - Zabbix Server
   Loaded: loaded (/usr/lib/systemd/system/zabbix-server.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-09-15 11:53:58 CST; 19s ago
  Process: 48991 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 48996 ExecStart=/usr/local/zabbix/sbin/zabbix_server -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 48998 (zabbix_server)
    Tasks: 44 (limit: 49617)
   Memory: 28.9M
   CGroup: /system.slice/zabbix-server.service
           ├─48998 /usr/local/zabbix/sbin/zabbix_server -c /usr/local/zabbix/etc/zabbix_server.conf
           ├─49001 /usr/local/zabbix/sbin/zabbix_server: configuration syncer [synced configuration in 0.042213 sec, idle 60 sec]
           ├─49002 /usr/local/zabbix/sbin/zabbix_server: alert manager #1 [sent 0, failed 0 alerts, idle 5.003170 sec during 5.003234 sec]
           ├─49003 /usr/local/zabbix/sbin/zabbix_server: alerter #1 started
           ├─49004 /usr/local/zabbix/sbin/zabbix_server: alerter #2 started
           ├─49005 /usr/local/zabbix/sbin/zabbix_server: alerter #3 started
           ├─49006 /usr/local/zabbix/sbin/zabbix_server: preprocessing manager #1 [queued 0, processed 6 values, idle 5.002300 sec during 5.002404 sec]
           ├─49007 /usr/local/zabbix/sbin/zabbix_server: preprocessing worker #1 started
           ├─49008 /usr/local/zabbix/sbin/zabbix_server: preprocessing worker #2 started
           ├─49009 /usr/local/zabbix/sbin/zabbix_server: preprocessing worker #3 started
           ├─49010 /usr/local/zabbix/sbin/zabbix_server: lld manager #1 [processed 0 LLD rules, idle 5.004166sec during 5.004217 sec]
           ├─49011 /usr/local/zabbix/sbin/zabbix_server: lld worker #1 started
           ├─49012 /usr/local/zabbix/sbin/zabbix_server: lld worker #2 started
           ├─49013 /usr/local/zabbix/sbin/zabbix_server: housekeeper [startup idle for 30 minutes]
           ├─49014 /usr/local/zabbix/sbin/zabbix_server: timer #1 [updated 0 hosts, suppressed 0 events in 0.000317 sec, idle 59 sec]
           ├─49015 /usr/local/zabbix/sbin/zabbix_server: http poller #1 [got 0 values in 0.000428 sec, idle 5 sec]
           ├─49016 /usr/local/zabbix/sbin/zabbix_server: discoverer #1 [processed 0 rules in 0.000443 sec, idle 60 sec]
           ├─49017 /usr/local/zabbix/sbin/zabbix_server: history syncer #1 [processed 0 values, 0 triggers in 0.000021 sec, idle 1 sec]
           ├─49018 /usr/local/zabbix/sbin/zabbix_server: history syncer #2 [processed 0 values, 0 triggers in 0.000022 sec, idle 1 sec]
           ├─49019 /usr/local/zabbix/sbin/zabbix_server: history syncer #3 [processed 0 values, 0 triggers in 0.000035 sec, idle 1 sec]
           ├─49020 /usr/local/zabbix/sbin/zabbix_server: history syncer #4 [processed 1 values, 1 triggers in 0.009523 sec, idle 1 sec]
           ├─49021 /usr/local/zabbix/sbin/zabbix_server: escalator #1 [processed 0 escalations in 0.000998 sec, idle 3 sec]
           ├─49022 /usr/local/zabbix/sbin/zabbix_server: proxy poller #1 [exchanged data with 0 proxies in 0.000021 sec, idle 5 sec]
           ├─49023 /usr/local/zabbix/sbin/zabbix_server: self-monitoring [processed data in 0.000034 sec, idle 1 sec]
           ├─49024 /usr/local/zabbix/sbin/zabbix_server: task manager [processed 0 task(s) in 0.000422 sec, idle 5 sec]
  • 配置PHP参数
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# sed -i 's#post_max_size = 8M#post_max_size = 16M#' /etc/php.ini
[root@zabbix54 zabbix-5.4.4]# sed -i 's#max_execution_time = 30#max_execution_time = 300#' /etc/php.ini
[root@zabbix54 zabbix-5.4.4]# sed -i 's#max_input_time = 60#max_input_time = 300#' /etc/php.ini
[root@zabbix54 zabbix-5.4.4]# sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /etc/php.ini
[root@zabbix54 zabbix-5.4.4]# systemctl restart php-fpm
  • 拷贝Zabbix前端文件到Apache默认的web目录
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# cp -r /home/zabbix-5.4.4/ui/* /var/www/html/
[root@zabbix54 zabbix-5.4.4]# chown -R apache:apache /var/www/html/

4. web初始化

  • 浏览器访问Zabbix服务器IP地址进入欢迎页面
  • 组件检查
  • 配置MySQL数据库密码
  • Zabbix服务器配置
  • GUI配置
  • 初始化信息汇总
  • 初始化安装,如果提示无法创建配置,点击页面上的链接下载配置文件上传到指定位置,并修改属组为apache:apache
  • 初始话完成,默认用户名:Admin,密码:zabbix
  • 进入仪表盘

5. ZABBIX SERVER IS NOT RENNING THE INFORMATION DISPLAYED MAY NOT BE CORRECT

Zabbix 5.4 启动后总是弹出ZABBIX SERVER IS NOT RENNING: THE INFORMATION DISPLAYED MAY NOT BE CORRECT的原因为SELINUX没有关闭,在前面的配置中我们已经关闭了SELINUX,因为没有重启,所以配置没有生效。

代码语言:javascript复制
# 重启之前
[root@zabbix54 ~]# getenforce 
Enforcing

# 重启之后
[root@zabbix54 ~]# getenforce 
Disabled

6. Zabbix增加中文

  • 检查中文依赖包并进行中文依赖包安装
代码语言:javascript复制
[root@zabbix54 ~]# locale -a | grep zh_CN

# 安装中文依赖包
[root@zabbix54 ~]# dnf install langpacks-zh_CN.noarch
Last metadata expiration check: 1:38:45 ago on Wed 15 Sep 2021 11:19:24 AM CST.
Dependencies resolved.
===========================================================================================================================================================================================
 Package                                         Architecture                         Version                                       Repository                                        Size
===========================================================================================================================================================================================
Installing:
 langpacks-zh_CN                                 noarch                               1.0-12.el8                                    c8-media-AppStream                               9.6 k
Installing weak dependencies:
 glibc-langpack-zh                               x86_64                               2.28-101.el8                                  c8-media-BaseOS                                  2.1 M

Transaction Summary
===========================================================================================================================================================================================
Install  2 Packages

Total size: 2.2 M
Installed size: 15 M
Is this ok [y/N]: y   
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                   1/1 
  Installing       : glibc-langpack-zh-2.28-101.el8.x86_64                                                                                                                             1/2 
  Installing       : langpacks-zh_CN-1.0-12.el8.noarch                                                                                                                                 2/2 
  Running scriptlet: langpacks-zh_CN-1.0-12.el8.noarch                                                                                                                                 2/2 
  Verifying        : glibc-langpack-zh-2.28-101.el8.x86_64                                                                                                                             1/2 
  Verifying        : langpacks-zh_CN-1.0-12.el8.noarch                                                                                                                                 2/2 
Installed products updated.

Installed:
  glibc-langpack-zh-2.28-101.el8.x86_64                                                          langpacks-zh_CN-1.0-12.el8.noarch                                                         

Complete!

# 安装后检查
[root@zabbix54 ~]# locale -a | grep zh_CN
zh_CN
zh_CN.gb18030
zh_CN.gbk
zh_CN.utf8
  • 监控项中文乱码处理

把C:WindowsFonts下的黑体(simhei.ttf)字体上传到Zabbix服务器,并复制到/var/www/html/assets/fonts目录(注意文件权限),查找下对应的assets/fonts即可,上传完成后修改/var/www/html/include/defines.inc.php,具体修改内容如下:复制define(‘ZBX_GRAPH_FONT_NAME’,’DejaVuSans’); // font file name这一行并注释掉修改字体名称为我们上传的字体define(‘ZBX_GRAPH_FONT_NAME’,’DejaVuSans’); // font file name

7. Zabbix Agent编译安装

  • 创建目录,用户并解压
代码语言:javascript复制
# 创建目录
[root@oracle12c ~]# mkdir -p /usr/local/zabbix

# 创建用户
[root@oracle12c ~]# useradd zabbix

# 解压Agent到创建的目录
[root@oracle12c ~]# tar -zxvf /home/zabbix_agent-5.4.4-linux-3.0-amd64-static.tar.gz -C /usr/local/zabbix/
./
./bin/
./bin/zabbix_sender
./bin/zabbix_get
./conf/
./conf/zabbix_agentd/
./conf/zabbix_agentd/userparameter_examples.conf
./conf/zabbix_agentd/userparameter_mysql.conf
./conf/zabbix_agentd.conf
./sbin/
./sbin/zabbix_agentd
[root@oracle12c ~]# 
  • 为监控服务器Zabbix Agent添加systemd启动文件
代码语言:javascript复制
[root@oracle12c ~]# vi /lib/systemd/system/zabbix-agent.service

# 文件内容
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=/usr/local/zabbix/conf/zabbix_agentd.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_agentd.pid
KillMode=control-group
ExecStart=/usr/local/zabbix/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
  • 配置Zabbix Agent
代码语言:javascript复制
[root@oracle12c ~]# vi /usr/local/zabbix/conf/zabbix_agentd.conf

# 修改服务器地址
[root@oracle12c ~]# cat /usr/local/zabbix/conf/zabbix_agentd.conf | grep ^Server
Server=10.50.2.200
ServerActive=10.50.2.200
  • 启动Agent并加入开机启动
代码语言:javascript复制
# 启动Agent服务
[root@oracle12c ~]# systemctl start zabbix-agent.service 

# 查看启动状态
[root@oracle12c ~]# systemctl status zabbix-agent.service 
● zabbix-agent.service - Zabbix Agent
   Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-09-15 14:11:05 CST; 4s ago
  Process: 11012 ExecStart=/usr/local/zabbix/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 11014 (zabbix_agentd)
    Tasks: 6
   CGroup: /system.slice/zabbix-agent.service
           ├─11014 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
           ├─11015 /usr/local/zabbix/sbin/zabbix_agentd: collector [idle 1 sec]
           ├─11016 /usr/local/zabbix/sbin/zabbix_agentd: listener #1 [waiting for connection]
           ├─11017 /usr/local/zabbix/sbin/zabbix_agentd: listener #2 [waiting for connection]
           ├─11018 /usr/local/zabbix/sbin/zabbix_agentd: listener #3 [waiting for connection]
           └─11019 /usr/local/zabbix/sbin/zabbix_agentd: active checks #1 [idle 1 sec]

Sep 15 14:11:05 oracle12c systemd[1]: Starting Zabbix Agent...
Sep 15 14:11:05 oracle12c systemd[1]: Started Zabbix Agent.

# 加入开机启动
[root@oracle12c ~]# systemctl enable zabbix-agent.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.

8. FAQ

  • 问题:Zabbix日志路径

回答:/tmp/zabbix_server.log

  • 问题:[Z3001] connection to database 'zabbix' failed:[1045] Access denied for user 'zabbix'@'localhost'(using password: NO)
代码语言:javascript复制
[root@zabbix54 zabbix-5.4.4]# tail -f /tmp/zabbix_server.log 
 48753:20210915:115253.502 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO)
 48753:20210915:115253.502 database is down: reconnecting in 10 seconds
 48753:20210915:115303.503 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO)
 48753:20210915:115303.503 database is down: reconnecting in 10 seconds
 48753:20210915:115313.504 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO)
 48753:20210915:115313.504 database is down: reconnecting in 10 seconds
 48753:20210915:115323.505 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO)
 48753:20210915:115323.505 database is down: reconnecting in 10 seconds
 48753:20210915:115333.506 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO)
 48753:20210915:115333.506 database is down: reconnecting in 10 seconds
 48753:20210915:115343.507 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO)

回答:Zabbix_server.conf中没有配置数据库的密码

  • 问题:添加主机后可用性为灰色或者红色

回答:灰色为没有添加监控项,红色网络不通

0 人点赞