[Distributed] zabbix 6.0 deployment explanation

1. Prologue

As an operation and maintenance, you need to use the monitoring system to check the server system performance, application service status and website traffic indicators, etc., and use the data of the monitoring system to understand the results and health status of the website's online release.

With a good monitoring software, we can:

  • Browse all server status of the entire website through a friendly interface
  • The monitoring data can be easily viewed on the front end of the web
  • Can look back to find system problems and alarms when an accident occurs

Two, zabbix concept

2.1 What is zabbix?

  • Zabbix is ​​an enterprise-level open source solution that provides distributed system monitoring and network monitoring functions based on a web interface.
  • Zabbix can monitor various network parameters to ensure the safe operation of the server system; and provide a flexible notification mechanism to allow system administrators to quickly locate/solve various problems.
  • zabbix consists of 2 parts, zabbix server and optional component zabbix agent. Collect data through C/S mode, and display and configure it on the Web side through B/S mode.
  • zabbix server can provide remote server/network status monitoring, data collection and other functions through zabbix agent, SNMP protocol, port monitoring and other methods, and it can run on platforms such as Linux.
  • The zabbix agent needs to be installed on the monitored target server. It mainly completes the collection of hardware information or memory, CPU and other information related to the operating system.

2.2 zabbix monitoring principle

The zabbix agent is installed on the monitored host. The zabbix agent is responsible for regularly collecting local data from the client and sending it to the zabbix server. After receiving the data, the zabbix server stores the data in the database. Users can see it based on Zabbix Web Data presents images on the front end. When zabbix monitors a specific project, the project will set a trigger threshold. When the monitored indicator exceeds the threshold set by the trigger, some necessary actions will be taken, including: sending information (email, WeChat, SMS) ), sending commands (shell commands, reboot, restart, install, etc.).

2.3 New features of zabbix 6.0

  1. Zabbix server high availability prevents hardware failure or downtime during planned maintenance periods:
  • Native opt-in HA cluster configuration
  • Define one or more standby nodes
  • Monitor the status of Zabbix server cluster nodes in real time
  • Configure Zabbix server in HA cluster mode without external tools
  1. Zabbix 6.0 LTS has added a Kubernetes monitoring function, which can collect indicators from multiple dimensions in the Kubernetes system:
  • Automatic discovery and monitoring of Kubernetes nodes and pods
  • Agentless collection of information about Kubernetes pods and nodes
  • Obtain high-level information about Kubernetes node hosts

2.4 zabbix 6.0 functional components

  • Zabbix Server
    • The zabbix server daemon is the core component of the Zabbix software, and the Zabbix Agent reports availability, system integrity information, and statistics to it.
    • Zabbix Server is also the core repository for all configuration, statistics and operational information.
    • Zabbix Server is also the alarm center of the Zabbix monitoring system. Any anomalies in the monitored systems will be notified to the administrator.

The functionality of the basic Zabbix Server is broken down into three distinct components. They are: Zabbix server, Web frontend, database.

All configuration information of Zabbix is ​​stored in the database where the Server interacts with the Web front end. For example, when you add a monitoring item through the web front end (or API), it will be added to the monitoring item table in the database. Then, Zabbix server queries the valid item in the monitoring item table once per minute, and then stores it in the cache in Zabbix server. That's why any changes made by the Zabbix frontend take around two minutes to show up in the latest data section.

  • Database
    All configuration information and data collected by Zabbix are stored persistently in the database.
    Can support MySQL, PostgreSQL, Oracle, DB2, TimescaleDB and other databases.

  • Web interface
    Web interface is a part of Zabbix Server, used to realize the display and configuration interface. Usually (but not necessarily) on the same physical machine as the Zabbix server.
    Based on Apache/Nginx + PHP implementation, it only supports LAMP architecture in the early stage, and supports LNMP since Zabbix5.0.

  • Zabbix Agent
    client daemon, deployed on the monitored target, is used to actively monitor local resources and applications, and send the collected data to Zabbix Server. Zabbix Agent2 is supported from Zabbix5.0.

  • Zabbix Proxy
    zabbix distributed proxy daemon process, which can collect performance and availability data instead of Zabbix Server. The deployment of Zabbix Proxy in Zabbix is ​​an optional part.
    The deployment of Zabbix Proxy can well share the load of a single Zabbix Server.
    It is usually used when monitoring more than 500 hosts, and a distributed monitoring architecture deployment is required.

  • Java Gateway
    Zabbix To monitor Tomcat service or other JAVA programs (such as Elasticsearch, ZooKeeper), you need to use Java Gateway as an agent to obtain data from JAVA programs.

