···
1
+
import ../make-test-python.nix ({ lib, pkgs, ... }:
4
+
name = "vector-nginx-clickhouse";
5
+
meta.maintainers = [ pkgs.lib.maintainers.happysalada ];
8
+
clickhouse = { config, pkgs, ... }: {
9
+
virtualisation.memorySize = 4096;
11
+
# Clickhouse module can't listen on a non-loopback IP.
12
+
networking.firewall.allowedTCPPorts = [ 6000 ];
13
+
services.clickhouse.enable = true;
15
+
# Exercise Vector sink->source for now.
23
+
address = "[::]:6000";
29
+
type = "clickhouse";
30
+
inputs = [ "vector_source" ];
31
+
endpoint = "http://localhost:8123";
32
+
database = "nginxdb";
33
+
table = "access_logs";
34
+
skip_unknown_fields = true;
41
+
nginx = { config, pkgs, ... }: {
44
+
virtualHosts.localhost = {};
54
+
include = [ "/var/log/nginx/access.log" ];
62
+
inputs = [ "nginx_logs" ];
63
+
address = "clickhouse:6000";
69
+
systemd.services.vector.serviceConfig = {
70
+
SupplementaryGroups = [ "nginx" ];
77
+
# work around quote/substitution complexity by Nix, Perl, bash and SQL.
78
+
databaseDDL = pkgs.writeText "database.sql" "CREATE DATABASE IF NOT EXISTS nginxdb";
80
+
tableDDL = pkgs.writeText "table.sql" ''
81
+
CREATE TABLE IF NOT EXISTS nginxdb.access_logs (
84
+
ENGINE = MergeTree()
88
+
# Graciously taken from https://clickhouse.com/docs/en/integrations/vector
89
+
tableView = pkgs.writeText "table-view.sql" ''
90
+
CREATE MATERIALIZED VIEW nginxdb.access_logs_view
96
+
RequestMethod String,
103
+
ENGINE = MergeTree()
104
+
ORDER BY RemoteAddr
107
+
splitByWhitespace(message) as split,
108
+
splitByRegexp('\S \d+ "([^"]*)"', message) as referer
110
+
split[1] AS RemoteAddr,
111
+
split[2] AS Client,
112
+
split[3] AS RemoteUser,
113
+
parseDateTimeBestEffort(replaceOne(trim(LEADING '[' FROM split[4]), ':', ' ')) AS TimeLocal,
114
+
trim(LEADING '"' FROM split[6]) AS RequestMethod,
115
+
split[7] AS Request,
116
+
trim(TRAILING '"' FROM split[8]) AS HttpVersion,
117
+
split[9] AS Status,
118
+
split[10] AS BytesSent,
119
+
trim(BOTH '"' from referer[2]) AS UserAgent
121
+
(SELECT message FROM nginxdb.access_logs)
124
+
selectQuery = pkgs.writeText "select.sql" "SELECT * from nginxdb.access_logs_view";
127
+
clickhouse.wait_for_unit("clickhouse")
128
+
clickhouse.wait_for_open_port(8123)
130
+
clickhouse.wait_until_succeeds(
131
+
"journalctl -o cat -u clickhouse.service | grep 'Started ClickHouse server'"
134
+
clickhouse.wait_for_unit("vector")
135
+
clickhouse.wait_for_open_port(6000)
137
+
clickhouse.succeed(
138
+
"cat ${databaseDDL} | clickhouse-client"
141
+
clickhouse.succeed(
142
+
"cat ${tableDDL} | clickhouse-client"
145
+
clickhouse.succeed(
146
+
"cat ${tableView} | clickhouse-client"
149
+
nginx.wait_for_unit("nginx")
150
+
nginx.wait_for_open_port(80)
151
+
nginx.wait_for_unit("vector")
152
+
nginx.wait_until_succeeds(
153
+
"journalctl -o cat -u vector.service | grep 'Starting file server'"
156
+
nginx.succeed("curl http://localhost/")
157
+
nginx.succeed("curl http://localhost/")
159
+
nginx.wait_for_file("/var/log/nginx/access.log")
160
+
nginx.wait_until_succeeds(
161
+
"journalctl -o cat -u vector.service | grep 'Found new file to watch. file=/var/log/nginx/access.log'"
164
+
clickhouse.wait_until_succeeds(
165
+
"cat ${selectQuery} | clickhouse-client | grep 'curl'"