这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.
2022 Zabbix中国峰会
2022 Zabbix中国峰会

6 Elasticsearch 配置

对 Elasticsearch 的支持仍在试验阶段!

Zabbix支持Elasticsearch代替数据库存储历史数据。用户可以在兼容的数据库和 Elasticsearch 之间选择存储历史数据的位置。本章节描述的设置适用于 Elasticsearch version 7.X。如果使用早期或更高版本的 Elasticsearch,某些功能可能无法使用。

如果所有历史数据都存储在 Elasticsearch 中,则不会计算趋势,也不会将其存储在数据库中。由于没有计算和存储任何趋势,则可能需要延长历史记录存储期。

配置

为确保所涉及的所有元素之间的正确通信,请确保服务器配置文件和前端配置文件参数正确。

Zabbix 服务端 和 前端

Zabbix 服务器配置文件模板,其中包含要更新的参数:

### 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 服务端配置文件示例的参数值

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

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

Elasticsearch 支持以下 item 类型:

uint,dbl,str,log,text

支持的 item 类型说明:

Item 类型 数据库表 Elasticsearch 类型
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种存储 Text, CharacterLog 的历史值. 还需要在conf/zabbix.conf.php中使用全局变量 $HISTORY,以确保一切正常 (有关如何执行此操作,请参阅conf/zabbix.conf.php.example ):

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

安装Elasticsearchit及创建映射是整个安装过程的最后两步了。

如何安装 Elasticsearch,请参阅Elasticsearch installation guide.

映射是 Elasticsearch 中的一种数据结构(类似于数据库中的表)。所有历史数据类型的映射都可用: database/elasticsearch/elasticsearch.map.

创建映射是必需的。如未根据指令创建映射,某些功能将不可用。

要为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"
                }
             }
          }
       }'

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

要使用Elasticsearch,请参阅Requirement page 以获取更多信息.

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

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

本节介绍使用管道和引入节点所需的其他步骤。 首先,必须为索引创建模板。 以下示例为创建 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"
                }
             }
          ]
       }'

用户可以更改参数("date_rounding")以设置特定的索引轮换周期。要创建其他pipelines,用户应更改 URL(最后一部分是pipelines的名称)并更改"index_name_prefix"字段以匹配索引名称 另见 Elasticsearch documentation. 此外,在Zabbix服务器新的配置参数中,还应启用将历史数据存储在多个基于日期的索引中:

### 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. 检查shards状态是否正常 (不正常时重启Elasticsearch可能解决问题)
  3. 检查Elasticsearch配置文件,配置文件应允许从Zabbix前端及Zabbix server主机访问。
  4. 检查Elasticsearch日志

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