Zabbix学习资源由浅入深 关于我们 联系我们 加入我们
5

基于TimeScaleDB(PG时序数据库)离线部署Zabbix5.4

TimeScaleDB

1.1、关于ZABBIX5.4

ZABBIX5.4系列已发布一段时间,其许多的新特性对运维有很大帮助。如定期生成的PDF可作为运维巡检的日报、周报或月报;基于标签式管理监控对象,包括:模板、主机、主机原型、触发器、指标和事件;在线Git模板存储库,可通过Zabbix API实时保持最新模板等等。更多特性可查看:官网中文介绍

1.2、关于TimeScaleDB

ZABBIX从4.2开始支持TimeScaleDB,它是一种基于PostGreSQL的数据库解决方案,可自动将数据划分为基于时间的块,以支持更快的大规模性能。Zabbix通过hypertable分区表实现监控数据自动清理和压缩,为大规模监控场景而生。值得注意的是,目前TimeScaleDB不支持ZABBIX PROXY作为后端库。

二、前置条件

2.1、环境介绍

环境版本备注
CentOS7.9x86_64
Zabbix5.4.4官网最新发行版,与DB同机部署
PHP7.4.247.2.5+,暂不支持PHP8.0
Cmake3.21.23.4+,编译TimeScale
PostgreSQL13.4官网最新发行版
TimeScaleDB2.1.1Zabbix5.4支持1.5~2.1

2.2、系统优化

1)将系统升级到最新

# yum -y update
# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

2)关闭SeLinux/FireWall

# systemctl disable --now firewalld
# setenforce 0
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# reboot

3)核实操作有效性

# sestatus
SELinux status:                 disabled
# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

三、部署后端库

3.1、后端库依赖包安装

在编译过程中,如提示缺少相关包的错误,一般情况下只需安装其对应的devel包即可解决问题,而CentOS的Yum Base仓会满足大部分场景,因此无需额外进行离线编译。

# yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel  python-devel gcc-c++ openssl-devel cmake gcc readline-devel lbzip2 systemd-devel tcl-devl net-tools  lrzsz

3.2、部署PostGreSQL

1)访问链接 ,下载PostGreSQL13.4,将pg安装包上传至包管理目录。


# mkdir -p /data/packages
# cd /data/packages
# rz -be
# md5sum postgresql-13.4.tar.bz2
7bda65a37c46b8b2c1933d9d1cd677f2  postgresql-13.4.tar.bz2

2)创建组和用户

# groupadd postgres
# useradd -g postgres postgres
# echo '' | passwd --stdin postgres

3)编译安装

# mkdir -p /data/postgres/{data,logs}
# tar xf postgresql-13.4.tar.bz2
# cd postgresql-13.4
# ./configure  --prefix=/data/postgres --enable-rpath --enable-nls --enable-dtrace --with-python --with-libxml --with-libxslt --with-openssl
# make -j8 && make install

4)设置系统环境变量

# cat >> /etc/profile << EOF
### postgres ###
export PGHOME=/data/postgres
export PGDATA=/data/postgres/data/
export LD_LIBRARY_PATH=\$PGHOME/lib
export PATH=\$PATH:\$HOME/bin:\$PGHOME/bin
EOF
# source /etc/profile
# pg_config |tail -n 1
VERSION = PostgreSQL 13.4

5)初始化数据库

# chown -R postgres.postgres /data/postgres/
# su postgres
$ initdb -D /data/postgres/data/
$ pg_ctl -D /data/postgres/data/ -l /data/postgres/logs/pgsql.log start
$ psql
psql (13.4)
Type "help" for help.

6)测试数据库

postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table test (id integer,name text);
CREATE TABLE
test=# insert into test values (1,'chasii');
INSERT 0 1
test=# insert into test values (1,'蔡斯');
INSERT 0 1
test=# select * from test;
 id |  name  
----+--------
  1 | chasii
  1 | 蔡斯
(2 rows)

test=# \q

7)实现远程访问

