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