7 Elasticsearch配置

Elasticsearch 支持为实验性功能!

Zabbix 支持通过 Elasticsearch 来存储历史数据,而不是使用数据库。用户可以在兼容的数据库和 Elasticsearch 之间选择历史数据的存储位置。本节中描述的设置过程适用于 Elasticsearch version 7.X。如果使用了更早或更新的 Elasticsearch version,某些功能可能无法按预期工作。

1. 如果所有历史数据都存储在 Elasticsearch 中,则不会计算和存储趋势数据。由于没有计算和存储趋势数据,可能需要延长历史数据的存储周期。
2. 当使用 Elasticsearch 时,通过时间范围检索数据库值的 queries 将受到数据存储周期时间戳的限制。

配置

为确保所有相关组件之间通信正常,请确保正确配置服务器配置 file 和前端配置 file 参数。

Zabbix server 和前端

Zabbix server 配置 file 参数更新草案:

### 选项:HistoryStorageURL
       # 历史存储 HTTP[S] URL。
       #
       # 必填项:否
       # 默认值:
       # HistoryStorageURL= 
       ### 选项:HistoryStorageTypes
       # 以逗号分隔的值类型列表,这些类型将发送到历史存储。
       #
       # 必填项:否
       # 默认值:
       # HistoryStorageTypes=uint,dbl,str,log,text

用于填充 Zabbix server 配置 file 的示例参数值:

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

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

Elasticsearch 支持以下 监控项 类型:

uint,dbl,str,log,text

支持的 监控项 类型说明:

监控项 值类型 数据库表 Elasticsearch 类型
数值(无符号) history_uint uint
数值(float) history dbl
字符 history_str str
日志 history_log log
文本 history_text text

Zabbix 前端配置 file(conf/zabbix.conf.php)参数更新草案:

// Elasticsearch URL(如果所有类型使用相同的 URL,则可以为 string)。
       $HISTORY['url']   = [
             'uint' => 'http://localhost:9200',
             'text' => 'http://localhost:9200'
       ];
       // 存储在 Elasticsearch 中的值类型。
       $HISTORY['types'] = ['uint', 'text'];

用于填充 Zabbix 前端配置 file 的示例参数值:

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

此配置强制将 TextCharacterLog 类型的历史值存储在 Elasticsearch 中。

此外,还需要在 conf/zabbix.conf.php 中将 $HISTORY 设为全局变量,以确保所有功能正常运行(请参阅 conf/zabbix.conf.php.example 了解具体操作方法):

// Zabbix GUI 配置文件。
       global $DB, $HISTORY;

{HEADER_02795988}

使一切正常工作的最后两步是安装Elasticsearch本身以及创建映射过程。

要安装Elasticsearch,请参阅 Elasticsearch installation guide

映射是Elasticsearch中的一个数据结构(类似于数据库中的表)。所有历史数据类型的映射可在此处找到:database/elasticsearch/elasticsearch.map

创建映射是必须的。如果未按照说明创建映射,某些功能将无法正常工作。

要为 text 类型 create 映射,请向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"
                }
             }
          }
       }'

对于 CharacterLog 历史值映射的创建,也需要执行类似的请求,并对相应的类型进行修正。

有关使用Elasticsearch的更多信息,请参阅 serverproxy

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

将历史数据存储在多个基于日期的 indices

本节描述了使用管道和摄入节点(ingest nodes)时所需的额外步骤。

首先,你必须为 indices create 模板。

以下示例展示了创建 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"
                }
             }
          }
       }'

To create other templates, user should change the URL (last part is the name of template), change "index_patterns" field to match index name and to set valid mapping, which can be taken from database/elasticsearch/elasticsearch.map.

For example, the following command can be used to create a template for text index:

{.java}
       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"
                }
             }
          }
       }'

This is required to allow Elasticsearch to set valid mapping for indices created automatically. Then it is required to create the pipeline definition. Pipeline is some sort of preprocessing of data before putting data in indices. The following command can be used to create pipeline for uint index:

{.java}
       curl -X PUT \
        http://your-elasticsearch.here:9200/_ingest/pipeline/uint-pipeline \
        -H 'content-type:application/json' \
        -d '{
          "description": "每日 uint 索引命名",
          "processors": [
             {
                "date_index_name": {
                   "field": "clock",
                   "date_formats": [
                      "UNIX"
                   ],
                   "index_name_prefix": "uint-",
                   "date_rounding": "d"
                }
             }
          ]
       }'

用户可以更改 rounding 参数("date_rounding")以设置特定的索引轮换周期。要 create 其他管道,用户应更改 URL(最后一部分是管道名称)并将 "index_name_prefix" 字段更改为匹配索引名称。

另请参阅 Elasticsearch documentation

此外,在 Zabbix server 配置中,还应在新的参数中启用基于日期将历史数据存储在多个 indices 中的功能:

### 选项: HistoryStorageDateIndex
       # 启用历史存储中的历史值预处理功能,以便根据日期将值存储在不同的 indices 中。
       # 0 - 禁用
       # 1 - 启用
       #
       # 必填项: 否
       # 默认:
       # HistoryStorageDateIndex=0

故障排除

以下步骤可帮助您排查 Elasticsearch 配置中的问题:

  1. 检查映射是否正确(向所需索引 URL 发送 GET 请求, 例如 http://localhost:9200/uint)。
  2. 检查分片是否未处于失败状态(重启 Elasticsearch 应该可以解决)。
  3. 检查 Elasticsearch 的配置。配置应允许来自 Zabbix 前端 主机 和 Zabbix server 主机 的访问。
  4. 检查 Elasticsearch 日志。
  5. 可以使用 LogSlowQueries 来检查 Elasticsearch 数据库中的慢速 queries。

如果您在安装过程中仍然遇到问题, 请使用此列表中的所有信息(映射、错误日志、配置、版本等)提交一个缺陷报告 create。