$ cp /data/postgres/data/pg_hba.conf{,.bak}
$ cp /data/postgres/data/postgresql.conf{,.bak}
echo  "host    all         all         0.0.0.0/0             password" >> /data/postgres/data/pg_hba.conf
$ sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '0.0.0.0'/g" /data/postgres/data/postgresql.conf
$ pg_ctl -D /data/postgres/data/ -l /data/postgres/logs/pgsql.log restart
$ netstat -lntp|grep 5432
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      30919/postgres

3.3、安装TimeScaleDB

1)安装CMake最新版,TimeScaleDB编译安装依赖CMake v3.11+,而CentOS7.9初始版本为v2.8。我们访问官网下载最新版CMake源码包进行编译安装。

# cd /data/packages
# rz -be
# tar xf cmake-3.21.2.tar.gz
# cd cmake-3.21.2
# ./configure --prefix=/usr/local/cmake
# gmake && make install
# mv /usr/bin/cmake /usr/bin/cmake_2.8
# ln -s /usr/local/cmake/bin/cmake /usr/bin/
# cmake --version|head -n 1
cmake version 3.21.2

2)安装TimeScaleDB,这里选择2.1.1,GigHub地址

# cd /data/packages
# rz -be
# tar xf timescaledb-2.1.1.tar.gz
# cd timescaledb-2.1.1
# ./bootstrap -DREGRESS_CHECKS=OFF
# cd ./build && make -j8 && make install

3)添加TimeScaleDB共享库到pgsql的配置文件

# su - postgres
$ sed -i "s/#shared_preload.*/shared_preload_libraries = 'timescaledb'/g" /data/postgres/data/postgresql.conf
$ pg_ctl -D /data/postgres/data/ -l /data/postgres/logs/pgsql.log restart

4)为ZABBIX创建PG用户和数据库

$ psql
psql (13.4)
Type "help" for help.

postgres=# create role zabbix with password 'zabbix' login;
CREATE ROLE
postgres=# create database zabbix with template template0 encoding 'UTF8';
CREATE DATABASE
postgres=# grant all on database zabbix to zabbix;
GRANT

5)为Zabbix库增加TimeScale扩展

$ psql -Uzabbix -dzabbix -h127.0.0.1
psql (13.4)
Type "help" for help.

zabbix=> CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
WARNING:  
WELCOME TO
 _____ _                               _     ____________  
|_   _(_)                             | |    |  _  \ ___ \ 
  | |  _ _ __ ___   ___  ___  ___ __ _| | ___| | | | |_/ / 
  | | | |  _ ` _ \ / _ \/ __|/ __/ _` | |/ _ \ | | | ___ \ 
  | | | | | | | | |  __/\__ \ (_| (_| | |  __/ |/ /| |_/ /
  |_| |_|_| |_| |_|\___||___/\___\__,_|_|\___|___/ \____/
               Running version 2.1.1
For more information on TimescaleDB, please visit the following links:

 1. Getting started: https://docs.timescale.com/getting-started
 2. API reference documentation: https://docs.timescale.com/api
 3. How TimescaleDB is designed: https://docs.timescale.com/introduction/architecture

Note: TimescaleDB collects anonymous reports to better understand and assist our users.
For more information and how to disable, please see our docs https://docs.timescaledb.com/using-timescaledb/telemetry.

CREATE EXTENSION

6)查询Zabbix库扩展

zabbix=> select oid,extname,extowner,extversion from pg_extension where extname = 'timescaledb';
  oid  |   extname   | extowner | extversion 
-------+-------------+----------+------------
 25185 | timescaledb |    24639 | 2.1.1
(1 row)

7)创建Zabbix数据库、索引、分区等配置

# cd /data/packages/
# tar xf zabbix-5.4.4.tar.gz
# cd zabbix-5.4.4/database/postgresql/
# su postgres
$ psql -Uzabbix -dzabbix -h127.0.0.1 -f schema.sql
$ psql -Uzabbix -dzabbix -h127.0.0.1 -f images.sql
$ psql -Uzabbix -dzabbix -h127.0.0.1 -f data.sql
$ psql -Uzabbix -dzabbix -h127.0.0.1 -f timescaledb.sql
NOTICE:  PostgreSQL version 13.4 is valid
NOTICE:  TimescaleDB extension is detected
NOTICE:  TimescaleDB version 2.1.1 is valid
NOTICE:  TimescaleDB is configured successfully
DO

