···
1
+
import ../make-test-python.nix (
4
+
# Take the original journald message and create a new payload which only
5
+
# contains the relevant fields - these must match the database columns.
6
+
journalVrlRemapTransform = {
8
+
inputs = [ "journald" ];
12
+
m.app = .SYSLOG_IDENTIFIER
14
+
m.severity = to_int(.PRIORITY) ?? 0
15
+
m.level = to_syslog_level(m.severity) ?? ""
16
+
m.message = strip_ansi_escape_codes!(.message)
17
+
m.timestamp = .timestamp
18
+
m.uid = to_int(._UID) ?? 0
19
+
m.pid = to_int(._PID) ?? 0
26
+
name = "vector-journald-clickhouse";
27
+
meta.maintainers = [ pkgs.lib.maintainers.happysalada ];
31
+
{ config, pkgs, ... }:
33
+
virtualisation.diskSize = 5 * 1024;
34
+
virtualisation.memorySize = 4096;
36
+
networking.firewall.allowedTCPPorts = [ 6000 ];
40
+
journaldAccess = true;
50
+
address = "[::]:6000";
54
+
transforms = journalVrlRemapTransform;
58
+
type = "clickhouse";
63
+
endpoint = "http://localhost:8123";
64
+
database = "journald";
66
+
date_time_best_effort = true;
73
+
services.clickhouse = {
79
+
{ config, pkgs, ... }:
83
+
journaldAccess = true;
92
+
transforms = journalVrlRemapTransform;
97
+
inputs = [ "journald_remap" ];
98
+
address = "clickhouse:6000";
108
+
# work around quote/substitution complexity by Nix, Perl, bash and SQL.
109
+
databaseDDL = pkgs.writeText "database.sql" "CREATE DATABASE IF NOT EXISTS journald";
111
+
# https://clickhouse.com/blog/storing-log-data-in-clickhouse-fluent-bit-vector-open-telemetry
112
+
tableDDL = pkgs.writeText "table.sql" ''
113
+
CREATE TABLE IF NOT EXISTS journald.logs (
114
+
timestamp DateTime64(6),
115
+
app LowCardinality(String),
116
+
host LowCardinality(String),
117
+
level LowCardinality(String),
123
+
ENGINE = MergeTree()
124
+
ORDER BY (host, app, timestamp)
125
+
PARTITION BY toYYYYMM(timestamp)
128
+
selectQuery = pkgs.writeText "select.sql" ''
129
+
SELECT COUNT(host) FROM journald.logs
130
+
WHERE message LIKE '%Vector has started%'
134
+
clickhouse.wait_for_unit("clickhouse")
135
+
clickhouse.wait_for_open_port(6000)
136
+
clickhouse.wait_for_open_port(8123)
138
+
clickhouse.succeed(
139
+
"cat ${databaseDDL} | clickhouse-client"
142
+
clickhouse.succeed(
143
+
"cat ${tableDDL} | clickhouse-client"
146
+
for machine in clickhouse, vector:
147
+
machine.wait_for_unit("vector")
148
+
machine.wait_until_succeeds(
149
+
"journalctl -o cat -u vector.service | grep 'Vector has started'"
152
+
clickhouse.wait_until_succeeds(
153
+
"cat ${selectQuery} | clickhouse-client | grep 2"