1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.statsd;
8
9 isBuiltinBackend = name:
10 builtins.elem name [ "graphite" "console" "repeater" ];
11
12 configFile = pkgs.writeText "statsd.conf" ''
13 {
14 address: "${cfg.listenAddress}",
15 port: "${toString cfg.port}",
16 mgmt_address: "${cfg.mgmt_address}",
17 mgmt_port: "${toString cfg.mgmt_port}",
18 backends: [${
19 concatMapStringsSep "," (name:
20 if (isBuiltinBackend name)
21 then ''"./backends/${name}"''
22 else ''"${name}"''
23 ) cfg.backends}],
24 ${optionalString (cfg.graphiteHost!=null) ''graphiteHost: "${cfg.graphiteHost}",''}
25 ${optionalString (cfg.graphitePort!=null) ''graphitePort: "${toString cfg.graphitePort}",''}
26 console: {
27 prettyprint: false
28 },
29 log: {
30 backend: "syslog"
31 },
32 automaticConfigReload: false${optionalString (cfg.extraConfig != null) ","}
33 ${cfg.extraConfig}
34 }
35 '';
36
37in
38
39{
40
41 ###### interface
42
43 options.services.statsd = {
44
45 enable = mkOption {
46 description = "Whether to enable statsd stats aggregation service";
47 default = false;
48 type = types.bool;
49 };
50
51 listenAddress = mkOption {
52 description = "Address that statsd listens on over UDP";
53 default = "127.0.0.1";
54 type = types.str;
55 };
56
57 port = mkOption {
58 description = "Port that stats listens for messages on over UDP";
59 default = 8125;
60 type = types.int;
61 };
62
63 mgmt_address = mkOption {
64 description = "Address to run management TCP interface on";
65 default = "127.0.0.1";
66 type = types.str;
67 };
68
69 mgmt_port = mkOption {
70 description = "Port to run the management TCP interface on";
71 default = 8126;
72 type = types.int;
73 };
74
75 backends = mkOption {
76 description = "List of backends statsd will use for data persistence";
77 default = [];
78 example = [
79 "graphite"
80 "console"
81 "repeater"
82 "statsd-librato-backend"
83 "stackdriver-statsd-backend"
84 "statsd-influxdb-backend"
85 ];
86 type = types.listOf types.str;
87 };
88
89 graphiteHost = mkOption {
90 description = "Hostname or IP of Graphite server";
91 default = null;
92 type = types.nullOr types.str;
93 };
94
95 graphitePort = mkOption {
96 description = "Port of Graphite server (i.e. carbon-cache).";
97 default = null;
98 type = types.nullOr types.int;
99 };
100
101 extraConfig = mkOption {
102 description = "Extra configuration options for statsd";
103 default = "";
104 type = types.nullOr types.str;
105 };
106
107 };
108
109 ###### implementation
110
111 config = mkIf cfg.enable {
112
113 users.extraUsers = singleton {
114 name = "statsd";
115 uid = config.ids.uids.statsd;
116 description = "Statsd daemon user";
117 };
118
119 systemd.services.statsd = {
120 description = "Statsd Server";
121 wantedBy = [ "multi-user.target" ];
122 environment = {
123 NODE_PATH=concatMapStringsSep ":"
124 (pkg: "${builtins.getAttr pkg pkgs.statsd.nodePackages}/lib/node_modules")
125 (filter (name: !isBuiltinBackend name) cfg.backends);
126 };
127 serviceConfig = {
128 ExecStart = "${pkgs.statsd}/bin/statsd ${configFile}";
129 User = "statsd";
130 };
131 };
132
133 environment.systemPackages = [ pkgs.statsd ];
134
135 };
136
137}