summary refs log tree commit diff
path: root/nixos/modules/services/databases/influxdb.nix
blob: b57ccebae16ef413d046ddc36a027260ba3c57e9 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.influxdb;

  influxdbConfig = pkgs.writeText "config.toml" ''
    bind-address = "${cfg.bindAddress}"

    [logging]
    level  = "info"
    file   = "stdout"

    [admin]
    port   = ${toString cfg.adminPort}
    assets = "${pkgs.influxdb}/share/influxdb/admin"

    [api]
    port   = ${toString cfg.apiPort}
    ${cfg.apiExtraConfig}

    [input_plugins]
      ${cfg.inputPluginsConfig}

    [raft]
    dir = "${cfg.dataDir}/raft"
    ${cfg.raftConfig}

    [storage]
    dir = "${cfg.dataDir}/db"
    ${cfg.storageConfig}

    [cluster]
    ${cfg.clusterConfig}

    [sharding]
      ${cfg.shardingConfig}

    [wal]
    dir = "${cfg.dataDir}/wal"
    ${cfg.walConfig}

    ${cfg.extraConfig}
  '';
in
{

  ###### interface

  options = {

    services.influxdb = {

      enable = mkOption {
        default = false;
        description = "Whether to enable the influxdb server";
        type = types.uniq types.bool;
      };

      package = mkOption {
        default = pkgs.influxdb;
        description = "Which influxdb derivation to use";
        type = types.package;
      };

      user = mkOption {
        default = "influxdb";
        description = "User account under which influxdb runs";
        type = types.string;
      };

      group = mkOption {
        default = "influxdb";
        description = "Group under which influxdb runs";
        type = types.string;
      };

      dataDir = mkOption {
        default = "/var/db/influxdb";
        description = "Data directory for influxd data files.";
        type = types.path;
      };

      bindAddress = mkOption {
        default = "127.0.0.1";
        description = "Address where influxdb listens";
        type = types.str;
      };

      adminPort = mkOption {
        default = 8083;
        description = "The port where influxdb admin listens";
        type = types.int;
      };

      apiPort = mkOption {
        default = 8086;
        description = "The port where influxdb api listens";
        type = types.int;
      };

      apiExtraConfig = mkOption {
        default = ''
          read-timeout = "5s"
        '';
        description = "Extra influxdb api configuration";
        example = ''
          ssl-port = 8084
          ssl-cert = /path/to/cert.pem
          read-timeout = "5s"
        '';
        type = types.lines;
      };

      inputPluginsConfig = mkOption {
        default = "";
        description = "Configuration of influxdb extra plugins";
        example = ''
          [input_plugins.graphite]
          enabled = true
          port = 2003
          database = "graphite"
        '';
      };

      raftConfig = mkOption {
        default = ''
          port = 8090
        '';
        description = "Influxdb raft configuration";
        type = types.lines;
      };

      storageConfig = mkOption {
        default = ''
          write-buffer-size = 10000
        '';
        description = "Influxdb raft configuration";
        type = types.lines;
      };

      clusterConfig = mkOption {
        default = ''
          protobuf_port = 8099
          protobuf_timeout = "2s"
          protobuf_heartbeat = "200ms"
          protobuf_min_backoff = "1s"
          protobuf_max_backoff = "10s"

          write-buffer-size = 10000
          max-response-buffer-size = 100

          concurrent-shard-query-limit = 10
        '';
        description = "Influxdb cluster configuration";
        type = types.lines;
      };

      leveldbConfig = mkOption {
        default = ''
          max-open-files = 40
          lru-cache-size = "200m"
          max-open-shards = 0
          point-batch-size = 100
          write-batch-size = 5000000
        '';
        description = "Influxdb leveldb configuration";
        type = types.lines;
      };

      shardingConfig = mkOption {
        default = ''
          replication-factor = 1

          [sharding.short-term]
          duration = "7d"
          split = 1

          [sharding.long-term]
          duration = "30d"
          split = 1
        '';
        description = "Influxdb sharding configuration";
        type = types.lines;
      };

      walConfig = mkOption {
        default = ''
          flush-after = 1000
          bookmark-after = 1000
          index-after = 1000
          requests-per-logfile = 10000
        '';
        description = "Influxdb write-ahead log configuration";
        type = types.lines;
      };

      extraConfig = mkOption {
        default = "";
        description = "Extra configuration options for influxdb";
        type = types.string;
      };
    };

  };


  ###### implementation

  config = mkIf config.services.influxdb.enable {

    systemd.services.influxdb = {
      description = "InfluxDB Server";
      wantedBy = [ "multi-user.target" ];
      after = [ "network-interfaces.target" ];
      serviceConfig = {
        ExecStart = ''${cfg.package}/bin/influxdb -config "${influxdbConfig}"'';
        User = "${cfg.user}";
        Group = "${cfg.group}";
        PermissionsStartOnly = true;
      };
      preStart = ''
        mkdir -m 0770 -p ${cfg.dataDir}
        if [ "$(id -u)" = 0 ]; then chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}; fi
      '';
      postStart = mkBefore ''
        until ${pkgs.curl}/bin/curl -s -o /dev/null 'http://${cfg.bindAddress}:${toString cfg.apiPort}/'; do
          sleep 1;
        done
      '';
    };

    users.extraUsers = optional (cfg.user == "influxdb") {
      name = "influxdb";
      uid = config.ids.uids.influxdb;
      description = "Influxdb daemon user";
    };

    users.extraGroups = optional (cfg.group == "influxdb") {
      name = "influxdb";
      gid = config.ids.gids.influxdb;
    };
  };

}