一、Zabbix概述
作为一个运维,需要会使用监控系统查看服务器状态以及网站流量指标,利用监控系统的数据去了解上线发布的结果,和网站的健康状态。
利用一个优秀的监控软件,我们可以:
- 通过一个友好的界面进行浏览整个网站所有的服务器状态
- 可以在Web 前端方便的查看监控数据
- 可以回溯寻找事故发生时系统的问题和报警情况
1、zabbix是什么
官方网址:www.zabbix.com
官方文档:https://www.zabbix.com/cn/manuals
- zabbix是一个基于Web 界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
- zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。
- zabbix由两部分构成,zabbix server(端口:10051)与可选组件zabbix agent(端口:10050)。通过C/S模式采集数据,通过B/S模式在Web端展示和配置。
- **zabbix server 可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,**它可以运行在Linux等平台上。
- zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。
Server
Zabbix Server需运行在LAMP(Linux+Apache+Mysql+PHP)环境下,对硬件要求低
agent
目前已有的agent基本支持市面常见的OS,包含Linux、HPUX、Solaris、Sun、windows等
SNMP
支持各类常见的网络设备
2、Zabbix原理及流程图

- zabbix agent安装在被监控的主机上,zabbix agent负责定期收集客户端本地各项数据,并发送至 zabbix server端
- zabbix server收到数据后,将数据存储到数据库中
- 用户基于 Zabbix WEB 可以看到数据在前端展现图像
- 当zabbix监控某个具体的项目, 该项目会设置一个触发器阈值,当被监控的指标超过该触发器设定的阈值,会进行一些必要的动作,包括发送信息(邮件、微信、短信)、发送命令(shell 命令、reboot、restart、install 等)
3、Zabbix的优点
- 开源,无软件成本投入
- Server对设备性能要求低
- 支持设备多,自带多种监控模板
- 支持分布式集中管理,有自动发现功能,可以实现自动化监控
- 开放式接口,扩展性强,插件编写容易
- 当监控的 item 比较多服务器队列此较大时可以采用主动状态,被监控客户端主动从server端去下载需要监控的item然后取数据上传到server端。这种方式对服务器的负载比较小。
- Api的支持,方便与其他系统结合
4、Zabbix的缺点
- 需在被监控主机上安装agent,所有数据都存在数据库里,产生的数据据很大,瓶颈主要在数据库。
- 项目批量修改不方便。
- 社区虽然成熟,但是中文资料相对较少,服务支持有限。
- 入门容易,能实现基础的监控,但是深层次需求需要非常熟悉Zabbix并进行大量的二次定制开发难度较大。
- 系统级别报警设置相对比较多,如果不筛选的话报警邮件会很多;且自定义的项目报警需要自己设置,过程比较繁琐。
- 缺少数据汇 总功能,如无法查看-组服务器平均值, 需进行二次开发;
5、Zabbix监控模式
**被动监控模式:**server向agent 发起连接
**主动模式模式:**agent向server 发起连接
6、Zabbix监控架构
在实际监控架构中,zabbix根据网络环境、监控规模等分了三种架构:server-client 、master-node-client、server-proxy-client
1.server-client 架构

2.server-proxy-client 架构

3.master-node-client

7、Zabbix的程序组件
zabbix 监控部署在系统中,包含常见的五个程序: zabbix_server、zabbix_agent、zabbix_proxy、zabbix_get、zabbix_sender 等。
- zabbix server:zabbix 服务端守护进程,其中 zabbix_agent、zabbix_get、zabbix_sender、zabbix_proxy 的数据最终都提交给 zabbix server;
- zabbix agent:客户端守护进程,负责收集客户端数据,例如:收集 CPU 负载、内存、硬盘使用情况等;
- zabbix proxy:zabbix 分布式代理守护进程,通常大于 500 台主机,需要进行分布式监控架构部署;
- zabbix get:zabbix 数据接收工具,单独使用的命令,通常在 server 或者 proxy 端执行获取远程客户端信息的命令;
- zabbix sender:zabbix 数据发送工具,用户发送数据给 server 或 proxy 端,通常用户耗时比较长的检查。
二、部署Zabbix服务端(端口:10051)
环境准备
部署zabbix的zabbix-server内存 至少2G ,推荐4G。
zabbix 服务端同时安装zabbix-server-mysql和zabbix-agent,因为服务端所在主机自己也要被监控。
节点 | IP地址 | 安装服务 |
---|---|---|
zabbix-server | 192.168.10.100 | zabbix-server-mysql 、zabbix-agent |
zabbix-agent | 192.168.10.101 | zabbix-agent2 |
1、关闭防火墙及核心防护 修改主机名
systemctl stop firewalld ##关闭防火墙
systemctl disable --now firewalld ##开启开机不自启
setenforce 0 ##关闭核心防护
hostnamectl set-hostname zbx-server ##修改主机名
2、获取zabbix下载资源
rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
cd /etc/yum.repos.d
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' zabbix.repo
yum clean all
yum makecache
yum install zabbix-server-mysql zabbix-agent -y



