6 Elasticsearch 配置

Elasticsearch支持尚处于实验阶段!

Zabbix支持使用Elasticsearch替代数据库存储历史数据。用户可在兼容数据库与Elasticsearch之间选择历史数据的存储位置。本节描述的配置流程适用于Elasticsearch 7.X版本。若使用更早或更新的Elasticsearch版本,部分功能可能无法按预期工作。

若所有历史数据均存储于Elasticsearch中, 趋势数据将不会被计算或存储于数据库。由于趋势数据未被计算存储,可能需要延长历史数据的存储周期。

配置

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

Zabbix server 和前端

Zabbix server 配置文件草案(需更新参数):

### 选项:HistoryStorageURL
       # 历史存储的HTTP[S] URL地址
       #
       # 必填:否
       # 默认值:
       # HistoryStorageURL=
       ### 选项:HistoryStorageTypes
       # 要发送到历史存储的值类型逗号分隔列表
       #
       # 必填:否
       # 默认值:
       # 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

支持的监控项类型说明:

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

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

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

填充Zabbix前端配置文件的示例参数值:

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

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

还需在conf/zabbix.conf.php中将$HISTORY设为全局变量以确保正常运行(具体方法参见conf/zabbix.conf.php.example):

// Zabbix图形界面配置文件
       global $DB, $HISTORY;
安装Elasticsearch并创建映射

完成配置的最后两个步骤是安装Elasticsearch本身和创建映射过程。

要安装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的更多信息,请参阅serverproxy

管家服务不会从Elasticsearch中删除任何数据。

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

本节描述使用管道和接收节点所需的额外步骤。

首先,必须为indices创建模板。

以下示例展示创建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": "daily uint index naming",
          "processors": [
             {
                "date_index_name": {
                   "field": "clock",
                   "date_formats": [
                      "UNIX"
                   ],
                   "index_name_prefix": "uint-",
                   "date_rounding": "d"
                }
             }
          ]
       }'

用户可修改舍入参数("date_rounding")来设置特定的索引轮转周期。要创建其他管道,用户应更改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日志。

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