从以上命令执行结果可看出timescaledb.sql语句已成功配置且当前版本有效。通过解读该sql语句可看出其主要作用于:history、history_uint、history_log、history_text、history_log、history_str、trends、trends_unit,这7张大表,通过hypertable表将数据按设定的(chunk_time_interval)时间条件自动分块,同时对7天前的监控数据进行压缩。

四、部署前端组件

4.1、依赖包安装

yum -y install patch gd-devel libxml2-devel libmcrypt-devel openldap-devel mhash-devel curl-devel libpng-devel libjpeg-devel freetype-devel sqlite-devel oniguruma-devel zlib-devel gettext-devel

4.2、安装HTTPD

这里可以选择HTTPD,也可以选择NGINX,后者性能更优越。

# yum -y install httpd
# systemctl enable httpd && systemctl start httpd
# curl -Is localhost| head -n 1 # 测试服务是否正常访问,403属于正常。
HTTP/1.1 403 Forbidden

4.3、编译安装PHP

1)Zabbix从5.0开始,最低要求PHP7.2,而CentOS默认Base库只有PHP5.4。因此需访问官网下载合适版本。

# cd /data/packages/
# rz -be
# tar xf php-7.4.24.tar.bz2
# cd php-7.4.24
# ./configure \
--prefix=/usr/local/php \
--enable-fpm \
--enable-gd \
--enable-ftp \
--with-freetype \
--with-jpeg \
--with-iconv \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap   \
--without-pear \
--with-gettext \
--with-pdo-pgsql=/data/postgres
# make -j8 && make install
# ln -s /usr/local/php/bin/php /usr/sbin/
# php -v | head -n 1 #验证版本
PHP 7.4.24 (cli) (built: Sep 24 2021 16:53:46) ( NTS )
# php -m | grep pgsql # 查询PHP是否有pgsql模块

2)配置PHP相关参数

# cp php.ini-production /usr/local/php/lib/php.ini
# sed -i -e 's/post_max_size = 8M/post_max_size = 16M/g' -e 's/max_execution_time = 30/max_execution_time = 300/g' -e  's/max_input_time = 60/max_input_time = 300/g' -e 's#;date.timezone.*#date.timezone = Asia/ShangHai#g' /usr/local/php/lib/php.ini
# cd /usr/local/php/etc/
# mv php-fpm.conf.default php-fpm.conf
# cd php-fpm.d/
# mv www.conf.default www.conf

3)配置PHP服务自启动

# cp./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm
# chkconfig --add /etc/init.d/php-fpm && chkconfig php-fpm on

五、部署后端组件

5.1、后端组件依赖包安装

# yum -y net-snmp-devel libssh2-devel libevent-devel libcurl-devel sqlite-devel go

5.2、添加ZABBIX用户组及用户

# groupadd zabbix
# useradd -g zabbix -d /usr/local/zabbix -s /bin/false zabbix

5.3、源码编译ZABBIX最新版

# cd /data/packages
# tar xf zabbix-5.4.4.tar.gz
# cd zabbix-5.4.4
# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-postgresql  --with-net-snmp --with-libcurl --with-libxml2  --with-ssh2 --with-libpcre
# make install
# /usr/local/zabbix/sbin/zabbix_server -V |head -n 1
zabbix_server (Zabbix) 5.4.4

5.4、配置ZABBIX SERVER数据库密码

# cd /usr/local/zabbix/etc/
# cp zabbix_server.conf{,.bak}
# vim zabbix_server.conf # 将# DBPassword=改为:DBPassword=zabbix

5.5、配置ZABBIX AGENTD配置

# cd /usr/local/zabbix/etc/
# cp zabbix_agentd.conf{,.bak}
# tee > zabbix_agentd.conf << EOF
PidFile=/tmp/zabbix_agentd.pid
LogType=file
LogFile=/tmp/zabbix_agentd.log
LogFileSize=5
SourceIP=127.0.0.1
Server=127.0.0.1
ListenPort=10050
ServerActive=127.0.0.1
Hostname=Zabbix server
HostMetadataItem=system.uname
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
UnsafeUserParameters=1
EOF

