ClickHouse setup

Zabbix can store history data in ClickHouse as an alternative to a relational database.

ClickHouse can also be used as a telemetry data source for application performance monitoring (APM); for details, see Administration > Data source.

This guide covers the setup for the supported versions of ClickHouse. If you're using a different version, some functionality may not work as intended.

ClickHouse can store the following value types:

Item value type Database table ClickHouse 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

ClickHouse does not accept JSON arrays. A JSON value must be either a single object, or a set of objects. Additionally, ClickHouse handles JSON keys with NULL the same as missing keys.

Important notes

Configuring ClickHouse

You need to create and configure a Zabbix database and user, and import the database schema.

This guide gives instructions for Docker or package installations of ClickHouse.

Docker

1. Create and configure the Zabbix database and user when running the ClickHouse container:

sudo docker run -d \
  --name clickhouse \
  -e CLICKHOUSE_DB=zabbix \
  -e CLICKHOUSE_USER=zabbix \
  -e CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 \
  -e CLICKHOUSE_PASSWORD=<password> \
  -p 8123:8123/tcp \
  -p 9000:9000/tcp \
  --cap-add=SYS_NICE \
  --cap-add=NET_ADMIN \
  --cap-add=IPC_LOCK \
  --ulimit nofile=262144:262144 \
  -v clickhouse_data:/var/lib/clickhouse \
  clickhouse/clickhouse-server:26.4

2. Confirm that ClickHouse is working and you can connect to it:

sudo docker exec -it clickhouse \
  clickhouse-client \
  --query "SELECT version()"

# 26.4.4.38

3. Import the database schema using the history_all.sh script from your Zabbix directory:

/usr/share/zabbix/sql-scripts/clickhouse/history_all.sh \
  --user zabbix \
  --password <password> \
  --db zabbix \
  --server http://localhost:8123

The data storage period (Time-To-Live, or TTL) for ClickHouse is, by default, 31 days. To change it, use the --ttl option when importing the database schema (e.g., --ttl 604800 for 7 days), or configure it later.

4. Verify that tables were created:

sudo docker exec -it clickhouse \
  clickhouse-client \
  --user zabbix \
  --password <password> \
  --query "SHOW TABLES FROM zabbix"

# history
# history_json
# history_log
# history_str
# history_text
# history_uint

Packages

1. Start ClickHouse:

sudo service clickhouse-server start

2. Create and configure the Zabbix database and user:

clickhouse-client --user default

localhost :) CREATE DATABASE IF NOT EXISTS zabbix
localhost :) CREATE USER IF NOT EXISTS zabbix IDENTIFIED WITH sha256_password BY '<password>'
localhost :) GRANT CREATE, ALTER, DROP, INSERT, SELECT, UPDATE, OPTIMIZE ON zabbix.* TO zabbix
localhost :) quit

3. Import the database schema using the history_all.sh script from your Zabbix directory:

/usr/share/zabbix/sql-scripts/clickhouse/history_all.sh \
  --user zabbix \
  --password <password> \
  --db zabbix \
  --server http://localhost:8123

The data storage period (Time-To-Live, or TTL) for ClickHouse is, by default, 31 days. To change it, use the --ttl option when importing the database schema (e.g., --ttl 604800 for 7 days), or configure it later.

4. Verify that tables were created:

clickhouse-client \
  --user zabbix \
  --password <password> \
  --query "SHOW TABLES FROM zabbix"

# history
# history_json
# history_log
# history_str
# history_text
# history_uint

Configuring Zabbix server

In your Zabbix server configuration file (zabbix_server.conf), set the HistoryProvider parameter.

For example, to store all supported types of values in ClickHouse:

HistoryProvider=clickhouse;value_types="uint,dbl,str,log,text,json",url=http://localhost:8123,db=zabbix,username=zabbix,password=<password>

After making changes, restart Zabbix server:

systemctl restart zabbix-server

Configuring Zabbix frontend

In your Zabbix frontend configuration file (zabbix.conf.php), set the $HISTORY_PROVIDERS variable to match the server configuration:

$HISTORY_PROVIDERS[] = [
  'types' => ['uint','dbl','str','log','text','json'],
  'provider' => 'clickhouse',
  'url' => 'http://localhost:8123',
  'db' => 'zabbix',
  'username' => 'zabbix',
  'password' => '<password>'
];

Additional configuration

The steps below are optional. You do not need them for a basic setup.

Configuring ClickHouse data storage period

The data storage period (Time-To-Live, or TTL) for ClickHouse is, by default, 31 days. To change it, run the commands below.

The examples below use Docker. If you installed ClickHouse using packages, run the queries in the ClickHouse client directly. If you have a ClickHouse cluster, run the queries for the _local tables and add the ON CLUSTER clause in step 1 (e.g., ALTER TABLE zabbix.history_json_local ON CLUSTER cluster_2S_2R MODIFY TTL clock_ns + toIntervalSecond(3600)).

1. Alter the table (replace history_json and 3600 with the values you need):

sudo docker exec -it clickhouse \
  clickhouse-client \
  --user zabbix \
  --password <password> \
  --query "ALTER TABLE zabbix.history_json MODIFY TTL clock_ns + toIntervalSecond(3600)"

2. Verify that the data storage period changed:

sudo docker exec -it clickhouse \
  clickhouse-client \
  --user zabbix \
  --password <password> \
  --query "SHOW CREATE TABLE zabbix.history_json"

# CREATE TABLE zabbix.history_json
# (
#     `itemid` UInt64,
#     `clock_ns` DateTime64(9),
#     `value` JSON,
#     `value_str` String
# )
# ENGINE = MergeTree
# PARTITION BY toDate(clock_ns)
# PRIMARY KEY (itemid, clock_ns)
# ORDER BY (itemid, clock_ns)
# TTL clock_ns + toIntervalSecond(3600)
# SETTINGS index_granularity = 8192

