| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  |   cfg = config.services.datadog-agent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ddConf = { | 
					
						
							| 
									
										
										
										
											2018-10-23 12:29:53 +02:00
										 |  |  |     skip_ssl_validation = false; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |     confd_path          = "/etc/datadog-agent/conf.d"; | 
					
						
							|  |  |  |     additional_checksd  = "/etc/datadog-agent/checks.d"; | 
					
						
							| 
									
										
										
										
											2018-08-25 00:18:59 +02:00
										 |  |  |     use_dogstatsd       = true; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   // optionalAttrs (cfg.logLevel != null) { log_level = cfg.logLevel; } | 
					
						
							|  |  |  |   // optionalAttrs (cfg.hostname != null) { inherit (cfg) hostname; } | 
					
						
							| 
									
										
										
										
											2020-09-25 22:06:38 +02:00
										 |  |  |   // optionalAttrs (cfg.ddUrl != null) { dd_url = cfg.ddUrl; } | 
					
						
							|  |  |  |   // optionalAttrs (cfg.site != null) { site = cfg.site; } | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |   // optionalAttrs (cfg.tags != null ) { tags = concatStringsSep ", " cfg.tags; } | 
					
						
							| 
									
										
										
										
											2018-09-12 17:46:31 +01:00
										 |  |  |   // optionalAttrs (cfg.enableLiveProcessCollection) { process_config = { enabled = "true"; }; } | 
					
						
							| 
									
										
										
										
											2018-10-23 12:29:53 +02:00
										 |  |  |   // optionalAttrs (cfg.enableTraceAgent) { apm_config = { enabled = true; }; } | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |   // cfg.extraConfig; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 14:00:24 +02:00
										 |  |  |   # Generate Datadog configuration files for each configured checks. | 
					
						
							|  |  |  |   # This works because check configurations have predictable paths, | 
					
						
							|  |  |  |   # and because JSON is a valid subset of YAML. | 
					
						
							| 
									
										
										
										
											2019-09-14 19:51:29 +02:00
										 |  |  |   makeCheckConfigs = entries: mapAttrs' (name: conf: { | 
					
						
							|  |  |  |     name = "datadog-agent/conf.d/${name}.d/conf.yaml"; | 
					
						
							|  |  |  |     value.source = pkgs.writeText "${name}-check-conf.yaml" (builtins.toJSON conf); | 
					
						
							| 
									
										
										
										
											2018-08-09 14:00:24 +02:00
										 |  |  |   }) entries; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   defaultChecks = { | 
					
						
							|  |  |  |     disk = cfg.diskCheck; | 
					
						
							|  |  |  |     network = cfg.networkCheck; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Assemble all check configurations and the top-level agent | 
					
						
							|  |  |  |   # configuration. | 
					
						
							| 
									
										
										
										
											2019-09-14 19:51:29 +02:00
										 |  |  |   etcfiles = with pkgs; with builtins; | 
					
						
							|  |  |  |   { "datadog-agent/datadog.yaml" = { | 
					
						
							|  |  |  |       source = writeText "datadog.yaml" (toJSON ddConf); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } // makeCheckConfigs (cfg.checks // defaultChecks); | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 16:01:12 +02:00
										 |  |  |   # Apply the configured extraIntegrations to the provided agent | 
					
						
							|  |  |  |   # package. See the documentation of `dd-agent/integrations-core.nix` | 
					
						
							|  |  |  |   # for detailed information on this. | 
					
						
							| 
									
										
										
										
											2019-06-27 13:52:16 -04:00
										 |  |  |   datadogPkg = cfg.package.override { | 
					
						
							|  |  |  |     pythonPackages = pkgs.datadog-integrations-core cfg.extraIntegrations; | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  | in { | 
					
						
							|  |  |  |   options.services.datadog-agent = { | 
					
						
							|  |  |  |     enable = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Whether to enable the datadog-agent v6 monitoring service | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     package = mkOption { | 
					
						
							|  |  |  |       default = pkgs.datadog-agent; | 
					
						
							|  |  |  |       defaultText = "pkgs.datadog-agent"; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							| 
									
										
										
										
											2018-08-09 16:01:12 +02:00
										 |  |  |         Which DataDog v6 agent package to use. Note that the provided | 
					
						
							| 
									
										
										
										
											2019-06-27 13:52:16 -04:00
										 |  |  |         package is expected to have an overridable `pythonPackages`-attribute | 
					
						
							| 
									
										
										
										
											2018-08-09 16:01:12 +02:00
										 |  |  |         which configures the Python environment with the Datadog | 
					
						
							|  |  |  |         checks. | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       '';
 | 
					
						
							|  |  |  |       type = types.package; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     apiKeyFile = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Path to a file containing the Datadog API key to associate the | 
					
						
							|  |  |  |         agent with your account. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       example = "/run/keys/datadog_api_key"; | 
					
						
							|  |  |  |       type = types.path; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 21:02:20 +02:00
										 |  |  |     ddUrl = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							| 
									
										
										
										
											2020-09-25 22:06:38 +02:00
										 |  |  |         Custom dd_url to configure the agent with. Useful if traffic to datadog | 
					
						
							|  |  |  |         needs to go through a proxy. | 
					
						
							|  |  |  |         Don't use this to point to another datadog site (EU) - use site instead. | 
					
						
							| 
									
										
										
										
											2020-09-12 21:02:20 +02:00
										 |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2020-09-25 22:06:38 +02:00
										 |  |  |       default = null; | 
					
						
							|  |  |  |       example = "http://haproxy.example.com:3834"; | 
					
						
							|  |  |  |       type = types.nullOr types.str; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     site = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         The datadog site to point the agent towards. | 
					
						
							|  |  |  |         Set to datadoghq.eu to point it to their EU site. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       default = null; | 
					
						
							|  |  |  |       example = "datadoghq.eu"; | 
					
						
							|  |  |  |       type = types.nullOr types.str; | 
					
						
							| 
									
										
										
										
											2020-09-12 21:02:20 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |     tags = mkOption { | 
					
						
							|  |  |  |       description = "The tags to mark this Datadog agent"; | 
					
						
							|  |  |  |       example = [ "test" "service" ]; | 
					
						
							|  |  |  |       default = null; | 
					
						
							|  |  |  |       type = types.nullOr (types.listOf types.str); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hostname = mkOption { | 
					
						
							|  |  |  |       description = "The hostname to show in the Datadog dashboard (optional)"; | 
					
						
							|  |  |  |       default = null; | 
					
						
							|  |  |  |       example = "mymachine.mydomain"; | 
					
						
							| 
									
										
										
										
											2019-08-08 22:48:27 +02:00
										 |  |  |       type = types.nullOr types.str; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     logLevel = mkOption { | 
					
						
							|  |  |  |       description = "Logging verbosity."; | 
					
						
							|  |  |  |       default = null; | 
					
						
							|  |  |  |       type = types.nullOr (types.enum ["DEBUG" "INFO" "WARN" "ERROR"]); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 16:01:12 +02:00
										 |  |  |     extraIntegrations = mkOption { | 
					
						
							|  |  |  |       default = {}; | 
					
						
							|  |  |  |       type    = types.attrs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Extra integrations from the Datadog core-integrations | 
					
						
							|  |  |  |         repository that should be built and included. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         By default the included integrations are disk, mongo, network, | 
					
						
							|  |  |  |         nginx and postgres. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         To include additional integrations the name of the derivation | 
					
						
							|  |  |  |         and a function to filter its dependencies from the Python | 
					
						
							|  |  |  |         package set must be provided. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       example = { | 
					
						
							|  |  |  |         ntp = (pythonPackages: [ pythonPackages.ntplib ]); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |     extraConfig = mkOption { | 
					
						
							|  |  |  |       default = {}; | 
					
						
							|  |  |  |       type = types.attrs; | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Extra configuration options that will be merged into the | 
					
						
							|  |  |  |         main config file <filename>datadog.yaml</filename>. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |      }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 17:46:31 +01:00
										 |  |  |     enableLiveProcessCollection = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Whether to enable the live process collection agent. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-10-23 12:29:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     enableTraceAgent = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Whether to enable the trace agent. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  |       default = false; | 
					
						
							|  |  |  |       type = types.bool; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 14:00:24 +02:00
										 |  |  |     checks = mkOption { | 
					
						
							|  |  |  |       description = ''
 | 
					
						
							|  |  |  |         Configuration for all Datadog checks. Keys of this attribute | 
					
						
							|  |  |  |         set will be used as the name of the check to create the | 
					
						
							|  |  |  |         appropriate configuration in `conf.d/$check.d/conf.yaml`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The configuration is converted into JSON from the plain Nix | 
					
						
							|  |  |  |         language configuration, meaning that you should write | 
					
						
							|  |  |  |         configuration adhering to Datadog's documentation - but in Nix | 
					
						
							|  |  |  |         language. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Refer to the implementation of this module (specifically the | 
					
						
							|  |  |  |         definition of `defaultChecks`) for an example. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Note: The 'disk' and 'network' check are configured in | 
					
						
							|  |  |  |         separate options because they exist by default. Attempting to | 
					
						
							|  |  |  |         override their configuration here will have no effect. | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       example = { | 
					
						
							|  |  |  |         http_check = { | 
					
						
							|  |  |  |           init_config = null; # sic! | 
					
						
							|  |  |  |           instances = [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               name = "some-service"; | 
					
						
							|  |  |  |               url = "http://localhost:1337/healthz"; | 
					
						
							|  |  |  |               tags = [ "some-service" ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           ]; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       default = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # sic! The structure of the values is up to the check, so we can | 
					
						
							|  |  |  |       # not usefully constrain the type further. | 
					
						
							|  |  |  |       type = with types; attrsOf attrs; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     diskCheck = mkOption { | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       description = "Disk check config"; | 
					
						
							|  |  |  |       type = types.attrs; | 
					
						
							|  |  |  |       default = { | 
					
						
							|  |  |  |         init_config = {}; | 
					
						
							| 
									
										
										
										
											2018-12-24 14:16:22 +03:00
										 |  |  |         instances = [ { use_mount = "false"; } ]; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2018-08-09 14:00:24 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 14:00:24 +02:00
										 |  |  |     networkCheck = mkOption { | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       description = "Network check config"; | 
					
						
							|  |  |  |       type = types.attrs; | 
					
						
							|  |  |  |       default = { | 
					
						
							|  |  |  |         init_config = {}; | 
					
						
							|  |  |  |         # Network check only supports one configured instance | 
					
						
							|  |  |  |         instances = [ { collect_connection_state = false; | 
					
						
							| 
									
										
										
										
											2018-08-09 14:00:24 +02:00
										 |  |  |           excluded_interfaces = [ "lo" "lo0" ]; } ]; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							| 
									
										
										
										
											2019-02-28 22:48:49 +03:00
										 |  |  |     environment.systemPackages = [ datadogPkg pkgs.sysstat pkgs.procps pkgs.iproute ]; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 19:51:29 +02:00
										 |  |  |     users.users.datadog = { | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       description = "Datadog Agent User"; | 
					
						
							|  |  |  |       uid = config.ids.uids.datadog; | 
					
						
							|  |  |  |       group = "datadog"; | 
					
						
							|  |  |  |       home = "/var/log/datadog/"; | 
					
						
							|  |  |  |       createHome = true; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 19:51:29 +02:00
										 |  |  |     users.groups.datadog.gid = config.ids.gids.datadog; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     systemd.services = let | 
					
						
							|  |  |  |       makeService = attrs: recursiveUpdate { | 
					
						
							| 
									
										
										
										
											2019-02-28 22:48:49 +03:00
										 |  |  |         path = [ datadogPkg pkgs.python pkgs.sysstat pkgs.procps pkgs.iproute ]; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |         wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |         serviceConfig = { | 
					
						
							|  |  |  |           User = "datadog"; | 
					
						
							|  |  |  |           Group = "datadog"; | 
					
						
							|  |  |  |           Restart = "always"; | 
					
						
							|  |  |  |           RestartSec = 2; | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2020-04-22 23:32:12 +02:00
										 |  |  |         restartTriggers = [ datadogPkg ] ++  map (x: x.source) (attrValues etcfiles); | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       } attrs; | 
					
						
							|  |  |  |     in { | 
					
						
							|  |  |  |       datadog-agent = makeService { | 
					
						
							|  |  |  |         description = "Datadog agent monitor"; | 
					
						
							|  |  |  |         preStart = ''
 | 
					
						
							|  |  |  |           chown -R datadog: /etc/datadog-agent | 
					
						
							|  |  |  |           rm -f /etc/datadog-agent/auth_token | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |         script = ''
 | 
					
						
							| 
									
										
										
										
											2018-08-09 16:01:12 +02:00
										 |  |  |           export DD_API_KEY=$(head -n 1 ${cfg.apiKeyFile}) | 
					
						
							| 
									
										
										
										
											2019-03-18 23:31:04 +03:00
										 |  |  |           exec ${datadogPkg}/bin/agent run -c /etc/datadog-agent/datadog.yaml | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |         '';
 | 
					
						
							|  |  |  |         serviceConfig.PermissionsStartOnly = true; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 14:00:24 +02:00
										 |  |  |       dd-jmxfetch = lib.mkIf (lib.hasAttr "jmx" cfg.checks) (makeService { | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |         description = "Datadog JMX Fetcher"; | 
					
						
							| 
									
										
										
										
											2018-08-09 16:01:12 +02:00
										 |  |  |         path = [ datadogPkg pkgs.python pkgs.sysstat pkgs.procps pkgs.jdk ]; | 
					
						
							|  |  |  |         serviceConfig.ExecStart = "${datadogPkg}/bin/dd-jmxfetch"; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-09-12 17:46:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       datadog-process-agent = lib.mkIf cfg.enableLiveProcessCollection (makeService { | 
					
						
							|  |  |  |         description = "Datadog Live Process Agent"; | 
					
						
							|  |  |  |         path = [ ]; | 
					
						
							|  |  |  |         script = ''
 | 
					
						
							|  |  |  |           export DD_API_KEY=$(head -n 1 ${cfg.apiKeyFile}) | 
					
						
							|  |  |  |           ${pkgs.datadog-process-agent}/bin/agent --config /etc/datadog-agent/datadog.yaml | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-10-23 12:29:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       datadog-trace-agent = lib.mkIf cfg.enableTraceAgent (makeService { | 
					
						
							|  |  |  |         description = "Datadog Trace Agent"; | 
					
						
							|  |  |  |         path = [ ]; | 
					
						
							|  |  |  |         script = ''
 | 
					
						
							|  |  |  |           export DD_API_KEY=$(head -n 1 ${cfg.apiKeyFile}) | 
					
						
							| 
									
										
										
										
											2019-03-01 10:28:24 +03:00
										 |  |  |           ${datadogPkg}/bin/trace-agent -config /etc/datadog-agent/datadog.yaml | 
					
						
							| 
									
										
										
										
											2018-10-23 12:29:53 +02:00
										 |  |  |         '';
 | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-27 14:59:38 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     environment.etc = etcfiles; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |