Zabbix

Stone大约 7 分钟

Zabbix

注意:

此文档对应的 Zabbix 版本为 5.0。

概述

Zabbix 是一个企业级的开源分布式监控解决方案。使用 Zabbix 监控服务器,网络设备,数据库,应用等各种 IT 设施,并在出现异常后向管理人员发送告警,以便第一时间做出响应,提升整体服务等级。

整体架构如下:

image-20230719163142152

Zabbix Server 获取服务器,网络设备,数据库以及应用的监控数据,如果监控对象较多或者处于不同的隔离网段,还可以使用 Zabbix Proxy 获取监控数据后传输给 Zabbix Server,Zabbix Server 最终会将数据存储在数据库中,然后就可以使用浏览器访问 Zabbix Web 界面,进行配置和查看监控数据。如果出现异常,则会通过电子邮件,短信或者电话通知相关人员。

安装

环境

硬件配置

根据需要监控的主机数量,建议配置如下:

Installation sizeMonitored metricsCPU/vCPU coresMemory (GiB)Database
Small1 00028MySQL Server, Percona Server, MariaDB Server, PostgreSQL
Medium10 000416MySQL Server, Percona Server, MariaDB Server, PostgreSQL
Large100 0001664MySQL Server, Percona Server, MariaDB Server, PostgreSQL, Oracle
Very large1 000 0003296MySQL Server, Percona Server, MariaDB Server, PostgreSQL, Oracle

支持平台

各组件支持的平台如下:

PlatformServerAgentAgent2
Linuxxxx
IBM AIXxx-
FreeBSDxx-
NetBSDxx-
OpenBSDxx-
HP-UXxx-
Mac OS Xxx-
Solarisxx-
Windows-xx

数据库

可以使用以下数据库存储监控数据:

SoftwareVersionComments
MySQL5.7.X-8.0.XRequired if MySQL is used as Zabbix backend database. InnoDB engine is required.
MariaDB (10.0.37-10.11.X) also works with Zabbix.
For MySQL we recommend using the C API (libmysqlclient)open in new window library for building server/proxy. For MariaDB we recommend using the MariaDB Connector/Copen in new window library for building server/proxy.
For MySQL versions 8.0.0-8.0.28 and MariaDB, see also: Possible deadlocksopen in new window.
Oracle11.2 or laterRequired if Oracle is used as Zabbix backend database.
PostgreSQL9.2.24 or laterRequired if PostgreSQL is used as Zabbix backend database. Depending on the installation size, it might be required to increase PostgreSQL work_mem configuration property (4MB being the default value), so that the amount of memory used by the database for particular operation is sufficient and query execution does not take too much time.
TimescaleDBFor Zabbix 5.0.0-5.0.9: 1.X, OSS (free, under Apache license) version Since Zabbix 5.0.10: 1.X, 2.0.1-2.10Required if TimescaleDB is used as a PostgreSQL database extension. Make sure to install TimescaleDB Community Edition, which supports compression.
Note that TimescaleDB 2.0.1-2.9 works only with PostgreSQL 11-14. PostgreSQL 15 is supported since TimescaleDB 2.10. You may also refer to the official documentationopen in new window for details regarding PostgreSQL and TimescaleDB version compatibility.
Added support for TimescaleDB versions:
- 2.6 and 2.7 since Zabbix 5.0.26;
- 2.8 since Zabbix 5.0.29;
- 2.9 since Zabbix 5.0.31;
- 2.10 since Zabbix 5.0.33;
- 2.11 since Zabbix 5.0.36.
SQLite3.3.5 or laterSQLite is only supported with Zabbix proxies. Required if SQLite is used as Zabbix proxy database.

建议使用 MySQLopen in new window 数据库,参考 Installing MySQL on Linuxopen in new window 安装数据库。如果 mysql.sock 文件不在 /var/lib/mysql/ 目录下,则创建一个对应的软连接。

[root@stone ~]# ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock

根据监控项数量以及历史数据保留时间计算所需磁盘空间:

ParameterFormula for required disk space (in bytes)
Zabbix configurationFixed size. Normally 10MB or less.
Historydays x (items / refresh rate) x 24 x 3600 x bytes
items : number of items
days : number of days to keep history
refresh rate : average refresh rate of items
bytes : number of bytes required to keep single value, depends on database engine, normally ~90 bytes.
Trendsdays x (items / 3600) x 24 x 3600 x bytes
items : number of items
days : number of days to keep history
bytes : number of bytes required to keep single trend, depends on database engine, normally ~90 bytes.
Eventsdays x events x 24 x 3600 x bytes
events : number of event per second. One (1) event per second in worst case scenario.
days : number of days to keep history
bytes : number of bytes required to keep single trend, depends on database engine, normally ~330 + average number of tags per event x 100 bytes.

总空间为以上四项之和。

端口

