2022 Zabbix中国峰会
2022 Zabbix中国峰会

5 Elasticsearch 配置

目前Zabbix对Elasticsearch的支持,仍在试验阶段!

Zabbix支持通过Elasticsearch,而不使用数据库来存储历史数据。用户可以在兼容的数据库和Elasticsearch之间来选择历史数据的存储位置。本章中所描述的设置过程适用于Elasticsearch 7.X版本。如果使用了较早或更高的版本,某些功能则可能会无法正常工作。

如果所有历史数据都存储在Elasticsearch上,将不会计算趋势,也不会存储在数据库中。如果没有计算和存储趋势,历史数据保留时长可能需要延长。

配置

为保证涉及的所有元素之间能正常通信,请确保正确配置了Zabbix server及其前端配置文件的参数。

Zabbix server和前端

在Zabbix server初始的配置文件中,需要更新如下参数:

### Option: HistoryStorageURL
       # History storage HTTP[S] URL.
       #
       # Mandatory: no
       # Default:
       # HistoryStorageURL= 
       ### Option: HistoryStorageTypes
       # Comma separated list of value types to be sent to the history storage.
       #
       # Mandatory: no
       # Default:
       # HistoryStorageTypes=uint,dbl,str,log,text

例如使用以下示例参数值,来设置Zabbix server的配置文件:

HistoryStorageURL=http://test.elasticsearch.lan:9200
       HistoryStorageTypes=str,log,text

使用此配置,Zabbix server会将数值类型的历史数据存储在相应的数据库中,将文本历史数据存储在Elasticsearch中。

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

uint,dbl,str,log,text

支持的监控项类型说明:

Item value type Database table Elasticsearch type
Numeric (unsigned) history_uint uint
Numeric (float) history dbl
Character history_str str
Log history_log log
Text history_text text

Zabbix前端配置文件(conf/zabbix.conf.php)中,需要更新如下参数:

// Elasticsearch url (can be string if same url is used for all types).
       $HISTORY['url']   = [
             'uint' => 'http://localhost:9200',
             'text' => 'http://localhost:9200'
       ];
       // Value types stored in Elasticsearch.
       $HISTORY['types'] = ['uint', 'text'];

例如使用以下示例参数值,来设置Zabbix前端的配置文件:

$HISTORY['url']   = 'http://test.elasticsearch.lan:9200';
       $HISTORY['types'] = ['str', 'text', 'log'];

使用此配置,文本字符日志类型的历史数据将存储到Elasticsearch中。

您还需要将conf/zabbix.conf.php文件的中$HISTORY配置为全局参数,以确保一切正常(了解如何配置,请参考conf/zabbix.conf.php.example):

// Zabbix GUI configuration file.
       global $DB, $HISTORY;
Elasticsearch安装及创建映射

使其正常运行的最后两个步骤是安装Elasticsearch和创建映射。

安装Elasticsearch,请参考 Elasticsearch安装指南

映射是Elasticsearch中的一种数据结构(类似于数据库中的表)。此处提供了所有历史数据类型的映射:database/elasticsearch/elasticsearch.map

<note warning>创建映射是强制性的。如果未按照要求创建映射,则某些功能将无法正常使用。 :::

创建text类型的映射,可以发送如下请求到Elasticsearch:

curl -X PUT \
        http://your-elasticsearch.here: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"
                }
             }
          }
       }'

对于创建字符日志类型的历史数据映射并有相应类型的修改,也需要执行类似的请求。

要使用Elasticsearch,请参考 安装条件页面 以获取更多信息。

Housekeeper 不会删除任何Elasticsearch中的数据。

在多个基于日期的索引中存储历史数据

本节将介绍使用pipelines和ingest节点所需的其他配置步骤。

首先,您必须为索引创建一个模板。

创建uint模板的请求示例如下:

curl -X PUT \
        http://your-elasticsearch.here:9200/_template/uint_template \
        -H 'content-type:application/json' \
        -d '{
          "index_patterns": [
             "uint*"
          ],
          "settings": {
             "index": {
                "number_of_replicas": 1,
                "number_of_shards": 5
             }
          },
          "mappings": {
             "properties": {
                "itemid": {
                   "type": "long"
                },
                "clock": {
                   "format": "epoch_second",
                   "type": "date"
                },
                "value": {
                   "type": "long"
                }
             }
          }
       }'

若要创建其他模板,用户需要修改URL(最后一部分是模板名称),更改"​index_patterns"字段以匹配索引名称并设置有效的映射,这些映射可以在​database/​elasticsearch/​elasticsearch.map中获取。​

例如:我们可以使用以下命令,来为文本索引创建一个模板:

curl -X PUT \
        http://your-elasticsearch.here:9200/_template/text_template \
        -H 'content-type:application/json' \
        -d '{
          "index_patterns": [
             "text*"
          ],
          "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"
                }
             }
          }
       }'

这是允许Elasticsearch为自动创建的索引设置有效映射所必需的。然后需要创建pipeline定义。Pipeline是在将数据放入索引之前,对数据的某种预处理。可以使用以下命令,为uint索引创建pipeline:

curl -X PUT \
        http://your-elasticsearch.here:9200/_ingest/pipeline/uint-pipeline \
        -H 'content-type:application/json' \
        -d '{
          "description": "daily uint index naming",
          "processors": [
             {
                "date_index_name": {
                   "field": "clock",
                   "date_formats": [
                      "UNIX"
                   ],
                   "index_name_prefix": "uint-",
                   "date_rounding": "d"
                }
             }
          ]
       }'

用户可以修改rounding参数(“date_rounding”)来设置特定的索引循环周期。要创建其他的pipeline,用户需要修改URL(最后一部分是pipeline名称)并更改"index_name_prefix"字段以匹配索引名称。

请参考 Elasticsearch文档

另外,还需要在Zabbix server配置中,加入新参数来启用在多个基于日期的索引中存储历史数据。

### Option: HistoryStorageDateIndex
       # Enable preprocessing of history values in history storage to store values in different indices based on date.
       # 0 - disable
       # 1 - enable
       #
       # Mandatory: no
       # Default:
       # HistoryStorageDateIndex=0

故障诊断

以下步骤可帮助您解决Elasticsearch的配置问题:

  1. 检查映射是否正确(向URL发送GET请求获取索引信息,例如:http://localhost:9200/uint)。
  2. 检查分片状态是否处于失败状态 (可以通过重启Elasticsearch来解决)。
  3. 检查Elasticsearch配置文件,配置文件应允许从Zabbix前端主机和Zabbix server主机进行访问。
  4. 检查Elasticsearch日志。

如果您仍然遇到安装问题,请创建一个bug报告,其中需包含该列表中的所有信息(映射、错误日志、配置、版本等信息)。