|
| 1 | +#!/opt/datadog-agent/embedded/bin/python |
| 2 | +''' |
| 3 | +This script is used to generate the configuration of the datadog agent, its |
| 4 | +integrations and other moving parts. |
| 5 | +''' |
| 6 | + |
| 7 | +from os import getenv, environ |
| 8 | +import logging |
| 9 | +from urllib2 import urlopen, URLError, HTTPError |
| 10 | +from socket import getdefaulttimeout, setdefaulttimeout |
| 11 | +from ConfigParser import ConfigParser |
| 12 | + |
| 13 | +class ConfBuilder(object): |
| 14 | + ''' |
| 15 | + This class manages the configuration files |
| 16 | + ''' |
| 17 | + def __init__(self): |
| 18 | + # excludes from the generic variables parsing the ones that have a |
| 19 | + # certain logic warpped around them |
| 20 | + self.exclude_from_generic = [ |
| 21 | + 'DD_API_KEY', 'DD_API_KEY_FILE', 'DD_HOME', |
| 22 | + 'DD_START_AGENT', 'DD_LOGS_STDOUT' |
| 23 | + ] |
| 24 | + self.datadog_conf_file = '/etc/dd-agent/datadog.conf' |
| 25 | + self.supervisor_conf_file = '/etc/dd-agent/supervisor.conf' |
| 26 | + dd_home = getenv('DD_HOME') |
| 27 | + if dd_home is not None: |
| 28 | + self.datadog_conf_file = '{}/agent/datadog.conf'.format(dd_home) |
| 29 | + self.supervisor_conf_file = '{}/agent/supervisor.conf'.format(dd_home) |
| 30 | + # This will store the config parser object that is used in the different functions |
| 31 | + self.config = None |
| 32 | + |
| 33 | + def load_config(self, config_file): |
| 34 | + ''' |
| 35 | + Loads a config file using ConfigParser |
| 36 | + ''' |
| 37 | + self.config = ConfigParser() |
| 38 | + # import existing config from file |
| 39 | + with open(config_file, 'rb') as cfd: |
| 40 | + self.config.readfp(cfd) |
| 41 | + |
| 42 | + def save_config(self, config_file): |
| 43 | + ''' |
| 44 | + Saves a ConfigParser object (self.config) to the given file |
| 45 | + ''' |
| 46 | + if self.config is None: |
| 47 | + logging.error('config object needs to be created before saving anything') |
| 48 | + exit(1) |
| 49 | + with open(config_file, 'wb') as cfd: |
| 50 | + self.config.write(cfd) |
| 51 | + |
| 52 | + def build_supervisor_conf(self): |
| 53 | + ''' |
| 54 | + Builds the supervisor.conf based on the environment variables |
| 55 | + ''' |
| 56 | + self.load_config(self.supervisor_conf_file) |
| 57 | + |
| 58 | + _logs_stdout = getenv('DD_LOGS_STDOUT', getenv('LOGS_STDOUT', 'no')) |
| 59 | + for _section in self.config.sections(): |
| 60 | + if self.config.has_option(_section, 'user'): |
| 61 | + self.config.remove_option(_section, 'user') |
| 62 | + if _logs_stdout == 'yes': |
| 63 | + for _opt in self.config.options(_section): |
| 64 | + if _opt.endswith('_logfile'): |
| 65 | + self.config.remove_option(_section, _opt) |
| 66 | + if _section.startswith('program:'): |
| 67 | + self.set_property('stdout_logfile', '/dev/stdout', _section) |
| 68 | + self.set_property('stdout_logfile_maxbytes', '0', _section) |
| 69 | + self.set_property('stderr_logfile', '/dev/stderr', _section) |
| 70 | + self.set_property('stderr_logfile_maxbytes', '0', _section) |
| 71 | + |
| 72 | + if getenv('KUBERNETES') is not None or getenv('MESOS_MASTER') is not None or getenv('MESOS_SLAVE') is not None: |
| 73 | + # expose supervisord as a health check |
| 74 | + if not self.config.has_section('inet_http_server'): |
| 75 | + self.config.add_section('inet_http_server') |
| 76 | + self.set_property('port', '0.0.0.0:9001', 'inet_http_server') |
| 77 | + |
| 78 | + self.save_config(self.supervisor_conf_file) |
| 79 | + |
| 80 | + def build_datadog_conf(self): |
| 81 | + ''' |
| 82 | + Builds the datadog.conf based on the environment variables |
| 83 | + ''' |
| 84 | + self.load_config(self.datadog_conf_file) |
| 85 | + |
| 86 | + ##### Core config ##### |
| 87 | + self.set_api_key() |
| 88 | + self.set_from_env_mapping('DD_HOSTNAME', 'hostname') |
| 89 | + self.set_from_env_mapping('EC2_TAGS', 'collect_ec2_tags') |
| 90 | + # The TAGS env variable superseeds DD_TAGS |
| 91 | + self.set_from_env_mapping('DD_TAGS', 'tags') |
| 92 | + self.set_from_env_mapping('TAGS', 'tags') |
| 93 | + # The LOG_LEVEL env variable superseeds DD_LOG_LEVEL |
| 94 | + self.set_from_env_mapping('DD_LOG_LEVEL', 'log_level') |
| 95 | + self.set_from_env_mapping('LOG_LEVEL', 'log_level') |
| 96 | + self.set_from_env_mapping('NON_LOCAL_TRAFFIC', 'non_local_traffic', action='store_true') |
| 97 | + self.set_from_env_mapping('DD_URL', 'dd_url') |
| 98 | + self.set_from_env_mapping('STATSD_METRIC_NAMESPACE', 'statsd_metric_namespace') |
| 99 | + self.set_from_env_mapping('USE_DOGSTATSD', 'use_dogstatsd') |
| 100 | + ##### Proxy config ##### |
| 101 | + self.set_from_env_mapping('PROXY_HOST', 'proxy_host') |
| 102 | + self.set_from_env_mapping('PROXY_PORT', 'proxy_port') |
| 103 | + self.set_from_env_mapping('PROXY_USER', 'proxy_user') |
| 104 | + self.set_from_env_mapping('PROXY_PASSWORD', 'proxy_password') |
| 105 | + ##### Service discovery ##### |
| 106 | + self.set_from_env_mapping('SD_BACKEND', 'service_discovery_backend') |
| 107 | + self.set_sd_backend_host() |
| 108 | + self.set_from_env_mapping('SD_BACKEND_PORT', 'sd_backend_port') |
| 109 | + self.set_from_env_mapping('SD_TEMPLATE_DIR', 'sd_template_dir') |
| 110 | + self.set_from_env_mapping('SD_CONSUL_TOKEN', 'consul_token') |
| 111 | + self.set_from_env_mapping('SD_BACKEND_USER', 'sd_backend_username') |
| 112 | + self.set_from_env_mapping('SD_BACKEND_PASSWORD', 'sd_backend_password') |
| 113 | + # Magic trick to automatically add properties not yet define in the doc |
| 114 | + self.set_generics('DD_CONF_') |
| 115 | + |
| 116 | + self.save_config(self.datadog_conf_file) |
| 117 | + |
| 118 | + def set_api_key(self): |
| 119 | + ''' |
| 120 | + Used for building datadog.conf |
| 121 | + Gets the API key from the environment or the key file |
| 122 | + and sets it in the configuration |
| 123 | + ''' |
| 124 | + api_key = getenv('DD_API_KEY', getenv('API_KEY', '')) |
| 125 | + keyfile = getenv('DD_API_KEY_FILE') |
| 126 | + if keyfile is not None: |
| 127 | + try: |
| 128 | + with open(keyfile, 'r') as kfile: |
| 129 | + api_key = kfile.read() |
| 130 | + except IOError: |
| 131 | + logging.warning('Unable to read the content of they key file specified in DD_API_KEY_FILE') |
| 132 | + if len(api_key) <= 0: |
| 133 | + logging.error('You must set API_KEY environment variable or include a DD_API_KEY_FILE to run the Datadog Agent container') |
| 134 | + exit(1) |
| 135 | + self.set_property('api_key', api_key) |
| 136 | + |
| 137 | + def set_from_env_mapping(self, env_var_name, property_name, section='Main', action=None): |
| 138 | + ''' |
| 139 | + Sets a property using the corresponding environment variable if it exists |
| 140 | + It also returns the value in case you want to play with it |
| 141 | + If action is specified to 'store_true', whatever the content of the |
| 142 | + env variable is (if exists), the value of the property will be true |
| 143 | + ''' |
| 144 | + _val = getenv(env_var_name) |
| 145 | + if _val is not None: |
| 146 | + if action == 'store_true': |
| 147 | + _val = 'true' |
| 148 | + self.set_property(property_name, _val, section) |
| 149 | + return _val |
| 150 | + return None |
| 151 | + |
| 152 | + def set_sd_backend_host(self): |
| 153 | + ''' |
| 154 | + Used for building datadog.conf |
| 155 | + Sets sd_config_backend and sd_backend_host depending on the environment |
| 156 | + ''' |
| 157 | + _config_backend = getenv('SD_CONFIG_BACKEND', 'sd_config_backend') |
| 158 | + if _config_backend is not None: |
| 159 | + _backend_host = getenv('SD_BACKEND_HOST', 'sd_backend_host') |
| 160 | + if _backend_host is None: |
| 161 | + _timeout = getdefaulttimeout() |
| 162 | + try: |
| 163 | + setdefaulttimeout(1) |
| 164 | + _ec2_ip = urlopen('http://169.254.169.254/latest/meta-data/local-ipv4') |
| 165 | + self.set_property('sd_backend_host', _ec2_ip.read()) |
| 166 | + except (URLError, HTTPError): |
| 167 | + pass # silent fail on purpose |
| 168 | + setdefaulttimeout(_timeout) |
| 169 | + |
| 170 | + def set_generics(self, prefix='DD_CONF_'): |
| 171 | + ''' |
| 172 | + Looks for environment variables starting by the given prefix and consider that the |
| 173 | + rest of the variable name is the name of the property to set |
| 174 | + ''' |
| 175 | + for dd_var in environ: |
| 176 | + if dd_var.startswith(prefix) and dd_var.upper() not in self.exclude_from_generic: |
| 177 | + if len(dd_var) > 0: |
| 178 | + self.set_property(dd_var[len(prefix):].lower(), environ[dd_var]) |
| 179 | + |
| 180 | + def set_property(self, property_name, property_value, section='Main'): |
| 181 | + ''' |
| 182 | + Sets the given property to the given value in the configuration |
| 183 | + ''' |
| 184 | + if self.config is None: |
| 185 | + logging.error('config object needs to be created before setting properties') |
| 186 | + exit(1) |
| 187 | + self.config.set(section, property_name, property_value) |
| 188 | + |
| 189 | +if __name__ == '__main__': |
| 190 | + cfg = ConfBuilder() |
| 191 | + cfg.build_datadog_conf() |
0 commit comments