3. Restart Zabbix server to refresh the data storage period in Administration > Housekeeping:

systemctl restart zabbix-server

Configuring ClickHouse clusters

This section provides configuration steps for ClickHouse clusters with replication and sharding. For information on how replication and sharding work in ClickHouse, see Replicated* table engines and Distributed table engine in ClickHouse documentation.

The steps below use Docker and the ClickHouse cluster_2S_2R from ClickHouse documentation (running ClickHouse 26.4.4.38 with ClickHouse Keeper). For your own cluster, make sure that its definition in the ClickHouse server configuration (the remote_servers section) sets internal_replication to true for each shard, as in the cluster_2S_2R example. Otherwise, Distributed tables write data to every replica themselves, while the replicas also copy the same data to each other, which adds load and can result in duplicated history data.

Before you start, it is recommended to put a load balancer in front of your ClickHouse cluster for Zabbix. Zabbix server and frontend configuration supports only a single ClickHouse URL, so it can only point to one node. If that node goes down, a load balancer will redirect Zabbix to a healthy node instead, through that same URL.

1. Create the Zabbix database:

sudo docker exec -it clickhouse-01 \
  clickhouse-client \
  --user default \
  --query "CREATE DATABASE IF NOT EXISTS zabbix ON CLUSTER cluster_2S_2R"

2. Create and configure the Zabbix user (replace <password>):

sudo docker exec -it clickhouse-01 \
  clickhouse-client \
  --user default \
  --query "CREATE USER IF NOT EXISTS zabbix ON CLUSTER cluster_2S_2R IDENTIFIED WITH sha256_password BY '<password>'"

sudo docker exec -it clickhouse-01 \
  clickhouse-client \
  --user default \
  --query "GRANT ON CLUSTER cluster_2S_2R
    CREATE, ALTER, DROP, INSERT, SELECT, UPDATE, OPTIMIZE ON zabbix.*,
    TABLE ENGINE ON Distributed,
    CLUSTER ON *.*
    TO zabbix"

3. Import the database schema using the history_all.sh script from your Zabbix directory (replace <password>). The script creates <table>_local tables that store the data, and Distributed tables (e.g., history) that store no data but pass Zabbix queries to the _local tables on all shards. Both types of tables are created on every node:

/usr/share/zabbix/sql-scripts/clickhouse/history_all.sh \
  --user zabbix \
  --password <password> \
  --db zabbix \
  --server http://localhost:8123 \
  --engine "ReplicatedMergeTree()" \
  --cluster cluster_2S_2R

The data storage period (Time-To-Live, or TTL) for ClickHouse is, by default, 31 days. To change it, use the --ttl option when importing the database schema (e.g., --ttl 604800 for 7 days), or configure it later. For other options (e.g., setting explicit replica paths), see the script README.

The ON CLUSTER clause in steps 1–3 reaches only nodes that are part of the cluster when you run the command. If you add nodes later, create the Zabbix database, user, and tables on them with the same definitions as on existing nodes, including table UUIDs (you cannot run history_all.sh again, as it fails if the tables already exist).

4. Verify that tables were created (you can run this command on any node):

sudo docker exec -it clickhouse-01 \
  clickhouse-client \
  --user default \
  --query "SHOW TABLES FROM zabbix"

# history
# history_json
# history_json_local
# history_local
# history_log
# history_log_local
# history_str
# history_str_local
# history_text
# history_text_local
# history_uint
# history_uint_local

5. Configure Zabbix server.

6. Configure Zabbix frontend.

7. Verify that Zabbix data is spread across shards. Replicas of the same shard should have the same row count (clickhouse-01 and clickhouse-03 for shard 1, clickhouse-02 and clickhouse-04 for shard 2). Since Zabbix server continuously inserts new data, the counts may briefly differ while replication catches up:

sudo docker exec -it clickhouse-01 \
  clickhouse-client \
  --user default \
  --query "SELECT hostName() AS node, count(*) FROM clusterAllReplicas('cluster_2S_2R', zabbix.history_local) GROUP BY node ORDER BY node"

# clickhouse-01  64
# clickhouse-02  61
# clickhouse-03  64
# clickhouse-04  61

8. Check that replication is in sync (active_replicas should match total_replicas):

sudo docker exec -it clickhouse-01 \
  clickhouse-client \
  --user default \
  --query "SELECT database, table, is_leader, total_replicas, active_replicas FROM system.replicas WHERE database = 'zabbix'"

# zabbix  history_json_local  1 2 2
# zabbix  history_local       1 2 2
# zabbix  history_log_local   1 2 2
# zabbix  history_str_local   1 2 2
# zabbix  history_text_local  1 2 2
# zabbix  history_uint_local  1 2 2

Troubleshooting

The following steps may help you troubleshoot problems with your ClickHouse setup:

  1. Check ClickHouse or Zabbix server logs for errors.

  2. To identify slow queries, set the LogSlowQueries Zabbix server configuration parameter. While waiting for a ClickHouse response, Zabbix server logs waiting for ClickHouse response <N>sec each time the specified time passes, or every 10 seconds if LogSlowQueries is set to 0.

  3. Verify that ClickHouse allows access from Zabbix server and Zabbix frontend.

  4. Query ClickHouse to see if the data collected by Zabbix is stored, for example:

sudo docker exec -it clickhouse \
  clickhouse-client \
  --user zabbix \
  --password <password> \
  --query "SELECT * FROM zabbix.history_uint WHERE itemid = 42269"