Elasticsearchのセットアップ
Zabbixは、リレーショナルデータベースの代替として、Elasticsearch に履歴データを保存できます。
Elasticsearchのサポートは現在実験的なものです。
このガイドでは、Elasticsearchのサポート対象バージョンのセットアップについて説明します。 別のバージョンを使用している場合、一部の機能が想定どおりに動作しないことがあります。
Elasticsearchは、次の値のタイプを保存できます。
| アイテムの値のタイプ | データベーステーブル | Elasticsearch type |
|---|---|---|
| Numeric (unsigned) | history_uint | uint |
| Numeric (float) | history | dbl |
| Character | history_str | str |
| Log | history_log | log |
| Text | history_text | text |
| Binary | history_bin | not supported by Zabbix |
| JSON | history_json | json |
ElasticsearchはJSON配列を受け付けません。 JSON値は、単一のオブジェクト、またはオブジェクトの集合のいずれかである必要があります。
重要な注意事項
- Elasticsearch には libcurl が必要です。詳細は 要件 を参照してください。
- housekeeper は Elasticsearch からデータを削除しません。 データの保持期間を制御するには、Index Lifecycle Management を参照してください。
- Zabbix は Elasticsearch 内でトレンドを計算または保存しません。 古いデータを保持するには、history storage period を延長することを検討してください。
- Elasticsearch を使用する場合、データベースから値を取得する範囲クエリは、データ保存期間のタイムスタンプによって制限されます。
- Elasticsearch は Zabbix プロキシではサポートされていません。
Elasticsearch がまだインストールされていない場合は、続行する前に 公式インストールガイド を参照してください。
Elasticsearchの設定
履歴データをElasticsearchに保存するには、以下の手順が必要です。
- 保存したい値の型ごとにインデックスを作成します。これはElasticsearchがデータを保存する場所であり、リレーショナルデータベースのテーブルに相当します。
- 各インデックスにマッピングを定義します。これはデータの構造を定義し、テーブルスキーマに相当します。
- 保存前に値を処理するためのインジェストパイプラインを設定します(JSON値や日付ベースのインデックスには必須です)。
Elasticsearchは、値の型ごとに単一のインデックスにデータを保存することも、複数の日付ベースのインデックスに分散して保存することもできます。 両方のアプローチについて以下で説明します。
1つのインデックスに履歴を保存する
この方法では、指定した値のタイプのすべての履歴データが1つのインデックス(例: uintやtext)に書き込まれます。
数値(整数)の値のタイプのインデックスを作成するには、次のリクエスト(URLの/uint付き)をElasticsearchインスタンスに送信します。
curl -X PUT \
http://localhost:9200/uint \
-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": { "type": "long" }
}
}
}'
Elasticsearchはインデックスが作成されたことを確認する応答を返します。
{"acknowledged": true, "shards_acknowledged": true, "index": "uint"}
Elasticsearchに保存したい追加の値のタイプごとに、同様のリクエストを送信する必要があります。
すべての値のタイプのマッピングはZabbixソースリポジトリで利用できます。
たとえば、テキスト値のタイプのインデックスを作成するには:
curl -X PUT \
http://localhost: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"
}
}
}
}'
JSON値タイプ
他の値タイプとは異なり、JSON値は保存前に追加の処理が必要です。
以下のインデックスは、パース済み値と生値のために個別のフィールドを使用するため、各値をJSONとしてパースし、正しいフィールドに保存するためのインジェストパイプラインが必要です。
JSON値タイプのインデックスを作成するには、Elasticsearchインスタンスに次のリクエスト(URLに/jsonを指定)を送信します。
curl -X PUT \
http://localhost:9200/json \
-H 'content-type:application/json' \
-d '{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"dynamic": false,
"properties": {
"itemid": { "type": "long" },
"clock": { "type": "date", "format": "epoch_second" },
"ns": { "type": "long" },
"value_parsed": { "type": "flattened" },
"value_raw": { "type": "keyword", "ignore_above": 1000000 }
}
}
}'
次に、インジェストパイプラインを作成します:
curl -X PUT \
http://localhost:9200/_ingest/pipeline/json \
-H 'content-type:application/json' \
-d '{
"processors": [
{
"json": {
"field": "value",
"target_field": "value_parsed",
"ignore_failure": true
}
},
{
"set": {
"if": "ctx.value_parsed == null",
"field": "value_raw",
"value": "{{{ value }}}"
}
}
],
"on_failure": [
{
"set": {
"field": "value_raw",
"value": "{{{ value }}}"
}
}
]
}'
Elasticsearchは、インジェストパイプラインが作成されたことを確認する応答を返します:
{"acknowledged": true}
日付ベースのインデックスに履歴を保存する
すべての履歴データを単一のインデックス(例: uint)に書き込む代わりに、Elasticsearch はこのデータを複数の日付ベースのインデックス(例: uint-2026-01-01、uint-2026-01-02)に分散できます。
これにより、時間の経過に伴うデータ量と保持期間をより簡単に管理できます。
これを有効にするには、次の操作が必要です。
- 保存する各値タイプごとに index template を作成します。これにより、Elasticsearch が新しい日付ベースのインデックスを自動作成する際に適用するマッピングが指定されます。
- 各値タイプごとに ingest pipeline を作成します。これは、受信した各値を処理し、正しい日付ベースのインデックスへ振り分けます。
インデックステンプレート
textインデックスのテンプレートを作成するには、以下の詳細でリクエストを送信します。
- ElasticsearchインスタンスのURLに
_template/text_templateを使用します。 - インデックス名に一致させるため、
"index_patterns"フィールドに"text*"を使用します。 text値タイプのマッピングを使用します(Zabbixソースリポジトリのマッピングを参照)。
curl -X PUT \
http://localhost: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"
}
}
}
}'
jsonインデックスのテンプレート:
curl -X PUT \
http://localhost:9200/_template/json_template \
-H 'content-type:application/json' \
-d '{
"index_patterns": [ "json*" ],
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"dynamic": false,
"properties": {
"itemid": { "type": "long" },
"clock": { "type": "date", "format": "epoch_second" },
"ns": { "type": "long" },
"value_parsed": { "type": "flattened" },
"value_raw": { "type": "keyword", "ignore_above": 1000000 }
}
}
}'
インジェストパイプライン
textインデックスのインジェストパイプラインを作成するには:
- ElasticsearchインスタンスのURLで
_ingest/pipeline/text-pipelineを使用します。 - 各値をそのタイムスタンプに基づいて正しい日付ベースのインデックスにルーティングするために、
date_index_nameプロセッサを含めます。
curl -X PUT \
http://localhost:9200/_ingest/pipeline/text-pipeline \
-H 'content-type:application/json' \
-d '{
"description": "daily text index naming",
"processors": [
{
"date_index_name": {
"field": "clock",
"date_formats": ["UNIX"],
"index_name_prefix": "text-",
"date_rounding": "d"
}
}
]
}'
jsonインデックスの場合、パイプラインは値を正しいインデックスにルーティングする前にJSON値を解析する必要もあります:
curl -X PUT \
http://localhost:9200/_ingest/pipeline/json-pipeline \
-H 'content-type:application/json' \
-d '{
"description": "daily json index naming"
"processors": [
{
"json": {
"field": "value",
"target_field": "value_parsed",
"ignore_failure": true
}
},
{
"script": {
"source": "if (ctx.value_parsed == null || !(ctx.value_parsed instanceof Map)) { ctx.value_raw = ctx.value; ctx.remove(\"value_parsed\"); }"
}
},
{
"date_index_name": {
"field": "clock",
"date_formats": [ "UNIX" ],
"index_name_prefix": "json-",
"date_rounding": "d"
}
}
]
}'
Zabbixサーバーの設定
Zabbixサーバーの設定ファイル (zabbix_server.conf) で、HistoryProvider パラメーターを設定します。
たとえば、Character、Log、Text、JSON 型の値を Elasticsearch に保存し、Numeric 値はデータベースに保持するには、次のようにします。
HistoryProvider=elasticsearch;value_types="str,log,text,json",url=http://localhost:9200
日付ベースのインデックスを使用している場合は、パラメーターに date_index=1 を追加します。
HistoryProvider=elasticsearch;value_types="str,log,text,json",url=http://localhost:9200,date_index=1
変更後、Zabbixサーバーを再起動します。
systemctl restart zabbix-server
Zabbix Webインターフェースの設定
Zabbix Webインターフェースの設定ファイル(zabbix.conf.php)で、$HISTORY_PROVIDERS 変数をサーバー設定に合わせて次のように設定します。
$HISTORY_PROVIDERS[] = [
'types' => ['str','log','text','json'],
'provider' => 'elasticsearch',
'url' => 'http://localhost:9200'
];
トラブルシューティング
以下の手順で、Elasticsearch の設定に関する問題のトラブルシューティングに役立つ場合があります。
-
Elasticsearch または Zabbix サーバーのログでエラーを確認します。
-
遅いクエリを特定するには、Zabbix サーバーの設定パラメータ
HistoryProviderでlog_slow_queriesオプションを使用します。 -
Elasticsearch が Zabbix サーバーおよび Zabbix Webインターフェースからのアクセスを許可していることを確認します。
-
auto_create_indexが有効になっていることを確認します。
curl -X GET \
"http://localhost:9200/_cluster/settings?include_defaults=true&filter_path=**.auto_create_index"
# {"defaults": {"action": {"auto_create_index": "false"} } }
有効にするには、次のリクエストを送信します。
curl -X PUT \
http://localhost:9200/_cluster/settings \
-H 'content-type:application/json' \
-d '{
"persistent": {
"action.auto_create_index": "true"
}
}'
# {"acknowledged": true, "persistent": {"action": {"auto_create_index": "true"} }, "transient": {} }
GETリクエストをそれぞれの URL に送信して、マッピング、テンプレート、および ingest pipeline が正しいことを確認します。
curl -X GET http://localhost:9200/json
curl -X GET http://localhost:9200/_template/json*
curl -X GET http://localhost:9200/_ingest/pipeline/json*
受信したレスポンスは、Elasticsearch API documentation の期待されるレスポンスと比較できます。
-
失敗状態になっている shards がないか確認します。Elasticsearch を再起動すると解決する場合があります。
-
Elasticsearch にクエリを実行して、Zabbix によって収集されたデータが保存されているか確認します。たとえば、次のようにします。
curl 'http://localhost:9200/json/_search' \
-H 'Content-Type: application/json' \
-d '{
"query": {
"term": {
"itemid": 42269
}
}
}'
- Elasticsearch の設定をリセットして最初からやり直す必要がある場合は、すべてのインデックス、テンプレート、および ingest pipeline を削除できます。
curl -X DELETE "http://localhost:9200/_all"
curl -X DELETE "http://localhost:9200/_template/*"
curl -X DELETE "http://localhost:9200/_ingest/pipeline/*"