一.为什么要学习 ZABBIX?
作为一个运维,需要会使用监控系统查看服务器系统性能、应用服务状态和网站流量指标等,利用监控系统的数据去了解网站上线发布的结果和健康状态。
利用一个优秀的监控软件,我们可以:
- 通过一个友好的界面进行浏览整个网站所有的服务器状态(并且是实时展示)
- 可以在 Web 前端方便的查看监控数据
- 可以回溯寻找事故发生时系统的问题和报警情况
二.什么是 ZABBIX?
Zabbix 是一款开源的网络监控和告警系统,主要用于监控服务器、网络设备、应用程序等的性能和可用性。它能够实时收集、存储和分析数据,并在检测到问题时发送告警通知。
官方地址:https://www.zabbix.com/cn/
三.ZABBIX 的主要功能
- 数据采集:支持多种协议(如 SNMP、IPMI、JMX 等)和自定义脚本。
- 监控范围:涵盖服务器、网络设备、虚拟化平台、数据库、应用程序等。
- 告警机制:支持多种通知方式(如邮件、短信、即时通讯工具),并允许自定义触发条件。
- 可视化:提供图表、仪表盘等工具,便于数据展示和分析。
- 自动化:支持自动发现设备和自动注册新设备。
- 扩展性:可通过自定义脚本和插件扩展功能。
四.ZABBIX 的主要组件
Zabbix Server:Zabbix Agent 是部署在被监控设备上的轻量级软件组件,负责采集被监控设备的各种指标数据(如CPU使用率、内存占用、磁盘空间等)并发送给 Zabbix Server。Zabbix Agent 支持主动模式和被动模式两种数据采集方式,以适应不同的监控需求和网络环境。
Zabbix Agent:Zabbix Agent 是部署在被监控设备上的轻量级软件组件,负责采集被监控设备的各种指标数据(如CPU使用率、内存占用、磁盘空间等)并发送给 Zabbix Server。Zabbix Agent 支持主动模式和被动模式两种数据采集方式,以适应不同的监控需求和网络环境。
Zabbix Proxy:Zabbix Proxy 是可选组件,用于在分布式监控环境中代替 Zabbix Server 接收监控数据并进行预处理。Proxy 减少了数据传输的距离和 Server 的处理压力,提高了监控系统的效率和可扩展性。预处理后的数据会批量发送给 Zabbix Server 进行进一步处理。
Zabbix Web 界面:Zabbix Web 界面是 Zabbix 监控系统的图形用户界面(GUI(Graphical User Interface))。
数据库:数据库用于存储被监控设备的配置信息和监控数据。Zabbix 支持多种数据库类型,如 MySQL、Oracle、PostgreSQL、SQLite 等,用户可以根据实际需求选择合适的数据库系统。
五.ZABBIX 两种工作模式
被动模式(Passive Mode)
Zabbix Server 消耗自身资源主动的要数据 agent被动的给,站在agent角度,叫被动模式。
工作原理
在被动模式下,Zabbix Agent会监听一个特定的端口(默认是10050),等待Zabbix Server的指令来收集数据。Zabbix Server会周期性地向Agent发送请求,Agent在收到请求后,会收集相应的监控数据并返回给Server。
特点
默认模式:Zabbix默认采用被动模式进行数据收集。
依赖Server:数据收集依赖于Server的指令,Server需要主动向Agent发起请求。
适用于小规模监控:在监控的主机数量不多时,被动模式可以很好地工作。
可能存在的问题:当被监控的主机数量达到一定规模时,Server需要处理大量的请求,可能会导致性能瓶颈。
主动模式(Active Mode)
Zabbix Server 不会消耗自身资源主动的要数据 agent主动的把采集的数据给server,站在agent角度,叫主动模式。
工作原理
在主动模式下,Zabbix Agent会主动收集监控数据,并通过TCP协议将数据发送到Zabbix Server的特定端口(默认是10051)。这种方式下,Agent不再等待Server的请求,而是主动将数据推送给Server。
特点
减轻Server压力:由于Agent主动发送数据,可以减轻Zabbix Server的负担,特别是在监控大量主机时。
实时性:数据可以更快地到达Server,提高了监控的实时性。
适用于大规模监控:在监控大量主机时,主动模式可以更有效地利用网络资源,减少延迟。
配置相对复杂:与被动模式相比,主动模式需要更多的配置工作,包括设置Agent的ServerActive参数等
六.ZABBIX 部署
(1)环境准备
系统 | IP 地址 | 硬件配置 |
Rocky9.3 | 192.168.123.167 | 2C 核心 4G内存 |
关闭防火墙并设置开机不启动
#查看防火墙状态
systemctl status firewalld
#关闭防火墙
systemctl stop firewalld
#设置防火墙开机禁用
systemctl disable firewalld
关闭 SElinux
#查看 SElinux 当前状态
getenforce
#永久关闭 SElinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#临时关闭 SElinux
setenforce 0
修改主机名称
hostnamectl set-hostname zabbix
(2)部署安装
官方地址与部署文档:https://www.zabbix.com/cn/download
根据自身系统版本和所需进行选择
1. 关闭无关的库(因为安装zabbix所需组件时候,如果不关闭其他库,系统会去其他库中寻找相关组件,导致版本不对等问题,使得安装出现失败)
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/epel.repo
2. 获得zabbix下载yum源
rpm -Uvh https://repo.zabbix.com/zabbix/7.2/release/rocky/9/noarch/zabbix-release-latest-7.2.el9.noarch.rpm
注意:在配置 zabbix 源的出现的错误
Retrieving https://repo.zabbix.com/zabbix/7.2/release/rocky/9/noarch/zabbix-release-latest-7.2.el9.noarch.rpm
warning: /var/tmp/rpm-tmp.5lmWZj: Header V4 RSA/SHA512 Signature, key ID b5333005: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-7.0-5.el9 ################################# [100%]
这是由于yum安装了旧版本的GPG keys造成的,执行如下:
rpm --import /etc/pki/rpm-gpg/RPM*
查询已安装的rpm源
[root@zabbix ~]# rpm -qa | grep zabbix
zabbix-release-7.0-5.el9.noarch
删除源
rpm -e zabbix-release-7.0-5.el9.noarch
3. 清除原理yum源缓存并加载新的yum
yum clean all&&yum makecache
4. 安装Zabbix server,Web前端,agent
dnf install zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent
5. 安装并开启数据库
yum install -y mariadb-server mariadb
systemctl enable --now mariadb
6.MariaDB安全化向导流程
[root@zabbix ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none): #输入root的当前密码(初始为空,直接回车)
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] y #切换到unix_socket身份验证
Enabled successfully!
Reloading privilege tables..
... Success!
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] y #修改 root 密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] n #删除匿名用户
... skipping.
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y #不允许远程root登录(如果 zabbix 服务器与数据库服务器分离需要选择 no
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y #删除测试数据库
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
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!
7. 创建初始数据库,在数据库主机上运行以下代码
[root@zabbix ~] # mysql -uroot -p
your password
mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
mysql> create user zabbix@localhost identified by 'password';
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> set global log_bin_trust_function_creators = 1;
mysql> quit;
8. 导入初始架构和数据,系统将提示您输入新创建的密码
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix
9. 导入数据库架构后禁用log_bin_trust_function_creates选项
mysql -uroot -p
your password
mysql> set global log_bin_trust_function_creators = 0;
mysql> quit;
10. 修改 zabbix server 配置文件
[root@zabbix ~]# vim /etc/zabbix/zabbix_server.conf
ListenPort=10051 #大约在12行附近
SourceIP=192.168.123.167 #大约在19行附近
DBHost=192.168.123.167 #大约在93行附近
DBPassword=root #大约在129行附近
DBPort=3306 #大约在146行附近
11. 修改Agent配置
[root@zabbix ~]# vim /etc/zabbix/zabbix_agentd.conf
SourceIP=192.168.123.167 #大约在64行附近,注意这个设置要和server配置中的SourceIP一致
Server=192.168.123.167 #大约在117行附近
ListenPort=10050 #大约在125行附近
ServerActive=192.168.123.167 #大约在171行附近
Hostname=zabbix #大约在182行附近
12. 为Zabbix前端配置PHP
[root@zabbix ~]# vim /etc/nginx/conf.d/zabbix.conf
listen 8080;
server_name example.com;
13. 启动Zabbix server和agent进程,并为它们设置开机自启
systemctl restart zabbix-server zabbix-agent nginx php-fpm
systemctl enable zabbix-server zabbix-agent nginx php-fpm
14.Web 安装
初始用户名:Admin,初始密码:zabbix