5.6、配置ZABBIX服务自启动

# cp /data/packages/zabbix-5.4.4/misc/init.d/fedora/core5/* /etc/init.d/
# vim /etc/init.d/zabbix_server # 修改成:ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd"
# vim /etc/init.d/zabbix_agentd # 修改成:ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server"
# chmod +x /etc/init.d/zabbix_*
# chkconfig --add zabbix_server
# chkconfig --add zabbix_agentd
# chkconfig zabbix_server on
# chkconfig zabbix_agentd on
# systemctl daemon-reload
# /etc/init.d/zabbix_server restart
Restarting zabbix_server (via systemctl):                  [  OK  ]
# /etc/init.d/zabbix_agentd restart
Restarting zabbix_agentd (via systemctl):                  [  OK  ]

错误提示1:提示pg共享库找不到

-- Unit zabbix_server.service has begun starting up. Sep 07 15:58:52 localhost.localdomain zabbix_server[48874]: Starting Zabbix Server: /usr/local/sbin/zabbix_server: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory

将依赖库软链接到系统默认共享库位置:/ln -fs /data/postgres/lib/libpq.so.5 /usr/lib64/

六、配置前端页面

6.1、配置ZABBIX站点

# mkdir /var/www/html/zabbix/ui
# cp -a . /var/www/html/zabbix/
# chown -R apache.apache /var/www/html/zabbix/
# chmod -R 777 /var/www/html/zabbix/
# sed -i 's#/var/www/html#/var/www/html/zabbix#' /etc/httpd/conf/httpd.conf

6.2、增加ZABBIX站点

# tee > /etc/httpd/conf.d/zabbix.conf << EOF
ServerSignature Off
ServerTokens Prod

    StartServers         2        # 启动时进程数
    MinSpareThreads      25       # 最小空闲线程数
    MaxSpareThreads      75       # 最大空闲线程数
    ThreadLimit          64       # 每个进程可以启动的线程数量上限值
    ThreadsPerChild      25       # 每个进程可以启动的线程数量
    MaxRequestWorkers    400      # 线程数量最大值
    MaxConnectionsPerChild   0    # 最大连接数限制



    SetHandler "proxy:fcgi://127.0.0.1:9000"

AddType text/html .php
DirectoryIndex index.php


    php_value session.save_handler "files"
    php_value session.save_path "/var/lib/php/session"

EOF
# systemctl restart httpd

6.1、访问ZABBIX配置页

1)打开浏览器访问http://<你的主机ip>,如无例外,会进入到配置页。如图一所示。

2)ZABBIX安装程序会自动检测PHP版本、配置参数是否符合要求,因前面我们已经初始化PHP.INI,此步可正常通过。如图二所示。

图片

3)配置DB数据库信息,这里建议将Database TLS encryption配置项加上,避免出现一些安全问题。如图三所示。

图片

4)如果出现:Unable to determine current Zabbix database version:the table "dbversion" was not found,多半是账号无权限。那么可以临时将zabbix账号权限提升为"SUPSERUSER"。ALTER USER zabbix WITH SUPERUSER;。如图四所示。

图片

5)如数据库配置正确,那么进入到ZABBIX SERVER配置页,这里默认就好。NAME配置项是ZABBIX站点的标题。

图片

6)设定ZABBIX时区

图片

7)到了此步,那么恭喜你,系统配置完全通过,点击下一步会尝试写入zabbix.conf.php文件到站点conf文件夹里。

图片

8)写入配置PHP配置文件,如提示失败,通常做法是用户权限或文件夹权限问题。一般做法是将ZABBIX站点权限修改为777,但安全上可能存在风险。


9)登录ZABBIX,默认账号:Admin、默认密码:zabbix

图片

10)成功登入后,"Zabbix server is running"的状态为Yes,系统正常运行。

图片

11)查看监控数据,Agent正常上报。

图片

12)通过Administration->General->Housekeeping可配置是否压缩历史数据,以及压缩时间点(系统限制最短为7天)。

图片

至此,基于TimeScaleDB(PG时序数据库)离线部署Zabbix5.4的整个过程算完成了,欢迎交流。


蔡斯

Zabbix开源社区签约专家

2021-10-09