Zabbix - 部署随笔

2023-06-15,,

部署Zabbix服务端

    准备机器,初始化环境

#查看IP地址
[root@Minimal ~]# ifconfig ens33 | awk 'NR==2{print $2}'
10.0.0.243 #关闭防火墙
[root@Minimal ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@Minimal ~]# systemctl disable --now firewalld #检查是否成功关闭
[root@Minimal ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination Chain OUTPUT (policy ACCEPT)
target prot opt source destination [root@Minimal ~]# getenforce
Disabled # 这样才是成功,如果提示Enforcing,需要重启。 [root@Minimal ~]# getenforce
Enforcing # 未生效,需要重启。
    Zabbix-server内存最好大于4G
    获取Zabbix的下载源
[root@Minimal ~]# rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
获取https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.Q6ueOF: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:zabbix-release-5.0-1.el7 ################################# [100%]
    替换zabbix.repo源为阿里源
[root@Minimal ~]# sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
    清空缓存,下载Zabbix服务端
[root@Minimal ~]# yum clean all
# 或者使用yum makecache
[root@Minimal ~]# yum -y install zabbix-server-mysql zabbix-agent
    安装 Software collections

安装 Software collections, 便于后续安装高版本的php,默认 yum 安装的php版本为 5.4 过低。SCL(Software collections)可以让你在同一个操作系统上安装和使用多个版本的软件,而不会影响整个系统的安装包。

软件包会安装在/opt/rh目录下。

为了避免系统广泛冲突,/opt/ rh包安装在目录中,例如,这允许你在centos 7机器上安装Python 3.5,而不会删除或干扰Python2.7。

/etc/opt/rh/软件包的所有配置文件都存储在目录中相应的目录中,SCL包提供了定义使用所包含应用程序所需的环境变量的Shell脚本,例如PATH,LD_LIBBAR...

[root@Minimal ~]#  yum -y install centos-release-scl
    修改zabbix前端源
[root@Minimal ~]# vim /etc/yum.repos.d/zabbix.repo 

[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1 #默认为0,修改成1。
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
    安装Zabbix前端环境,且是安装到scl环境下
[root@Minimal ~]# yum -y install zabbix-web-mysql-scl zabbix-apache-conf-scl
    安装Zabbix所需的mariadb数据库
[root@Minimal ~]# yum -y install mariadb-server
    配置数据库开机自启动
[root@Minimal ~]# systemctl enable --now mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. # 检查服务和端口号
[root@Minimal ~]# systemctl status mariadb
[root@Minimal ~]# netstat -tunlp
    初始化数据库,设置密码
[root@Minimal ~]# mysql_secure_installation
# 输入当前数据库root用户密码,默认为空,直接回车
Enter current password for root (enter for none):
OK, successfully used password, moving on...
# 是否要设置root密码
Set root password? [Y/n] y
New password: # 新密码
Re-enter new password: # 再次输入以确认
Password updated successfully!
# 是否要移除匿名用户
Remove anonymous users? [Y/n] y
... Success!
# 是否禁止root远程登录
Disallow root login remotely? [Y/n] n
... skipping.
# 是否已出测试数据库
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
# 是否刷新授权表
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
# 配置完成
Thanks for using MariaDB!
    添加数据库用户,以及Zabbix所需的数据库信息
[root@Minimal ~]# mysql -uroot -p
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
# 创建Zabbix数据库
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
# 创建Zabbix用户
MariaDB [(none)]> create user zabbix@localhost identified by '密码';
Query OK, 0 rows affected (0.00 sec)
# 授权
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)
# 刷新授权表
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退出Mariadb
MariaDB [(none)]> exit;
Bye
    使用zabbix-mysql命令,导入数据库信息。
# mysql -u用户名 -p 数据库名

[root@Minimal ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

# 检查是否导入成功

[root@Minimal ~]# mysql -uzabbix -p密码
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| zabbix |
+--------------------+
2 rows in set (0.00 sec) MariaDB [(none)]> use zabbix MariaDB [zabbix]> show tables;
+----------------------------+
| Tables_in_zabbix |
+----------------------------+
| acknowledges |
| actions |
| alerts |
...
+----------------------------+
166 rows in set (0.00 sec) MariaDB [zabbix]> exit
Bye
    修改Zabbix server配置文件,修改数据库的密码
[root@Minimal ~]# vim /etc/zabbix/zabbix_server.conf
[root@Minimal ~]# grep '^DBPass' /etc/zabbix/zabbix_server.conf
DBPassword=数据库密码
    修改Zabbix的PHP配置文件
[root@Minimal ~]# vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
[root@Minimal ~]# grep 'timezone' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
php_value[date.timezone] = Asia/Shanghai
    启动Zabbix相关服务
[root@Minimal ~]# systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
[root@Minimal ~]# systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
    访问Zabbix入口:IP地址/zabbix

检查环境是否正常,没问题点击下一步

输入数据库密码,其他无特殊需求可以保持默认,然后下一步。

下一步

安装的细节信息,没问题可以直接下一步

显示如下页面代表成功安装

    安装成功后,默认账户名为Admin,密码为zabbix

登录成功

修改语言

遇到问题:zabbix server is not running

# 查看Zabbix日志,G跳到最后一行
[root@Minimal ~]# vi /var/log/zabbix/zabbix_server.log
# 发现提示
6396:20220303:233805.643 cannot initialize alert manager: Cannot bind socket to "/var/run/zabbix/zabbix_server_alerter.sock": [13] Permission denied.
6399:20220303:233805.646 server #5 started [alerter #3]
6386:20220303:233805.646 One child process died (PID:6396,exitcode/signal:1). Exiting ...
6397:20220303:233805.647 server #3 started [alerter #1]
6386:20220303:233805.650 syncing trend data...
6386:20220303:233805.651 syncing trend data done
6386:20220303:233805.651 Zabbix Server stopped. Zabbix 5.0.21 (revision 47104dd574).

由提示可知是权限问题


解决办法:

一:关闭防火墙

systemctl stop firewalld && systemctl disable firewalld

二:关闭SELinux

setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

然后重启Zabbix服务即可


部署Zabbix客户端

Zabbix5.0版本

agent2新版本采用golang语言开发的客户端

由于是go语言开发,部署起来就很方便了,和之前的程序部署形式不一样了

agnet2默认用10050端口,也就是zabbix客户端的端口

• 旧版本的客户端,zabbix-agent

• go语言新版客户端,zabbix-agent2

准备工作:确保防火墙和SELinux是关闭的

    检查服务端和客户端时间和时区是否一致,如不一致,可以参考下面配置

    时间

    [root@Minimal ~]# yum -y install ntpdate
    [root@Minimal ~]# ntpdate -u ntp.aliyun.com

    时区

    mv /etc/localtime{,.bak}
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

    安装zabbix-agent2

    # 提前配置好客户端的Zabbix源,参考前面部署服务端端配置即可。
    [root@Minimal ~]# yum -y install zabbix-agent2
    # 查看配置文件是否存在
    [root@Minimal ~]# vim /etc/zabbix/zabbix_agent2.conf
    # 查看zabbix_agent2是否存在
    [root@Minimal ~]# ls -l /usr/sbin/zabbix_agent2
    -rwxr-xr-x 1 root root 15958896 2月 28 22:36 /usr/sbin/zabbix_agent2

    设置zabbix-agent2开机自启,并检查是否正常运行在10050端口

    [root@Minimal ~]# systemctl enable --now zabbix-agent2
    Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent2.service to /usr/lib/systemd/system/zabbix-agent2.service. [root@Minimal ~]# netstat -ntlp|grep zabbix
    tcp6 0 0 :::10050 :::* LISTEN 1435/zabbix_agent2

systemctl是怎么管理服务的

[root@Minimal ~]# ls /lib/systemd/system/zabbix-agent2.service
/lib/systemd/system/zabbix-agent2.service
[root@Minimal ~]# cat /lib/systemd/system/zabbix-agent2.service
[Unit]
Description=Zabbix Agent 2
After=syslog.target
After=network.target [Service]
Environment="CONFFILE=/etc/zabbix/zabbix_agent2.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-agent2
Type=simple
Restart=on-failure
PIDFile=/run/zabbix/zabbix_agent2.pid
KillMode=control-group
ExecStart=/usr/sbin/zabbix_agent2 -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix [Install]
WantedBy=multi-user.target

    修改agent2配置文件,查看配置信息

    修改主机名可以使用下面这条命令

    hostnamectl set-hostname 主机名称
    [root@Minimal ~]# grep -Ev '^#|^$' /etc/zabbix/zabbix_agent2.conf
    PidFile=/var/run/zabbix/zabbix_agent2.pid
    LogFile=/var/log/zabbix/zabbix_agent2.log # Zabbix客户端日志路径
    LogFileSize=0 # 日志大小,默认为0代表没有限制
    Server=127.0.0.1 # 服务端地址,这里需要修改。
    ServerActive=127.0.0.1 # 修改成Zabbix服务端地址
    Hostname=Zabbix server # 修改为客户端的主机名称
    Include=/etc/zabbix/zabbix_agent2.d/*.conf # 配置文件地址
    ControlSocket=/tmp/agent.sock [root@Minimal ~]# vim /etc/zabbix/zabbix_agent2.conf # 修改上面需要修改的信息 [root@Minimal ~]# cat /var/run/zabbix/zabbix_agent2.pid
    1430[root@Minimal ~]# ps -ef | grep zabbix
    zabbix 1430 1 0 00:27 ? 00:00:01 /usr/sbin/zabbix_agent2 -c /etc/zabbix/zabbix_agent2.conf
    root 1449 1303 0 00:37 pts/0 00:00:00 grep --color=auto zabbix

    重启zabbix-agent2

    [root@zabbix-agent1 ~]# systemctl restart zabbix-agent2

验证Zabbix-agent2的连通性

    在服务端上通过命令,主动获取数据。

    # 安装zabbix-get
    [root@Minimal ~]# yum -y install zabbix-get
    # 采集客户端机器信息
    zabbix_get -s '客户端IP地址' -p 端口号(默认10050) -k '指定执行的命令',返回结果为1代表成功。
    [root@Minimal ~]# zabbix_get -s '10.0.0.38' -p 10050 -k 'agent.ping'
    1
    [root@Minimal ~]# zabbix_get -s '10.0.0.24' -p 10050 -k 'agent.ping'
    1
    # 测试获取主机名
    [root@Minimal ~]# zabbix_get -s '10.0.0.38' -p 10050 -k 'system.hostname'
    zabbix-agent1
    [root@Minimal ~]# zabbix_get -s '10.0.0.24' -p 10050 -k 'system.hostname'
    zabbix-agent2

    解决Zabbix-server查看的乱码问题

    Zabbix默认检测了服务端本身,但是编码有问题。


解决办法:

# 安装字体
[root@Minimal ~]# yum install wqy-microhei-fonts
# 复制字体
[root@Minimal ~]# \cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf
# Windows平台如上面办法无法生效,可以使用下面这个方法
1. Zabbix的字体目录是/usr/share/fonts/dejavu/DejaVuSans.ttf
2. 在自己电脑里字体找到C:\Windows\Fonts\simkai.ttf
3. 上传到服务器/usr/share/fonts/dejavu然后把\simkai.ttf.重命名为DejaVuSans.ttf即可

添加agent主机

    填写主机信息

    选择模板

    成功添加

Zabbix - 部署随笔的相关教程结束。

《Zabbix - 部署随笔.doc》

下载本文的Word格式文档,以方便收藏与打印。