@@ -1,95 +1,107 @@
1−# Test the ELK stack: Elasticsearch, Logstash and Kibana.
2−
3−import ./make-test.nix ({ pkgs, ...} :
1+{ system ? builtins.currentSystem }:
2+with import ../lib/testing.nix { inherit system; };
3+with pkgs.lib;
44let
55 esUrl = "http://localhost:9200";
6−in {
7− name = "ELK";
8− meta = with pkgs.stdenv.lib.maintainers; {
9− maintainers = [ eelco chaoflow offline basvandijk ];
10− };
116
12− nodes = {
13− one =
14− { config, pkgs, ... }: {
15− # Not giving the machine at least 2060MB results in elasticsearch failing with the following error:
16− #
17− # OpenJDK 64-Bit Server VM warning:
18− # INFO: os::commit_memory(0x0000000085330000, 2060255232, 0)
19− # failed; error='Cannot allocate memory' (errno=12)
20− #
21− # There is insufficient memory for the Java Runtime Environment to continue.
22− # Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
23− #
24− # When setting this to 2500 I got "Kernel panic - not syncing: Out of
25− # memory: compulsory panic_on_oom is enabled" so lets give it even a
26− # bit more room:
27− virtualisation.memorySize = 3000;
7+ mkElkTest = name : elk : makeTest {
8+ inherit name;
9+ meta = with pkgs.stdenv.lib.maintainers; {
10+ maintainers = [ eelco chaoflow offline basvandijk ];
11+ };
12+ nodes = {
13+ one =
14+ { config, pkgs, ... }: {
15+ # Not giving the machine at least 2060MB results in elasticsearch failing with the following error:
16+ #
17+ # OpenJDK 64-Bit Server VM warning:
18+ # INFO: os::commit_memory(0x0000000085330000, 2060255232, 0)
19+ # failed; error='Cannot allocate memory' (errno=12)
20+ #
21+ # There is insufficient memory for the Java Runtime Environment to continue.
22+ # Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
23+ #
24+ # When setting this to 2500 I got "Kernel panic - not syncing: Out of
25+ # memory: compulsory panic_on_oom is enabled" so lets give it even a
26+ # bit more room:
27+ virtualisation.memorySize = 3000;
2828
29− # For querying JSON objects returned from elasticsearch and kibana.
30− environment.systemPackages = [ pkgs.jq ];
29+ # For querying JSON objects returned from elasticsearch and kibana.
30+ environment.systemPackages = [ pkgs.jq ];
3131
32− services = {
33− logstash = {
34− enable = true;
35− package = pkgs.logstash5;
36− inputConfig = ''
37− exec { command => "echo -n flowers" interval => 1 type => "test" }
38− exec { command => "echo -n dragons" interval => 1 type => "test" }
39− '';
40− filterConfig = ''
41− if [message] =~ /dragons/ {
42− drop {}
43− }
44− '';
45− outputConfig = ''
46− file {
47− path => "/tmp/logstash.out"
48− codec => line { format => "%{message}" }
49− }
50− elasticsearch {
51− hosts => [ "${esUrl}" ]
52− }
53− '';
54− };
32+ services = {
33+ logstash = {
34+ enable = true;
35+ package = elk.logstash;
36+ inputConfig = ''
37+ exec { command => "echo -n flowers" interval => 1 type => "test" }
38+ exec { command => "echo -n dragons" interval => 1 type => "test" }
39+ '';
40+ filterConfig = ''
41+ if [message] =~ /dragons/ {
42+ drop {}
43+ }
44+ '';
45+ outputConfig = ''
46+ file {
47+ path => "/tmp/logstash.out"
48+ codec => line { format => "%{message}" }
49+ }
50+ elasticsearch {
51+ hosts => [ "${esUrl}" ]
52+ }
53+ '';
54+ };
5555
56− elasticsearch = {
57− enable = true;
58− package = pkgs.elasticsearch5;
59− };
56+ elasticsearch = {
57+ enable = true;
58+ package = elk.elasticsearch;
59+ };
6060
61− kibana = {
62− enable = true;
63− package = pkgs.kibana5;
64− elasticsearch.url = esUrl;
61+ kibana = {
62+ enable = true;
63+ package = elk.kibana;
64+ elasticsearch.url = esUrl;
65+ };
6566 };
6667 };
67− };
68− };
68+ };
6969
70− testScript = ''
71− startAll;
70+ testScript = ''
71+ startAll;
7272
73− $one->waitForUnit("elasticsearch.service");
73+ $one->waitForUnit("elasticsearch.service");
7474
75− # Continue as long as the status is not "red". The status is probably
76− # "yellow" instead of "green" because we are using a single elasticsearch
77− # node which elasticsearch considers risky.
78− #
79− # TODO: extend this test with multiple elasticsearch nodes and see if the status turns "green".
80− $one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_cluster/health' | jq .status | grep -v red");
75+ # Continue as long as the status is not "red". The status is probably
76+ # "yellow" instead of "green" because we are using a single elasticsearch
77+ # node which elasticsearch considers risky.
78+ #
79+ # TODO: extend this test with multiple elasticsearch nodes and see if the status turns "green".
80+ $one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_cluster/health' | jq .status | grep -v red");
8181
82− # Perform some simple logstash tests.
83− $one->waitForUnit("logstash.service");
84− $one->waitUntilSucceeds("cat /tmp/logstash.out | grep flowers");
85− $one->waitUntilSucceeds("cat /tmp/logstash.out | grep -v dragons");
82+ # Perform some simple logstash tests.
83+ $one->waitForUnit("logstash.service");
84+ $one->waitUntilSucceeds("cat /tmp/logstash.out | grep flowers");
85+ $one->waitUntilSucceeds("cat /tmp/logstash.out | grep -v dragons");
8686
87− # See if kibana is healthy.
88− $one->waitForUnit("kibana.service");
89− $one->waitUntilSucceeds("curl --silent --show-error 'http://localhost:5601/api/status' | jq .status.overall.state | grep green");
87+ # See if kibana is healthy.
88+ $one->waitForUnit("kibana.service");
89+ $one->waitUntilSucceeds("curl --silent --show-error 'http://localhost:5601/api/status' | jq .status.overall.state | grep green");
9090
91− # See if logstash messages arive in elasticsearch.
92− $one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_search' -H 'Content-Type: application/json' -d '{\"query\" : { \"match\" : { \"message\" : \"flowers\"}}}' | jq .hits.total | grep -v 0");
93− $one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_search' -H 'Content-Type: application/json' -d '{\"query\" : { \"match\" : { \"message\" : \"dragons\"}}}' | jq .hits.total | grep 0");
94− '';
95−})
91+ # See if logstash messages arive in elasticsearch.
92+ $one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_search' -H 'Content-Type: application/json' -d '{\"query\" : { \"match\" : { \"message\" : \"flowers\"}}}' | jq .hits.total | grep -v 0");
93+ $one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_search' -H 'Content-Type: application/json' -d '{\"query\" : { \"match\" : { \"message\" : \"dragons\"}}}' | jq .hits.total | grep 0");
94+ '';
95+ };
96+in mapAttrs mkElkTest {
97+ "ELK-5" = {
98+ elasticsearch = pkgs.elasticsearch5;
99+ logstash = pkgs.logstash5;
100+ kibana = pkgs.kibana5;
101+ };
102+ "ELK-6" = {
103+ elasticsearch = pkgs.elasticsearch6;
104+ logstash = pkgs.logstash6;
105+ kibana = pkgs.kibana6;
106+ };
107+}