3、安装SCL(Software Collections),修改 Zabbix-front 前端源
便于后续安装高版本的 php,默认 yum 安装的 php 版本为 5.4,版本过低,zabbix 5.0 版本对 php 版本最低要 7.2.0 版本。SCL 可以使得在同一台机器上使用多个版本的软件,而又不会影响整个系统的依赖环境。软件包会安装在 /opt/rh 目录下。
[root@zbx-server yum.repos.d]#yum install -y centos-release-scl
[root@zbx-server yum.repos.d]#vim zabbix.repo
[root@zbx-server yum.repos.d]#yum install -y zabbix-web-mysql-scl zabbix-apache-conf-scl




4、安装 zabbix 所需的数据库
yum install -y mariadb-server mariadb
systemctl enable --now mariadb
mysql_secure_installation #初始化数据库,并设置密码,如 abc123



5、添加数据库用户,以及 zabbix 所需的数据库信息
mysql -u root -pabc123
show databases;

CREATE DATABASE zabbix character set utf8 collate utf8_bin;
GRANT all ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix';
flush privileges;
show databases;


6、导入数据库信息
rpm -ql zabbix-server-mysql #查询 sql 文件的位置
zcat /usr/share/doc/zabbix-server-mysql-5.0.42/create.sql.gz | mysql -uroot -pabc123 zabbix




7、修改 zabbix server 配置文件,修改数据库的密码
vim /etc/zabbix/zabbix_server.conf
......
DBPassword=zabbix #124行,指定 zabbix 数据库的密码


8、修改 zabbix 的 php 配置文件
vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
-----------------------------------------------
php_value[date.timezone] = Asia/shanghai


9、启动Zabbix相关服务
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

10、浏览器访问验证
浏览器访问:http://192.168.10.100/zabbix,点击下一步







设置文件界面:点击左边菜单栏的【User settings】,【Language】选择 Chinese(zh_CN),再点击 Update 更新


中文模式下产生乱码

11、解决 Zabbix-server Web页面中文乱码问题


三、部署Zabbix客户端(端口:10050)
zabbix 5.0 版本采用 golang 语言开发的新版本客户端 agent2 。
zabbix 服务端 zabbix_server 默认使用 10051 端口,客户端 zabbix_agent2 默认使用 10050 端口。
1、环境准备
[root@localhost ~]#setenforce 0
[root@localhost ~]#systemctl disable --now firewalld
[root@localhost ~]#hostname zbx-agent1
2、服务端和客户端都配置时间同步
yum install -y ntpdate
ntpdate -u ntp.aliyun.com




3、客户端配置时区,要与服务端保持一致
mv /etc/localtime{,.bak}
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
date
4、设置 zabbix 的下载源,安装 zabbix-agent2
rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
cd /etc/yum.repos.d
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo


5、修改 agent2 配置文件,并修改配置文件
[root@zbx-agent1 yum.repos.d]#yum install -y zabbix-agent2 #安装zabbix-agent2
[root@zbx-agent1 yum.repos.d]#vim /etc/zabbix/zabbix_agent2.conf
Server=192.168.10.130 #80行,指定 zabbix 服务端的 IP 地址
ServerActive=192.168.10.130 #120行,指定 zabbix 服务端的 IP 地址
Hostname=zbx-agent1 #131行,指定当前 zabbix 客户端的主机名
#修改完保存退出




6、启动 zabbix-agent2
systemctl start zabbix-agent2.service
systemctl enable zabbix-agent2.service
systemctl status zabbix-agent2.service
netstat -natp | grep zabbix

7、在服务端验证 zabbix-agent2 的连通性
[root@zbx-server ~]#yum install -y zabbix-get ##安装 zabbix 主动获取数据的命令
[root@zbx-server ~]#zabbix_get -s "192.168.10.101" -p 10050 -k "agent.ping" ##服务端对客户端进行连通性测试,返回1表示连通
1
[root@zbx-server ~]#zabbix_get -s "192.168.10.101" -p 10050 -k "system.hostname" ##system.hostname返回客户端主机名,表示连通正常
zbx-agent1


8、将客户端加入服务端的监控主机
在Web界面添加agent主机
(1)点击左边菜单栏【配置】中的【主机】,点击【创建主机】


(2)再点击上方菜单栏【模板】






敬请期待后续