Three, zabbix 6.0 deployment

  • System: CentOS 7 does not support yum installation of Zabbix 6.0 server

  • Installation method: The Zabbix server adopts the method of compiling and installing, and the zabbix client adopts the method of yum installation

Deploy the server

//关闭 selinux 与防火墙
systemctl disable --now firewalld
setenforce 0
hostnamectl set-hostname zbx-server

insert image description here

3.1 Deploy Nginx + PHP environment and test

3.1.1 Install nginx

#安装 nginx
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

yum install -y nginx

3.1.2 install php

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

3.1.3 Modify Nginx configuration

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;
  }
}

insert image description hereinsert image description here
insert image description here

3.1.4 Modify PHP configuration

vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx

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

insert image description hereinsert image description hereinsert image description hereinsert image description here

3.1.5 Create directory and test file

mkdir -p /var/www/zbx

vim /var/www/zbx/index.php
<?php
phpinfo();
?>


#启动服务
systemctl enable --now nginx php-fpm

#测试访问(hosts解析)
修改 C:\Windows\System32\drivers\etc\hosts
192.168.80.20 zbx.kgc.com

浏览器访问:http://zbx.kgc.com/index.php

insert image description here

insert image description here

3.2 Deploying the database requires MySQL 5.7 or Mariadb 10.5 and above

#配置 Mariadb yum源
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

yum install -y mariadb-server mariadb

systemctl enable --now mariadb
#初始化数据库
mysql_secure_installation
分别输入 回车 -> n -> Y (设置root密码,如abc123) -> 后面一路 Y

mysql -u root -pabc123

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

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

insert image description hereinsert image description here

#向数据库导入 zabbix 数据
上传源码包 zabbix-6.0.13.tar.gz 到 /opt 目录
cd /opt
tar xf zabbix-6.0.13.tar.gz

ls /opt/zabbix-6.0.13/database/mysql
data.sql  double.sql  history_pk_prepare.sql  images.sql  Makefile.am  Makefile.in  schema.sql

#按照顺利导入数据库
cd /opt/zabbix-6.0.13/database/mysql
mysql -uroot -pabc123 zabbix < schema.sql
mysql -uroot -pabc123 zabbix < images.sql
mysql -uroot -pabc123 zabbix < data.sql
mysql -uroot -pabc123 zabbix < double.sql
mysql -uroot -pabc123 zabbix < history_pk_prepare.sql

insert image description here
insert image description here

3.3 Compile and install zabbix Server server

#安装依赖包,创建 zabbix 用户
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

#编译安装
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

#检查版本
zabbix_server --version
zabbix_server (Zabbix) 6.0.13

insert image description here

#修改 zabbix server 配置文件,修改数据库的密码
vim /etc/zabbix/zabbix_server.conf 
......
LogFile=/var/log/zabbix_server.log		# 38行,指定 zabbix 日志路径
DBPassword=zabbix					# 123行,指定 zabbix 数据库的密码

insert image description here
insert image description here

#准备 systemctl 服务管理文件
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

touch /var/log/zabbix_server.log
chown -R zabbix:zabbix /var/log/zabbix_server.log
systemctl daemon-relead
systemctl enable --now zabbix-server

netstat -lntp | grep 10051			#zabbix_server 默认监听 10051 端口


insert image description here

3.4 Deploy the web front end for access

