1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.dd-agent;
7
8 ddConf = pkgs.writeText "datadog.conf" ''
9 [Main]
10 dd_url: https://app.datadoghq.com
11 skip_ssl_validation: no
12 api_key: ${cfg.api_key}
13 ${optionalString (cfg.hostname != null) "hostname: ${cfg.hostname}"}
14
15 collector_log_file: /var/log/datadog/collector.log
16 forwarder_log_file: /var/log/datadog/forwarder.log
17 dogstatsd_log_file: /var/log/datadog/dogstatsd.log
18 pup_log_file: /var/log/datadog/pup.log
19
20 # proxy_host: my-proxy.com
21 # proxy_port: 3128
22 # proxy_user: user
23 # proxy_password: password
24
25 # tags: mytag0, mytag1
26 ${optionalString (cfg.tags != null ) "tags: ${concatStringsSep "," cfg.tags }"}
27
28 # collect_ec2_tags: no
29 # recent_point_threshold: 30
30 # use_mount: no
31 # listen_port: 17123
32 # graphite_listen_port: 17124
33 # non_local_traffic: no
34 # use_curl_http_client: False
35 # bind_host: localhost
36
37 # use_pup: no
38 # pup_port: 17125
39 # pup_interface: localhost
40 # pup_url: http://localhost:17125
41
42 # dogstatsd_port : 8125
43 # dogstatsd_interval : 10
44 # dogstatsd_normalize : yes
45 # statsd_forward_host: address_of_own_statsd_server
46 # statsd_forward_port: 8125
47
48 # device_blacklist_re: .*\/dev\/mapper\/lxc-box.*
49
50 # ganglia_host: localhost
51 # ganglia_port: 8651
52 '';
53
54 diskConfig = pkgs.writeText "disk.yaml" ''
55 init_config:
56
57 instances:
58 - use_mount: no
59 '';
60
61 networkConfig = pkgs.writeText "network.yaml" ''
62 init_config:
63
64 instances:
65 # Network check only supports one configured instance
66 - collect_connection_state: false
67 excluded_interfaces:
68 - lo
69 - lo0
70 '';
71
72 postgresqlConfig = pkgs.writeText "postgres.yaml" cfg.postgresqlConfig;
73 nginxConfig = pkgs.writeText "nginx.yaml" cfg.nginxConfig;
74 mongoConfig = pkgs.writeText "mongo.yaml" cfg.mongoConfig;
75 jmxConfig = pkgs.writeText "jmx.yaml" cfg.jmxConfig;
76 processConfig = pkgs.writeText "process.yaml" cfg.processConfig;
77
78 etcfiles =
79 let
80 defaultConfd = import ./dd-agent-defaults.nix;
81 in (map (f: { source = "${pkgs.dd-agent}/agent/conf.d-system/${f}";
82 target = "dd-agent/conf.d/${f}";
83 }) defaultConfd) ++ [
84 { source = ddConf;
85 target = "dd-agent/datadog.conf";
86 }
87 { source = diskConfig;
88 target = "dd-agent/conf.d/disk.yaml";
89 }
90 { source = networkConfig;
91 target = "dd-agent/conf.d/network.yaml";
92 } ] ++
93 (optional (cfg.postgresqlConfig != null)
94 { source = postgresqlConfig;
95 target = "dd-agent/conf.d/postgres.yaml";
96 }) ++
97 (optional (cfg.nginxConfig != null)
98 { source = nginxConfig;
99 target = "dd-agent/conf.d/nginx.yaml";
100 }) ++
101 (optional (cfg.mongoConfig != null)
102 { source = mongoConfig;
103 target = "dd-agent/conf.d/mongo.yaml";
104 }) ++
105 (optional (cfg.processConfig != null)
106 { source = processConfig;
107 target = "dd-agent/conf.d/process.yaml";
108 }) ++
109 (optional (cfg.jmxConfig != null)
110 { source = jmxConfig;
111 target = "dd-agent/conf.d/jmx.yaml";
112 });
113
114in {
115 options.services.dd-agent = {
116 enable = mkOption {
117 description = "Whether to enable the dd-agent montioring service";
118 default = false;
119 type = types.bool;
120 };
121
122 api_key = mkOption {
123 description = "The Datadog API key to associate the agent with your account";
124 example = "ae0aa6a8f08efa988ba0a17578f009ab";
125 type = types.str;
126 };
127
128 tags = mkOption {
129 description = "The tags to mark this Datadog agent";
130 example = [ "test" "service" ];
131 default = null;
132 type = types.nullOr (types.listOf types.str);
133 };
134
135 hostname = mkOption {
136 description = "The hostname to show in the Datadog dashboard (optional)";
137 default = null;
138 example = "mymachine.mydomain";
139 type = types.uniq (types.nullOr types.string);
140 };
141
142 postgresqlConfig = mkOption {
143 description = "Datadog PostgreSQL integration configuration";
144 default = null;
145 type = types.uniq (types.nullOr types.string);
146 };
147
148 nginxConfig = mkOption {
149 description = "Datadog nginx integration configuration";
150 default = null;
151 type = types.uniq (types.nullOr types.string);
152 };
153
154 mongoConfig = mkOption {
155 description = "MongoDB integration configuration";
156 default = null;
157 type = types.uniq (types.nullOr types.string);
158 };
159
160 jmxConfig = mkOption {
161 description = "JMX integration configuration";
162 default = null;
163 type = types.uniq (types.nullOr types.string);
164 };
165
166 processConfig = mkOption {
167 description = ''
168 Process integration configuration
169
170 See http://docs.datadoghq.com/integrations/process/
171 '';
172 default = null;
173 type = types.uniq (types.nullOr types.string);
174 };
175
176 };
177
178 config = mkIf cfg.enable {
179 environment.systemPackages = [ pkgs."dd-agent" pkgs.sysstat pkgs.procps ];
180
181 users.extraUsers.datadog = {
182 description = "Datadog Agent User";
183 uid = config.ids.uids.datadog;
184 group = "datadog";
185 home = "/var/log/datadog/";
186 createHome = true;
187 };
188
189 users.extraGroups.datadog.gid = config.ids.gids.datadog;
190
191 systemd.services.dd-agent = {
192 description = "Datadog agent monitor";
193 path = [ pkgs."dd-agent" pkgs.python pkgs.sysstat pkgs.procps ];
194 wantedBy = [ "multi-user.target" ];
195 serviceConfig = {
196 ExecStart = "${pkgs.dd-agent}/bin/dd-agent foreground";
197 User = "datadog";
198 Group = "datadog";
199 Restart = "always";
200 RestartSec = 2;
201 };
202 restartTriggers = [ pkgs.dd-agent ddConf diskConfig networkConfig postgresqlConfig nginxConfig mongoConfig jmxConfig processConfig ];
203 };
204
205 systemd.services.dogstatsd = {
206 description = "Datadog statsd";
207 path = [ pkgs."dd-agent" pkgs.python pkgs.procps ];
208 wantedBy = [ "multi-user.target" ];
209 serviceConfig = {
210 ExecStart = "${pkgs.dd-agent}/bin/dogstatsd start";
211 User = "datadog";
212 Group = "datadog";
213 Type = "forking";
214 PIDFile = "/tmp/dogstatsd.pid";
215 Restart = "always";
216 RestartSec = 2;
217 };
218 restartTriggers = [ pkgs.dd-agent ddConf diskConfig networkConfig postgresqlConfig nginxConfig mongoConfig jmxConfig processConfig ];
219 };
220
221 systemd.services.dd-jmxfetch = lib.mkIf (cfg.jmxConfig != null) {
222 description = "Datadog JMX Fetcher";
223 path = [ pkgs."dd-agent" pkgs.python pkgs.sysstat pkgs.procps pkgs.jdk ];
224 wantedBy = [ "multi-user.target" ];
225 serviceConfig = {
226 ExecStart = "${pkgs.dd-agent}/bin/dd-jmxfetch";
227 User = "datadog";
228 Group = "datadog";
229 Restart = "always";
230 RestartSec = 2;
231 };
232 restartTriggers = [ pkgs.dd-agent ddConf diskConfig networkConfig postgresqlConfig nginxConfig mongoConfig jmxConfig ];
233 };
234
235 environment.etc = etcfiles;
236 };
237}