Zabbix性能优化:利用Elasticsearch缓解监控数据存储的性能问题

在使用 Zabbix 的过程中,随着监控对象数量的不断增加,历史数据量也会逐日递增。这往往会引发一系列问题,比如前端页面出现卡顿现象、采集队列发生堆积,以及 MySQL 或 POSTGRESQL 数据库的读写压力急剧增大等。这时候可以将监控数据从 MySQL 或 POSTGRESQL 数据库中分离出来,利用 Elasticsearch 来存储历史监控数据,优化Zabbix性能。

在使用 Zabbix 的过程中,随着监控对象数量的不断增加,历史数据量也会逐日递增。这往往会引发一系列问题,比如前端页面出现卡顿现象、采集队列发生堆积,以及 MySQL 或 POSTGRESQL 数据库的读写压力急剧增大等。一般来说,这种情况可以通过优化 Zabbix 参数和数据库性能进行处理。此外,还有一种有效的解决方法,那就是将监控数据从 MySQL 或 POSTGRESQL 数据库中分离出来,利用 Elasticsearch 来存储历史监控数据。下面将详细介绍如何使用 Elasticsearch 存储 Zabbix 的历史监控数据。

1.Elasticsearch部署

Eticsearch 版本:7.10.0,端口:9200

1.1.更新系统软件包到最新版本

[root@localhost ~]# dnf -y update

1.2.安装Java 11运行时环境

[root@localhost ~]# dnf install java-11-openjdk-devel

1.3.下载Elasticsearch的RPM安装包

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.0-x86_64.rpm

1.4.安装Elasticsearch

[root@localhost ~]# rpm -ivh elasticsearch-7.10.0-x86_64.rpm 

1.5.启动Elasticsearch

[root@localhost ~]# systemctl daemon-reload

[root@localhost ~]# systemctl start elasticsearch.service
[root@localhost ~]# systemctl status elasticsearch.service
[root@localhost ~]# systemctl enable elasticsearch

NJVDfq5B67f62c94ed297.png

1.6.验证Elasticsearch是否正常运行

curl -X GET "http://localhost:9200"

UGv4vlbW67f62ca7881aa.png

1.7.配置Elasticsearch

配置 Java 内存限制

# 根据服务器配置总堆空间的初始/最大大小

[root@localhost ~]# vi /etc/elasticsearch/jvm.options
.....
-Xms1g
-Xmx1g

根据需要修改配置参数,例如修改监听地址、集群名称等

[root@localhost ~]# vi /etc/elasticsearch/elasticsearch.yml

cluster.name: 集群名称
node.name: 节点名称
network.host: 绑定 IP 地址
http.port: HTTP 端口号
path.data: 数据存放路径
path.logs: 日志存放路径

重启Elasticsearch

[root@localhost ~]# systemctl start elasticsearch.service

2.zabbix 安装

2.1.组件安装

yum源安装

[root@localhost ~]# rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rocky/8/x86_64/zabbix-release-latest-7.0.el8.noarch.rpm

[root@localhost ~]# dnf clean all

切换php版本

[root@localhost ~]# dnf module switch-to php:8.2

安装zabbix server、web、agent

[root@localhost ~]# dnf install zabbix-server-pgsql zabbix-web-pgsql zabbix-nginx-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent2

安装postgresql

[root@localhost ~]#dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

[root@localhost ~]# dnf -qy module disable postgresql
[root@localhost ~]# dnf install -y postgresql15-server
# 初始化数据库
[root@localhost ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb
[root@localhost ~]# systemctl enable --now postgresql-15
[root@localhost ~]# systemctl status postgresql-15 验证数据库状态
[root@localhost ~]# sudo -u postgres createuser --pwprompt zabbix
[root@localhost ~]# sudo -u postgres createdb -O zabbix zabbix
[root@localhost ~]# zcat /usr/share/zabbix-sql-scripts/postgresql/server.sql.gz | sudo -u zabbix psql zabbix
# mysql -uroot -p
password
mysql> set global log_bin_trust_function_creators = 0;
mysql> quit;

2.2.Elasticsearch数据库配置

Elasticsearch索引说明

Elasticsearch支持以下几种监控项类型

uint,dbl,str,log,text

原来Zabbix 的数据是存储在 MySQL/POSTGRESQL 中的,按照数据格式的不同分别存储的五个表中:history、history_uint、history_str、history_log、history_text。这五个表和 es 中相对应的索引关系如下。

数据类型zabbix数据库表es索引类型
数字(无符号)history_uint uint
数字(浮点型)history 
dbl
字符history_str
str
日志history_log
log
文本history_text text


Elasticsearch索引创建

¡添加数字(无符号)类型的索引

curl -X PUT \

 http://localhost:9200/uint \
 -H 'content-type:application/json' \
 -d '{
   "settings": {
      "index": {
         "number_of_replicas": 1,
         "number_of_shards": 5
      }
   },
   "mappings": {
      "properties": {
         "itemid": {
            "type": "long"
         },
         "clock": {
            "format": "epoch_second",
            "type": "date"
         },
         "value": {
            "type": "long"
         }
      }
   }
}'