各组件默认使用的端口如下:

Zabbix componentPort numberProtocolType of connection
Zabbix agent10050TCPon demand
Zabbix agent 210050TCPon demand
Zabbix server10051TCPon demand
Zabbix proxy10051TCPon demand
Zabbix Java gateway10052TCPon demand

系统配置

系统版本:

[root@stone ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

关闭防火墙:

[root@stone ~]# systemctl stop firewalld.service 
[root@stone ~]# systemctl disable firewalld.service 

关闭 SELinux,修改文件 /etc/selinux/config,将 SELINUX=enforcing 修改为 SELINUX=disabled,然后重启主机:

[root@stone ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@stone ~]# init 6
[root@stone ~]# getenforce 
Disabled

配置地址映射:

[root@stone ~]# echo "192.168.44.135   stone" >> /etc/hosts
[root@stone ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.44.135   stone

安装 Zabbix Server

确保主机可以连接外网,在官方网站open in new window选择版本和平台:

image-20230720132947123

安装 Zabbix 仓库:

[root@stone ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
warning: /var/tmp/rpm-tmp.qLuWJO: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-release-5.0-1.el7         ################################# [100%]
   
[root@stone ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base docker-ce-stable epel extras kubernetes updates zabbix zabbix-non-supported
Cleaning up list of fastest mirrors

安装 Zabbix Server,Agent:

[root@stone ~]# yum install zabbix-server-mysql zabbix-agent

安装 Zabbix Fronted:

[root@stone ~]# yum install centos-release-scl

修改文件 /etc/yum.repos.d/zabbix.repo,启用 [zabbix-frontend] 仓库,将其 enabled=0 修改为 enabled=1

[root@stone ~]# vi /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=1

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1

然后安装 Zabbix Fronted 包:

[root@stone ~]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl

创建 Zabbix Database

连接到 MySQL 创建 Zabbix 数据库:

[root@stone ~]# mysql

[(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected, 2 warnings (0.01 sec)

[(none)]> create user zabbix@localhost identified with mysql_native_password by 'MyNewPass4#';
Query OK, 0 rows affected (0.01 sec)

[(none)]> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected, 1 warning (0.00 sec)

[(none)]> set global log_bin_trust_function_creators = 1;
Query OK, 0 rows affected (0.00 sec)

执行脚本,初始化数据库:

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

完成后禁用参数 log_bin_trust_function_creators

[(none)]> set global log_bin_trust_function_creators = 0;
Query OK, 0 rows affected (0.01 sec)

配置 Zabbix Server

修改文件 /etc/zabbix/zabbix_server.conf 中的 DBPassword

[root@stone ~]# sed -i "s/# DBPassword=/DBPassword=MyNewPass4#/g" /etc/zabbix/zabbix_server.conf
[root@stone ~]# egrep -v "(^#|^$)" /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBName=zabbix
DBUser=zabbix
DBPassword=MyNewPass4#
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1

修改文件 /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf 的时区配置:

[root@stone ~]# sed -i "s|; php_value\[date.timezone\] = Europe/Riga|php_value\[date.timezone\] = Asia/Shanghai|g" /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
[root@stone ~]# cat /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
[zabbix]
user = apache
group = apache

listen = /var/opt/rh/rh-php72/run/php-fpm/zabbix.sock
listen.acl_users = apache
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 200

php_value[session.save_handler] = files
php_value[session.save_path]    = /var/opt/rh/rh-php72/lib/php/session/

php_value[max_execution_time] = 300
php_value[memory_limit] = 128M
php_value[post_max_size] = 16M
php_value[upload_max_filesize] = 2M
php_value[max_input_time] = 300
php_value[max_input_vars] = 10000
php_value[date.timezone] = Asia/Shanghai

启动 Zabbix Server

[root@stone ~]# systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
[root@stone ~]# systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

安装 Zabbix Web

浏览器访问地址:http://192.168.44.135/zabbix,进入安装欢迎页面,点击open in new window "Next step"。

image-20230720134508790

进入 "Check of pre-requisites" 页面,确认所有的要求都 "OK",点击 "Next step"。

image-20230720134432705

进入 "Configure DB connection" 页面,输入数据库用户密码,点击 "Next step"。

image-20230720134652446

进入 "Zabbix server details" 页面,在 "Name" 处可以填入一个名称,会出现在菜单栏和页面标题,点击 "Next step"。

image-20230720143256063

进入 "Pre-installation summary" 页面,确认信息无误后,点击 "Next step"。

image-20230720143359868

安装完成,点击 "Finish"。

image-20230720143501132

进入登录界面,输入默认账户 Admin,密码 zabbix,点击 "Sign in"。

image-20230720150522314

在 "Global view" 页面,可以看到整个系统的运行情况。

image-20230720152050373

上次编辑于:
贡献者: stonebox,stone