1.Zabbix概述
作为一个运维,需要会使用监控系统查看服务器状态以及网站流量指标,利用监控系统的数据去了解上线发布的结果,和网站的健康状态。
利用一个优秀的监控软件,我们可以:
-
通过一个友好的界面进行浏览整个网站所有的服务器状态
-
可以在Web 前端方便的查看监控数据
-
可以回溯寻找事故发生时系统的问题和报警情况
1.1 Zabbix是什么
-
zabbix是一个基于Web界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
-
zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。
-
zabbix由2部分构成,zabbix server 与可选组件zabbix agent。 通过c/s 模式采集数据,通过B/s模式在web端展示和配置。
-
zabbix server 可以通过SNMP(简单网络管理协议),zabbix agent,ping, 端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它 可以运行在Linux等平台上。(支持多个平台,windows也支持)
-
zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。

1.2 zabbix监控原理
zabbix agent安装在被监控的主机上,zabbix agent 负责定期收集客户端本地各项数据,并发送至zabbix server 端,zabbix server收到数据后,将数据存储到数据库中,用户基于zabbix WEB可以看到数据在前端展现图像。
当zabbix 监控某个具体的项目,该项目会设置一个触发器阈值,当被监控的指标超过该触发器设定的阈值,会进行一些必要的动作,动作包括:发送信息(邮件、微信、短信)、发送命令(shell 命令、reboot、 restart、 install 等)。
用户可以基于zabbix-web可以在WEBUI界面中查看展现的数据图像,以及进行相关的配置管理用户还可以在WEBUI界面中设置监控项的触发器,如被监控的数据指标超过触发器设定的阈值,会进行发送通知信息或者一些应急操作指令。
1.3 zabbix常见的五个组件
zabbix监控部署在系统中,包含常见的五个程序: zabbix server、 zabbix agent、 zabbix proxy、zabbix get、zabbix sender 等。
(1) zabbix server: zabbix 服务端守护进程,其中zabbix_agent、 zabbix_ get、zabbix_sender、 zabbix_proxy的数据最终都提交给zabbix server;
(2) zabbix agent: 客户端守护进程,负责收集客户端数据,例如:收集CPU负载、内存、硬盘使用情况等;
(3)zabbi xproxy: zabbix分布式代理守护进程,通常大于500台主机,需要进行分布式监控架构部署;
(4)zabbix get: zabbix 数据接收工具,单独使用的命令,通常在server 或者proxy端执行获取远程客户端信息的命令;
(5)zabbix sender: zabbix 数据发送工具,用户发送数据给server 或proxy端,通常用户耗时比较长的检查。
1.4 zabbix端口号
- zabbix服务端zabbix_server 默认使用10051 端口。
- 客户端zabbix_agent2 默认使用10050 端口。
2.部署zabbix服务器
2.1部署zabbix服务端(端口号10051)
1.先关闭防火墙并修改主机名

2.安装nginx并修改nginx的配置文件如下
ini
vim /etc/nginx/conf.d/zbx.conf
server {
listen 80;
server_name zbx.kgc.com;
root /var/www/zbx;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/zbx$fastcgi_script_name;
include fastcgi_params;
}
}


3.安装php,并修改配置文件
bash
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache php72w-ldap php72w-bcmath


ini
vim /etc/php.ini
max_execution_time = 300 # 368行
max_input_time = 600 # 378行
post_max_size = 80M # 656行
date.timezone = Asia/Shanghai # 877行



4.去到/var/www/zbx文件下创建一个测试页面

5.启动nginx与php到浏览器测试是否配置成功


6.安装mariadb数据库
ini
cat > /etc/yum.repos.d/mariadb.repo << EOF
[mariadb]
name = MariaDB
baseurl = http://mirrors.aliyun.com/mariadb/yum/10.5/centos7-amd64/
gpgkey = http://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
enabled=1
EOF

7.初始化数据库



8.创建数据库并指定字符集
sql
CREATE DATABASE zabbix character set utf8 collate utf8_bin;