0Hz8oYI167f62dc3e099e.png

¡添加数字(浮点型)类型的索引

curl -X PUT \

 http://localhost:9200/dbl \
 -H 'content-type:application/json' \
 -d '{
   "settings": {
      "index": {
         "number_of_replicas": 1,
         "number_of_shards": 5
      }
   },
   "mappings": {
      "properties": {
         "itemid": {
            "type": "long"
         },
         "clock": {
            "format": "epoch_second",
            "type": "date"
         },
         "value": {
            "type": "double"
         }
      }
   }
}'

cZaDY65y67f62dda36a38.png

¡添加字符类型的索引

curl -X PUT \

 http://localhost:9200/str \
 -H 'content-type:application/json' \
 -d '{
   "settings": {
      "index": {
         "number_of_replicas": 1,
         "number_of_shards": 5
      }
   },
   "mappings": {
      "properties": {
         "itemid": {
            "type": "long"
         },
         "clock": {
            "format": "epoch_second",
            "type": "date"
         },
         "value": {
            "fields": {
               "analyzed": {
                  "index": true,
                  "type": "text",
                  "analyzer": "standard"
               }
            },
            "index": false,
            "type": "text"
         }
      }
   }
}'

1qtJPZzv67f62deebecaf.png

¡添加日志类型的索引

2QShsx7e67f62dfd4df13.png

¡添加文本类型的索引

curl -X PUT \

 http://localhost:9200/text \
 -H 'content-type:application/json' \
 -d '{
   "settings": {
      "index": {
         "number_of_replicas": 1,
         "number_of_shards": 5
      }
   },
   "mappings": {
      "properties": {
         "itemid": {
            "type": "long"
         },
         "clock": {
            "format": "epoch_second",
            "type": "date"
         },
         "value": {
            "fields": {
               "analyzed": {
                  "index": true,
                  "type": "text",
                  "analyzer": "standard"
               }
            },
            "index": false,
            "type": "text"
         }
      }
   }
}'
u8MtK95l67f62e0fdb768.png

2.3.修改 Zabbix 的配置文件

修改zabbix server配置

[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf

DBPassword=password #这里为mysql数据库地址
HistoryStorageURL=127.0.0.1:9200 #这里为elasticsearch地址
HistoryStorageTypes=uint,dbl,str,log,text

修改zabbix web配置

[root@localhost ~]# vim /etc/zabbix/web/zabbix.conf.php

// Zabbix GUI configuration file.
       global $DB, $HISTORY;
#修改两个“$HISTORY”的值。
// Elasticsearch url (can be string if same url is used for all types).
$HISTORY['url'] = 'http://127.0.0.1:9200';
// Value types stored in Elasticsearch.
$HISTORY['types'] = ['uint', 'text', 'log', 'str', 'dbl'];

重启服务

[root@localhost ~]# # systemctl restart zabbix-server zabbix-agent2 nginx php-fpm

[root@localhost ~]# # systemctl enable zabbix-server zabbix-agent2 nginx php-fpm

检查所有索引和文档数量

curl -X GET "http://127.0.0.1:9200/_cat/indices?v"

b63CqGjk67f62e53c264a.png

登录web查看监控数据

gs8ApqpQ67f62e5dea59b.png

此后历史监控数据都会录入elasticsearch,而不会存放在postgresql数据库了,这样可以有效解决数据库的读写性能瓶颈问题。


0 条评论

请先 登录 后评论
乐维君
乐维君

430 篇文章

作家榜 »

  1. 乐维君 430 文章
  2. YOHOHO 14 文章
  3. 机灵小和尚 13 文章
  4. 细雨闲花 12 文章
  5. 我是一只小菜鸡 12 文章
  6. 。。。 9 文章
  7. 御前侍卫张五哥 9 文章
  8. 小黄人 8 文章