cp -r /opt/zabbix-6.0.13/ui/* /var/www/zbx

chown -R nginx.nginx /var/www/zbx

浏览器访问:http://zbx.kgc.com/ 
【Default language】选择 Chinese(zh_CN),点击下一步
【密码】输入 zabbix,点击下一步
【Zabbix主机名称】输入 Zabbix-监控;【默认时区】选择 Asia/Shanghai,点击下一步

安装完成后,默认的登录账号和密码为:Admin/zabbix

insert image description here

insert image description hereinsert image description here
insert image description here
insert image description here

insert image description here

3.4 Install zabbix client to realize self-monitoring of zabbix server

rpm -ivh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm
sed -i 's#https://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

#zabbix 5.0 版本开始采用 golang 语言开发的新版本客户端 agent2
yum install -y zabbix-agent2

systemctl enable --now zabbix-agent2

netstat -lntp | grep 10050			#客户端 zabbix_agent2 默认监听 10050 端口


//解决 zabbix-server Web页面中文乱码问题
yum install -y wqy-microhei-fonts

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

刷新浏览器页面

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

3.5 Add zabbix client host

systemctl disable --now firewalld
setenforce 0
hostnamectl set-hostname zbx-agent01


//服务端和客户端都配置时间同步
yum install -y ntpdate
ntpdate -u ntp.aliyun.com


//服务端和客户端都设置 hosts 解析
cat > /etc/hosts << EOF
192.168.80.20 zbx-server
192.168.80.30 zbx-agent01
EOF

//设置 zabbix 的下载源,安装 zabbix-agent2
rpm -ivh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm
sed -i 's#https://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

yum install -y zabbix-agent2

//修改 agent2 配置文件
vim /etc/zabbix/zabbix_agent2.conf
......
Server=192.168.137.101			#80行,指定 zabbix 服务端的 IP 地址
ServerActive=192.168.137.101	#133行,指定 zabbix 服务端的 IP 地址
Hostname=zbx-agent01			#144行,指定当前 zabbix 客户端的主机名


//启动 zabbix-agent2
systemctl start zabbix-agent2
systemctl enable zabbix-agent2

netstat -natp | grep zabbix
tcp6       0      0 :::10050                :::*                    LISTEN      43654/zabbix_agent2 

insert image description here
insert image description here
insert image description here

//在服务端验证 zabbix-agent2 的连通性
yum install -y zabbix-get				#安装 zabbix 主动获取数据的命令

zabbix_get -s '192.168.137.102' -p 10050 -k 'agent.ping'
1

zabbix_get -s '192.168.137.102' -p 10050 -k 'system.hostname'
zbx-agent01

insert image description here

#常用的键值
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表示可以连接

//在 Web 页面中添加 agent 主机
点击左边菜单栏【配置】中的【主机】,点击【创建主机】
【主机名称】输入 zbx-agent01
【可见的名称】输入 zbx-agent01-192.168.137.102
【模板】搜索 Linux ,选择 Linux by Zabbix agent
【群组】选择 Linux servers
【Interfaces】点击添加 客户端,【IP地址】输入 192.168.137.102


#监控模板下载地址
https://share.zabbix.com/
https://monitoringartist.github.io/zabbix-searcher/
https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates

insert image description here
insert image description hereinsert image description here

Customize monitoring content

Case: Custom monitoring of the number of people logged in on the client server
Requirement: Limit the number of people logged in to no more than 3, and send an alarm message if more than 3

Create a custom key on the client

1.明确需要执行的 linux 命令
who | wc -l  统计当前登入了多少终端

insert image description here

2.创建 zabbix 的监控项配置文件,用于自定义 key
vim /etc/zabbix/zabbix_agent2.conf
#可以将自定义的监控项配置文件创建在 zabbix_agent2.d 目录中
281 Include=/etc/zabbix/zabbix_agent2.d/*.conf
#自定义监控项的格式如下
321 #	Format: UserParameter=<key>,<shell command>

cd /etc/zabbix/zabbix_agent2.d/

vim UserParameter_login.conf
UserParameter=login.user,who|wc -l

systemctl restart zabbix-agent2

insert image description here
insert image description here

3.在服务端验证新建的监控项
zabbix_get -s '192.168.80.30' -p 10050 -k 'login.user'

insert image description here

Create custom monitoring item templates on web pages

1.创建模板
点击左边菜单栏【配置】中的【模板】,点击【创建模板】
【模板名称】设置成 Template Login User
【可见的名称】设置成 Template Login User
【群组】选择 Template
【描述】可自定义
点击 【添加】,此时就可在【名称】中搜索到 Template Login User 了


insert image description here
insert image description here
insert image description here

2.创建监控项
点击 Template Login User 模板进入
点击上方菜单栏【监控项】,点击【创建监控项】
【名称】设置成 Number of login users
【键值】设置成 login.user			#键值必须要与自定义的监控项配置文件中设置的保持一致
【更新间隔】设置成 10s
【历史数据保留时长】Storage period	30d		#保留时间可自定义设置
点击 【添加】

insert image description here
insert image description hereinsert image description here

3.创建触发器(当监控项获取到监控的值后和触发器预设的值进行对比,判断是否报警)
点击上方菜单栏【触发器】,点击【创建触发器】
【名称】设置成 Number of login users is greater than 3
【严重性】设置成 一般严重		#根据严重程度可自定义设置
【表达式】点击添加,【监控项】点击选择 Number of login users,【功能】选择 last(),【结果】选择 > 3,点击 【插入】
点击 【添加】

insert image description here
insert image description here

5.创建图形
点击上方菜单栏【图形】,点击【创建图形】
【名称】设置成 Number of login users
【宽】、【高】可直接采用默认值
【监控项】点击添加勾选相关监控项 Number of login users,【功能】选择 最大,其它可保持默认值
点击 【添加】

insert image description here

6.将主机与模板关联起来(一个主机可以关联多个模板)
点击左边菜单栏【配置】中的【主机】,点击你要关联的主机
【模板】搜索 login,选择 Template Login User,点击【更新】

此时就点击【监测】中的【主机】,点击你关联主机的【图形】,即可查看到相关的监控项指标

insert image description here

insert image description here

7.设置邮件报警
点击左边菜单栏【管理】中的【报警媒介类型】,点击【创建媒体类型】
【名称】设置成 qq_Email
【SMTP服务器】设置成 smtp.qq.com
【SMTP服务器端口】设置成 25
【SMTP HELO】设置成 qq.com
【SMTP电邮】设置成 自己的邮箱地址,例如 [email protected]
【认证】选择 用户名和密码
【用户名称】设置成 自己的邮箱地址,例如 [email protected]
【密码】可登录QQ邮箱页面,点击【设置】-->【账户】中的【生成授权码】,通过短信获取授权码
【描述】可自定义
点击上方菜单栏【Message templates】,点击【添加】,【Message type】选择 问题,点击【更新】
点击 【添加】,并测试功能

点击左边菜单栏【User settings】-->【Profile】-->【报警媒介】,点击【添加】
【类型】选择 qq_Email
【收件人】设置成 [email protected]
【当启用时】设置成 1-7,00:00-24:00
【如果存在严重性则使用】勾选需要的严重性
点击 【添加】
再点击 【更新】

点击左边菜单栏【配置】->【动作】->【Trigger actions】
选择相对应的动作名称点击进入,点击 【添加】
【类型】选择 触发器,【操作者】选择 等于,【触发器】点击选择 Nunber of login users is greater than 3
点击【添加】
勾选 【已启动】
点击 【更新】


//测试邮件报警
增加测试客户端的用户登录数超过触发器预设的值,查看【监测】-->【仪表板】,确认报警

insert image description here
insert image description here
insert image description here

insert image description here
insert image description here

zabbix automatic discovery and automatic registration

zabbix auto-discovery (passive mode for agent2)

zabbix server 主动的去发现所有的客户端,然后将客户端的信息登记在服务端上。
缺点是如果定义的网段中的主机数量多,zabbix server 登记耗时较久,且压力会较大。

systemctl disable --now firewalld
setenforce 0
hostnamectl set-hostname zbx-agent02

1.确保客户端上的 zabbix-agent2 服务状态正常
systemctl is-active zabbix-agent2.service 
active


2.在 Web 页面删除原有的客户端主机
点击左边菜单栏【配置】中的【主机】,勾选原有的客户端主机,点击 删除


3.在服务端和客户端上配置 hosts 解析
vim /etc/hosts
192.168.80.20 zbx-server
192.168.80.30 zbx-agent01
192.168.80.40 zbx-agent02


4.在 Web 页面配置自动发现
点击左边菜单栏【配置】中的【自动发现】,点击【创建发现规则】
【名称】设置成 mynetwork
【IP范围】设置成 192.168.80.1-254
【更新间隔】设置成 30s
【检查】点击【添加】,【检查类型】选择 Zabbix 客户端,【端口范围】设置成 10050,【键值】设置成 system.uname
【设备唯一性准则】选择 IP地址
【主机名称】选择 DNS名称
【可见的名称】选择 主机名称
勾选 【已启用】,点击 【添加】

点击左边菜单栏【配置】中的【动作】,上方菜单选择 【发现动作】
勾选 【Auto discovery. Linux servers.】,点击 【启用】

点击左边菜单栏【配置】中的【主机】刷新,等待一段时间后即可刷新出自动发现的客户端主机

可在服务端查看 zabbix 日志
tail -f /var/log/zabbix_server.log
......
  6601:20210922:225044.115 enabling Zabbix agent checks on host "zbx-agent02": interface became available

insert image description here
insert image description here
insert image description hereinsert image description hereinsert image description here

insert image description here

insert image description here
insert image description here

Automatic registration (active mode for agent2)

zabbix agent2 will actively report its own information and send it to zabbix server.
The disadvantage is that zabbix agent2 may not be able to find the zabbix server due to incorrect configuration of the configuration file or network failure.

1.环境准备
点击左边菜单栏【配置】中的【发现动作】,勾选发现规则,点击 禁用
点击左边菜单栏【配置】中的【主机】,勾选原有的客户端主机,点击 删除

vim /etc/hosts
192.168.137.101 zbx-server
192.168.137.102 zbx-agent01
192.168.137.103 zbx-agent02

2.修改 zabbix-agent2 配置文件
vim /etc/zabbix/zabbix_agent2.conf
......
HostnameItem=system.hostname		#152行,取消注释

egrep -v "^#|^$" /etc/zabbix/zabbix_agent2.conf 
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=192.168.137.101
ServerActive=192.168.137.101
Hostname=zbx-agent01
HostnameItem=system.hostname
Include=/etc/zabbix/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.sock

systemctl restart zabbix-agent2


3.在 Web 页面配置自动注册
点击左边菜单栏【配置】中的【动作】,上方菜单选择 【自动注册动作】,点击【创建动作】
【名称】设置成 Auto registration
点击 【添加】,【类型】选择 主机名称,【操作者】选择 包含,【值】设置成 zbx-agent
点击上方菜单栏【操作】,点击【添加】,【操作类型】选择 添加主机,点击 【Add】
再点击【添加】,【操作类型】选择 添加到主机群组,【主机群组】选择 Linux servers,点击 【Add】
再点击【添加】,【操作类型】选择 与模板关联,【模板】搜索 Linux,选择 Linux by Zabbix agent,点击 【Add】
点击下方的【添加】

等待一段时间后,点击左边菜单栏【配置】中的【主机】刷新,即可刷新出自动发现的客户端主机

在服务端查看 zabbix 日志
tail -f /var/log/zabbix_server.log

insert image description here
insert image description hereinsert image description hereinsert image description here

insert image description hereinsert image description here

Guess you like

Origin blog.csdn.net/2302_76410765/article/details/131554004