Zabbix Elasticsearch schema scripts

Scripts that create Elasticsearch index templates and ingest pipelines for
Zabbix history.

Usage:
./<history table>_schema.sh -s <Elasticsearch URL> [-d <db>] [-S <shards>] [-r <replicas>] [-n]

Parameters:
-s|--server: Elasticsearch URL (default: http://localhost:9200), when the port
              is omitted 9200 is used
-d|--db: Prefix of index, template and pipeline names (default: none), must be
         the same as the server option db, for example zabbix_ creates
         zabbix_dbl, zabbix_dbl-pipeline and so on
-S|--shards: Number of primary shards (default: 1)
-r|--replicas: Number of replicas (default: 1), use 0 on a single node cluster
-n|--dry-run: Print the requests instead of sending them, curl is not required
-h|--help: Display help message

Example:
./history_all.sh -s http://localhost:9200 -d zabbix_

The same prefix must be set as the db option in the server configuration
(zabbix_server.conf):

HistoryProvider=elasticsearch;value_types="uint,dbl,str,log,text,json",url=http://localhost:9200,db=zabbix_

and in the frontend configuration (zabbix.conf.php):

$HISTORY_PROVIDERS[] = [
	'types' => ['uint', 'dbl', 'str', 'log', 'text', 'json'],
	'provider' => 'elasticsearch',
	'url' => 'http://localhost:9200',
	'db' => 'zabbix_'
];

It is recommended to use a unique prefix (for example zabbix_), so that the
names do not clash with other indexes, index templates and pipelines in
Elasticsearch.

Scripts Overview
elasticsearch.sh - helper script, not to be used directly

Schema Creation Scripts:
- history_all.sh - invokes all history schema creation scripts

- history_schema.sh - dbl index template and dbl-pipeline
- history_log_schema.sh - log index template and log-pipeline
- history_str_schema.sh - str index template and str-pipeline
- history_text_schema.sh - text index template and text-pipeline
- history_uint_schema.sh - uint index template and uint-pipeline
- history_json_schema.sh - json index template, json-pipeline and json-parse

These scripts create an index template for each value type, it applies to the
<index> index and to the daily <index>-YYYY-MM-DD indexes. Elasticsearch
creates the indexes from the templates when the server stores the first value,
also when an index was deleted. The same setup works with and without the
server option date_index.

elasticsearch.map contains the same requests, it is generated by:

./history_all.sh -d zabbix_ -n > elasticsearch.map

The json index parses the values with the json-parse ingest pipeline, which
is set as the default pipeline of the index (with date_index=1 json-pipeline
calls it before routing the values). JSON objects are stored in the
value_parsed field, other values (scalars, arrays and invalid JSON) in the
value_raw field.

Date based indexes:

When the server is configured with date_index=1, for example:

HistoryProvider=elasticsearch;value_types="uint,dbl,str,log,text,json",url=http://localhost:9200,db=zabbix_,date_index=1

the server sends the values through the <index>-pipeline ingest pipeline, which
stores them in daily <index>-YYYY-MM-DD indexes (by the value clock, in UTC).

Elasticsearch history is not removed by the Zabbix housekeeper. With daily
indexes old history can be removed by deleting the indexes, for example:

curl -X DELETE "<Elasticsearch URL>/dbl-2025-01-01"

The schema creation scripts never drop or change existing indexes, the index
templates and ingest pipelines are created or replaced. Changes in templates
apply only to indexes created after them. To recreate an index from the
template, together with all its data, delete it:

curl -X DELETE "<Elasticsearch URL>/dbl"
