1{ pkgs, lib, ... }:
2{
3 name = "graylog";
4 meta.maintainers = [ ];
5
6 nodes.machine =
7 { pkgs, ... }:
8 {
9 virtualisation.memorySize = 4096;
10 virtualisation.diskSize = 1024 * 6;
11
12 services.mongodb.enable = true;
13 services.elasticsearch.enable = true;
14 services.elasticsearch.extraConf = ''
15 network.publish_host: 127.0.0.1
16 network.bind_host: 127.0.0.1
17 '';
18
19 services.graylog = {
20 enable = true;
21 passwordSecret = "YGhZ59wXMrYOojx5xdgEpBpDw2N6FbhM4lTtaJ1KPxxmKrUvSlDbtWArwAWMQ5LKx1ojHEVrQrBMVRdXbRyZLqffoUzHfssc";
22 elasticsearchHosts = [ "http://localhost:9200" ];
23
24 # `echo -n "nixos" | shasum -a 256`
25 rootPasswordSha2 = "6ed332bcfa615381511d4d5ba44a293bb476f368f7e9e304f0dff50230d1a85b";
26 };
27
28 environment.systemPackages = [ pkgs.jq ];
29
30 systemd.services.graylog.path = [ pkgs.netcat ];
31 systemd.services.graylog.preStart = ''
32 until nc -z localhost 9200; do
33 sleep 2
34 done
35 '';
36 };
37
38 testScript =
39 let
40 payloads.login = pkgs.writeText "login.json" (
41 builtins.toJSON {
42 host = "127.0.0.1:9000";
43 username = "admin";
44 password = "nixos";
45 }
46 );
47
48 payloads.input = pkgs.writeText "input.json" (
49 builtins.toJSON {
50 title = "Demo";
51 global = false;
52 type = "org.graylog2.inputs.gelf.udp.GELFUDPInput";
53 node = "@node@";
54 configuration = {
55 bind_address = "0.0.0.0";
56 decompress_size_limit = 8388608;
57 number_worker_threads = 1;
58 override_source = null;
59 port = 12201;
60 recv_buffer_size = 262144;
61 };
62 }
63 );
64
65 payloads.gelf_message = pkgs.writeText "gelf.json" (
66 builtins.toJSON {
67 host = "example.org";
68 short_message = "A short message";
69 full_message = "A long message";
70 version = "1.1";
71 level = 5;
72 facility = "Test";
73 }
74 );
75 in
76 ''
77 machine.start()
78 machine.wait_for_unit("graylog.service")
79
80 machine.wait_until_succeeds(
81 "journalctl -o cat -u graylog.service | grep 'Started REST API at <127.0.0.1:9000>'"
82 )
83
84 machine.wait_for_open_port(9000)
85 machine.succeed("curl -sSfL http://127.0.0.1:9000/")
86
87 machine.wait_until_succeeds(
88 "journalctl -o cat -u graylog.service | grep 'Graylog server up and running'"
89 )
90
91 session = machine.succeed(
92 "curl -X POST "
93 + "-sSfL http://127.0.0.1:9000/api/system/sessions "
94 + "-d $(cat ${payloads.login}) "
95 + "-H 'Content-Type: application/json' "
96 + "-H 'Accept: application/json' "
97 + "-H 'x-requested-by: cli' "
98 + "| jq .session_id | xargs echo"
99 ).rstrip()
100
101 machine.succeed(
102 "curl -X POST "
103 + f"-sSfL http://127.0.0.1:9000/api/system/inputs -u {session}:session "
104 + '-d $(cat ${payloads.input} | sed -e "s,@node@,$(cat /var/lib/graylog/server/node-id),") '
105 + "-H 'Accept: application/json' "
106 + "-H 'Content-Type: application/json' "
107 + "-H 'x-requested-by: cli' "
108 )
109
110 machine.wait_until_succeeds(
111 "journalctl -o cat -u graylog.service | grep -E 'Input \[GELF UDP/Demo/[[:alnum:]]{24}\] is now RUNNING'"
112 )
113
114 machine.wait_until_succeeds(
115 "test \"$(curl -sSfL 'http://127.0.0.1:9000/api/cluster/inputstates' "
116 + f"-u {session}:session "
117 + "-H 'Accept: application/json' "
118 + "-H 'Content-Type: application/json' "
119 + "-H 'x-requested-by: cli'"
120 + "| jq 'to_entries[]|.value|.[0]|.state' | xargs echo"
121 + ')" = "RUNNING"'
122 )
123
124 machine.succeed(
125 "echo -n $(cat ${payloads.gelf_message}) | nc -w10 -u 127.0.0.1 12201"
126 )
127
128 machine.succeed(
129 'test "$(curl -X GET '
130 + "-sSfL 'http://127.0.0.1:9000/api/search/universal/relative?query=*' "
131 + f"-u {session}:session "
132 + "-H 'Accept: application/json' "
133 + "-H 'Content-Type: application/json' "
134 + "-H 'x-requested-by: cli'"
135 + ' | jq \'.total_results\' | xargs echo)" = "1"'
136 )
137 '';
138}