7 Elasticsearch配置

Elasticsearch 支持为实验性功能!

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

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

配置

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

Zabbix 服务器和前端

需要更新参数的 Zabbix 服务器配置文件草案:

### Option: HistoryStorageURL
# 历史数据存储 HTTP(S) URL。
#
# Mandatory: no
# Default:
# HistoryStorageURL= 
### Option: HistoryStorageTypes
# 要发送到历史数据存储的值类型列表,以逗号分隔。
#
# Mandatory: no
# Default:
# HistoryStorageTypes=uint,dbl,str,log,text

用于填充 Zabbix 服务器配置文件的参数值示例:

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

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

Elasticsearch 支持以下监控项类型:

uint,dbl,str,log,text

支持的监控项类型说明:

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

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

// Elasticsearch URL(如果所有类型使用相同的 URL,也可以使用字符串)。
$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'];

此配置会强制将 Text、Character 和 Log 历史值存储在 Elasticsearch 中。

此外,还必须在 conf/zabbix.conf.php 中将 $HISTORY 声明为全局变量,以确保一切正常运行(有关操作方法,请参阅 conf/zabbix.conf.php.example):

// Zabbix GUI 配置文件。
global $DB, $HISTORY;
安装Elasticsearch并创建映射

使系统正常运行的最终两个步骤是安装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"
         }
      }
   }
}'

对于Character和Log历史值映射的创建,需要执行类似的请求,并相应调整类型。

有关使用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。