summary refs log tree commit diff
path: root/nixos/modules/services/logging/logstash.nix
blob: 802dd454878b376f64f546c562f7b980b12d199b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.logstash;

in

{
  ###### interface

  options = {
    services.logstash = {
      enable = mkOption {
        default = false;
        description = "Enable logstash";
      };

      enableWeb = mkOption {
        default = false;
        description = "Enable logstash web interface";
      };

      inputConfig = mkOption {
        default = ''stdin { type => "example" }'';
        description = "Logstash input configuration";
        example = ''
          # Read from journal
          pipe {
            command => "${pkgs.systemd}/bin/journalctl -f -o json"
            type => "syslog" codec => json {}
          }
        '';
      };

      filterConfig = mkOption {
        default = ''noop {}'';
        description = "logstash filter configuration";
        example = ''
          if [type] == "syslog" {
            # Keep only relevant systemd fields
            # http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
            prune {
              whitelist_names => [
                "type", "@timestamp", "@version",
                "MESSAGE", "PRIORITY", "SYSLOG_FACILITY",
              ]
            }
          }
        '';
      };

      outputConfig = mkOption {
        default = ''stdout { debug => true debug_format => "json"}'';
        description = "Logstash output configuration";
        example = ''
          redis { host => "localhost" data_type => "list" key => "logstash" codec => json }
          elasticsearch { embedded => true }
        '';
      };
    };
  };


  ###### implementation

  config = mkIf cfg.enable {
    systemd.services.logstash = with pkgs; {
      description = "Logstash Daemon";
      wantedBy = [ "multi-user.target" ];
      environment = { JAVA_HOME = jre; };
      serviceConfig = {
        ExecStart = "${logstash}/bin/logstash agent -f ${writeText "logstash.conf" ''
          input {
            ${cfg.inputConfig}
          }

          filter {
            ${cfg.filterConfig}
          }

          output {
            ${cfg.outputConfig}
          }
        ''} ${optionalString cfg.enableWeb "-- web"}";
      };
    };
  };
}