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
77 etcfiles =
78 [ { source = ddConf;
79 target = "dd-agent/datadog.conf";
80 }
81 { source = diskConfig;
82 target = "dd-agent/conf.d/disk.yaml";
83 }
84 { source = networkConfig;
85 target = "dd-agent/conf.d/network.yaml";
86 } ] ++
87 (optional (cfg.postgresqlConfig != null)
88 { source = postgresqlConfig;
89 target = "dd-agent/conf.d/postgres.yaml";
90 }) ++
91 (optional (cfg.nginxConfig != null)
92 { source = nginxConfig;
93 target = "dd-agent/conf.d/nginx.yaml";
94 }) ++
95 (optional (cfg.mongoConfig != null)
96 { source = mongoConfig;
97 target = "dd-agent/conf.d/mongo.yaml";
98 }) ++
99 (optional (cfg.jmxConfig != null)
100 { source = jmxConfig;
101 target = "dd-agent/conf.d/jmx.yaml";
102 });
103
104in {
105 options.services.dd-agent = {
106 enable = mkOption {
107 description = "Whether to enable the dd-agent montioring service";
108 default = false;
109 type = types.bool;
110 };
111
112 api_key = mkOption {
113 description = "The Datadog API key to associate the agent with your account";
114 example = "ae0aa6a8f08efa988ba0a17578f009ab";
115 type = types.str;
116 };
117
118 tags = mkOption {
119 description = "The tags to mark this Datadog agent";
120 example = [ "test" "service" ];
121 default = null;
122 type = types.nullOr (types.listOf types.str);
123 };
124
125 hostname = mkOption {
126 description = "The hostname to show in the Datadog dashboard (optional)";
127 default = null;
128 example = "mymachine.mydomain";
129 type = types.uniq (types.nullOr types.string);
130 };
131
132 postgresqlConfig = mkOption {
133 description = "Datadog PostgreSQL integration configuration";
134 default = null;
135 type = types.uniq (types.nullOr types.string);
136 };
137
138 nginxConfig = mkOption {
139 description = "Datadog nginx integration configuration";
140 default = null;
141 type = types.uniq (types.nullOr types.string);
142 };
143
144 mongoConfig = mkOption {
145 description = "MongoDB integration configuration";
146 default = null;
147 type = types.uniq (types.nullOr types.string);
148 };
149
150 jmxConfig = mkOption {
151 description = "JMX integration configuration";
152 default = null;
153 type = types.uniq (types.nullOr types.string);
154 };
155
156 };
157
158 config = mkIf cfg.enable {
159 environment.systemPackages = [ pkgs."dd-agent" pkgs.sysstat pkgs.procps ];
160
161 users.extraUsers.datadog = {
162 description = "Datadog Agent User";
163 uid = config.ids.uids.datadog;
164 group = "datadog";
165 home = "/var/log/datadog/";
166 createHome = true;
167 };
168
169 users.extraGroups.datadog.gid = config.ids.gids.datadog;
170
171 systemd.services.dd-agent = {
172 description = "Datadog agent monitor";
173 path = [ pkgs."dd-agent" pkgs.python pkgs.sysstat pkgs.procps ];
174 wantedBy = [ "multi-user.target" ];
175 serviceConfig = {
176 ExecStart = "${pkgs.dd-agent}/bin/dd-agent foreground";
177 User = "datadog";
178 Group = "datadog";
179 Restart = "always";
180 RestartSec = 2;
181 };
182 restartTriggers = [ pkgs.dd-agent ddConf diskConfig networkConfig postgresqlConfig nginxConfig mongoConfig jmxConfig ];
183 };
184
185 systemd.services.dogstatsd = {
186 description = "Datadog statsd";
187 path = [ pkgs."dd-agent" pkgs.python pkgs.procps ];
188 wantedBy = [ "multi-user.target" ];
189 serviceConfig = {
190 ExecStart = "${pkgs.dd-agent}/bin/dogstatsd start";
191 User = "datadog";
192 Group = "datadog";
193 Type = "forking";
194 PIDFile = "/tmp/dogstatsd.pid";
195 Restart = "always";
196 RestartSec = 2;
197 };
198 restartTriggers = [ pkgs.dd-agent ddConf diskConfig networkConfig postgresqlConfig nginxConfig mongoConfig jmxConfig ];
199 };
200
201 systemd.services.dd-jmxfetch = lib.mkIf (cfg.jmxConfig != null) {
202 description = "Datadog JMX Fetcher";
203 path = [ pkgs."dd-agent" pkgs.python pkgs.sysstat pkgs.procps pkgs.jdk ];
204 wantedBy = [ "multi-user.target" ];
205 serviceConfig = {
206 ExecStart = "${pkgs.dd-agent}/bin/dd-jmxfetch";
207 User = "datadog";
208 Group = "datadog";
209 Restart = "always";
210 RestartSec = 2;
211 };
212 restartTriggers = [ pkgs.dd-agent ddConf diskConfig networkConfig postgresqlConfig nginxConfig mongoConfig jmxConfig ];
213 };
214
215 environment.etc = etcfiles;
216 };
217}