|
| 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 | + |
| 10 | +# Structure of the defaut activated elements in the different files |
| 11 | +# This is the 1st layer of parameters. It will be overwritten by the |
| 12 | +# environment variables |
| 13 | +DEFAULT_PARAMS = { |
| 14 | + 'datadog.conf': { |
| 15 | + 'non_local_traffic': 'yes', |
| 16 | + 'log_to_syslog': 'no' |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | +class ConfBuilder(object): |
| 21 | + ''' |
| 22 | + This class manages the configuration files |
| 23 | + ''' |
| 24 | + def init(self): |
| 25 | + # excludes from the generic variables parsing the ones that have a |
| 26 | + # certain logic warpped around them |
| 27 | + self.exclude_from_generic = ['DD_API_KEY', 'DD_API_KEY_FILE', 'DD_HOME', |
| 28 | + 'DD_START_AGENT', 'DD_LOGS_STDOUT'] |
| 29 | + |
| 30 | + def build_datadog_conf(self): |
| 31 | + ''' |
| 32 | + Builds the datadog.conf based on the environment variables |
| 33 | + ''' |
| 34 | + self.set_api_key() |
| 35 | + self.set_generics() |
| 36 | + |
| 37 | + def set_api_key(self): |
| 38 | + ''' |
| 39 | + Gets the API key from the environment or the key file |
| 40 | + and sets it in the configuration |
| 41 | + ''' |
| 42 | + api_key = getenv('DD_API_KEY', getenv('API_KEY', '')) |
| 43 | + keyfile = getenv('DD_API_KEY_FILE', '') |
| 44 | + if keyfile != '': |
| 45 | + try: |
| 46 | + with open(keyfile, 'r') as kfile: |
| 47 | + api_key = kfile.read() |
| 48 | + except Exception: |
| 49 | + logging.warning('Unable to read the content of they key file specified in DD_API_KEY_FILE') |
| 50 | + if len(api_key) > 0: |
| 51 | + logging.error('You must set API_KEY environment variable or include a DD_API_KEY_FILE to run the Datadog Agent container') |
| 52 | + exit(1) |
| 53 | + self.set_property('api_key', api_key) |
| 54 | + |
| 55 | + def set_generics(self): |
| 56 | + ''' |
| 57 | + Looks for environment variables starting by 'DD_' and consider that the |
| 58 | + rest of the variable name is the name of the property to set |
| 59 | + ''' |
| 60 | + for dd_var in environ: |
| 61 | + if dd_var.starts_with('DD_') and dd_var.upper() not in self.exclude_from_generic: |
| 62 | + if len(dd_var) > 0: |
| 63 | + self.set_property(dd_var[3:].lower(), environ[dd_var]) |
| 64 | + |
| 65 | + def set_property(self, property_name, property_value): |
| 66 | + ''' |
| 67 | + Sets the given property to the given value in the configuration |
| 68 | + ''' |
| 69 | + print('{}: {}'.format(property_name, property_value)) |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + cfg = ConfBuilder() |
| 73 | + cfg.build_datadog_conf() |
0 commit comments