> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Nightingale and ClickHouse

> Run scheduled SQL threshold alerts against ClickHouse from Nightingale.

Community maintained

[Nightingale](https://github.com/ccfos/nightingale) is a self-hosted alerting engine. Its ClickHouse data source runs scheduled SQL queries so you can alert on thresholds in data you already store in ClickHouse.

## Prerequisites

* [Nightingale v9.3.0](https://github.com/ccfos/nightingale/releases/tag/v9.3.0), with permission to add a data source and create an alert rule in a business group.
* A ClickHouse service reachable from the Nightingale server and, if used, the edge alert engine that runs the rule.
* A ClickHouse user with `SELECT` on the table the rule reads. For the example, you also need a separate account that can create the table and insert sample data.

| Nightingale | ClickHouse Cloud | Self-managed ClickHouse |
| - | - | - |
| v9.3.0 (Linux ARM64) | Verified on `26.6.1.2455` using **HTTP** over TLS on port `8443`, with server certificate verification enabled. | Configuration reference. Historical test versions and TLS settings are unavailable. |

Native TLS on port `9440` is included as a configuration reference.

## Authentication and privileges

Use a dedicated database user with a password over TLS and grant `SELECT` on the table or view used by the rule. For the example, [prepare the sample data](#prepare-the-sample-data) first, then run these statements as a ClickHouse administrator. Replace the example password before running them.

```sql theme={null}
CREATE USER nightingale_reader IDENTIFIED WITH sha256_password BY 'replace-with-a-strong-password';
GRANT SELECT ON n9e_demo.requests TO nightingale_reader;
```

Use `nightingale_reader` in the data source. Run the example's `CREATE` and `INSERT` statements with a separate privileged account.

## Connect Nightingale to ClickHouse

In Nightingale, open **Integrations** → **Data sources** → **Add** and select **ClickHouse**. Enter a name and provide the ClickHouse **User** and **Password** for the read-only account. In **Nodes**, enter one `host:port` without a URL scheme or path. In **Associated alerting engine cluster**, select the cluster that runs the rule. Use `default` for a single-engine installation with the default configuration.

> [tab] ClickHouse Cloud
>
> Open your service in the Cloud console and select **Connect**. Copy the service hostname and use the port for your chosen protocol. Turn on **Secure connection (SSL/TLS)** and leave **Skip SSL verify** off.
>
> | Protocol in Nightingale | Port |
> | - | - |
> | **Native** | `9440` |
> | **HTTP** | `8443` (HTTPS) |
>
> If the service restricts incoming connections, allow the Nightingale server or alert engine's egress IP. Use database credentials for the service. [Cloud connection details](https://clickhouse.com/docs/cloud/guides/sql-console/gather-connection-details.md) · [IP filters](https://clickhouse.com/docs/cloud/security/setting-ip-filters.md)

> [tab] Self-managed ClickHouse
>
> Match the protocol, port, and TLS setting to the server listener. For encrypted connections, turn on **Secure connection (SSL/TLS)** and leave **Skip SSL verify** off. Use the hostname on the certificate. If your server uses a private CA, add its CA certificate to the trust store of each Nightingale process that connects to ClickHouse.
>
> | Protocol in Nightingale | Typical port with TLS | Default port without TLS |
> | - | - | - |
> | **Native** | `9440` | `9000` |
> | **HTTP** | `8443` (HTTPS) | `8123` |
>
> Use the actual listener port if it differs. [Network ports](https://clickhouse.com/docs/guides/sre/network-ports.md) · [Configure TLS](https://clickhouse.com/docs/guides/sre/tls/configuring-tls.md)

Click **Save & test** to run the `SHOW DATABASES` connection check. Then use the rule editor's **Preview** to verify the account can read the target table and return the expected columns.

## Alert on recent HTTP errors

### Prepare the sample data

Create a table and add a baseline. The twelve old errors fall outside the rule's one-minute window. The recent `200` response is inside the window but does not count as an error.

```sql theme={null}
CREATE DATABASE n9e_demo;

CREATE TABLE n9e_demo.requests
(
    event_time DateTime,
    service LowCardinality(String),
    status UInt16
)
ENGINE = MergeTree
ORDER BY (service, event_time);

INSERT INTO n9e_demo.requests
SELECT now() - INTERVAL 2 MINUTE, 'checkout', 503 FROM numbers(12);

INSERT INTO n9e_demo.requests VALUES (now(), 'checkout', 200);
```

Create the [read-only user](#authentication-and-privileges) and [save its data source](#connect-nightingale-to-clickhouse) before creating the rule.

### Build and preview the rule

In **Alerts & Notifications** → **Alert rules**, add a rule in your business group. Choose **ClickHouse** as the data source type and select the data source you saved. Paste this SQL as query `A`.

```sql theme={null}
SELECT
    'checkout' AS service,
    countIf(status >= 500) AS error_count
FROM n9e_demo.requests AS requests
WHERE requests.service = 'checkout'
  AND event_time >= now() - INTERVAL 1 MINUTE
  AND event_time <= now()
```

In the query's auxiliary settings, set **Value field** to `error_count` and **Label field** to `service`. Click **Preview** and check that query `A` returns `0` with the label `service=checkout`.

Set the trigger expression to `$A.error_count >= 10` and choose a severity. Set the evaluation interval to 15 seconds, use zero trigger and recovery delay, and keep the default recovery behavior. The interval controls when Nightingale runs the query. The `WHERE` clause sets the one-minute lookback window.

### Verify firing and recovery

Enable the rule and wait for an evaluation record showing `0`.

Insert twelve recent errors using the privileged account.

```sql theme={null}
INSERT INTO n9e_demo.requests
SELECT now(), 'checkout', 503 FROM numbers(12);
```

On the next evaluation, the query should return `12` and the rule should create a current event with `service=checkout`. Stop inserting errors and wait for the window to pass. The same query returns `0`, so the rule should recover on the next evaluation and record the recovery in historical events. The aggregate returns a row with the same label even when the window is empty. A notification channel is optional for inspecting these events.

In the Cloud test, Nightingale verified the data source when saving it. The scheduled rule evaluated the 25-row fixture from `0` to `12` and back to `0`. The firing and recovery records shared the same rule, `service=checkout` label, and event hash.

Disable or delete the demo rule when finished. See [Nightingale's SQL-backed rules guide](https://n9e.io/docs/alerting/sql-rules) for rule settings.

## Keep scheduled queries bounded

* Filter on a sorting or partition key where possible. In the example, the `WHERE` clause restricts both `service` and `event_time` to match the table's `ORDER BY` key.
* Aggregate to one row per label set you want to evaluate. Return the numeric values and label columns the rule uses.
* Measure query time with the same account and a representative time window before choosing an evaluation interval. For costly queries that need frequent evaluation, point the rule at a pre-aggregated table.

## Known limits

* In the open-source edition, the ClickHouse data source is available only in the SQL alert-rule editor.
* Nightingale uses the first entry in **Nodes**. Point it at a service endpoint if your deployment needs failover.
* The data source form supports password authentication only.
* Use a finite scalar number for the value field. A complex value such as an array, tuple, or map cannot be compared directly. Aggregate it or extract a number in SQL. Values are converted to `Float64`, so very large integers can lose precision.
* The default result limit is 500 rows. Nightingale first counts the rows in a subquery and rejects results above the configured cap. The Cloud check returned 500 points for a 500-row query and rejected a 501-row query. Use the SQL predicates to bound how much data ClickHouse reads.

## Troubleshooting

* **Connection or TLS test fails.** Verify the protocol and listener port, the network path from Nightingale, and any Cloud IP filters. For certificate errors, verify the hostname and trust chain. Keep certificate verification enabled.
* **Connection test succeeds but the query fails.** Check the table grant, use a fully qualified name such as `n9e_demo.requests`, and verify the SQL with the same read-only account.
* **Preview succeeds but the rule has no evaluation records.** Check that the rule is enabled and the data source is associated with a running alerting engine cluster.
* **The rule does not fire or recover as expected.** Inspect the rule's evaluation records for the query value and label. Check the time window, the exact alias in **Value field**, and the expression reference. A grouped query can drop a label when the group is absent. If that should keep the alert firing, select the **Recovery configuration** option that requires data and a trigger condition that is no longer met.