9.创建zabbix数据库用户并授权
sql
GRANT all ON zabbix.* TO 'zabbix'@'localhost' IDENTIFIED BY 'zabbix';
GRANT all ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix';
flush privileges;

10 向数据库导入 zabbix数据,先将我们的数据包解压

11.进入database文件夹并将mysql文件夹内的文件按顺序导入到mariadb数据库中


12.编译安装zabbix server服务端,先安装依赖环境,创建zabbix用户
bash
yum install -y mysql-devel pcre-devel openssl-devel zlib-devel libxml2-devel net-snmp-devel net-snmp libssh2-devel OpenIPMI-devel libevent-devel openldap-devel libcurl-devel fping gcc gcc-c++ make
useradd -s /sbin/nologin -M zabbix

- 进入软件包进行编译安装
css
cd /opt/zabbix-6.0.13/
./configure \
--sysconfdir=/etc/zabbix/ \
--enable-server \
--with-mysql \
--with-net-snmp \
--with-libxml2 \
--with-ssh2 \
--with-openipmi \
--with-zlib \
--with-libpthread \
--with-libevent \
--with-openssl \
--with-ldap \
--with-libcurl \
--with-libpcre
make install
#注意这边直接make install,不需要make


14.检查一下版本查看是否安装好了
css
zabbix_server --version

15.修改 zabbix server 配置文件,修改数据库的密码



16 准备 systemctl 服务管理文件
ini
cat > /usr/lib/systemd/system/zabbix-server.service << EOF
[Unit]
Description=Zabbix Server with MySQL DB
After=syslog.target network.target mysqld.service
[Service]
Type=simple
ExecStart=/usr/local/sbin/zabbix_server -f
User=zabbix
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now zabbix-server
netstat -lntp | grep 10051 #zabbix_server 默认监听 10051 端口


17.部署 Web 前端,进行访问
bash
cp -r /opt/zabbix-6.0.13/ui/* /var/www/zbx
chown -R nginx.nginx /var/www/zbx







18.安装agent组件,实现 zabbix 服务端自我监控,先到官网中查看安装文件
bash
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm

yum install zabbix-agent2 zabbix-agent2-plugin-*

bash
#启动服务
systemctl start zabbix-agent2.service


19.解决 zabbix-server Web页面中文乱码问题


bash
yum install -y wqy-microhei-fonts
\cp -f /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /var/www/zbx/assets/fonts/DejaVuSans.ttf


20 让zabbix也对别的服务器进行监控,先设置服务器的时间与zabbix服务器同步
ntpdate ntp.aliyun.com


21.修改agent服务器的主机名与zabbix服务器的主机名


- 修改两台的/etc/hosts文件
vbscript
192.168.223.103 zabbix-server
192.168.223.100 zbx-agent01

23.将zabbix服务器的zabbix源发送给我们agent服务器
bash
scp zabbix* zbx-agent01:`pwd`

24.直接安装agent
bash
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm
yum install zabbix-agent2 zabbix-agent2-plugin-* -y

25.修改/etc/zabbix中zabbix_agent2.conf 配置文件



26.启动服务

27.在服务端验证 zabbix-agent2 的连通性,先安装zabbix-get工具
csharp
yum install -y zabbix-get
zabbix_get -s '192.168.223.100' -p 10050 -k 'agent.ping'
#agent.ping为键值
#常用的键值
agent.ping
#服务端与客户端是否连通,返回1表示可达,返回非表示不可达
system.hostname
#系统主机名
agent.hostname
#客户端主机名
net.if.in[if,<mode>]
#网络接口进入的流量统计,if表示网卡名称,带<>的参数表示可以省略
net.if.out[if,<mode>]
#网络接口流出的流量统计
proc.num[<name>,<user>,<state>,<cmdline>,<zone>]
#进程数
net.tcp.port[<ip>,port]
#检查是否能建立tcp连接到指定端口,返回0表示不能连接,返回1表示可以连接

28.在我们的web界面中配置我们的客户端


