-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
795c013
commit 86e3fe0
Showing
7 changed files
with
75 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/opt/datadog-agent/embedded/bin/python | ||
''' | ||
This script is used to generate the configuration of the datadog agent, its | ||
integrations and other moving parts. | ||
''' | ||
|
||
from os import getenv, environ | ||
import logging | ||
|
||
# Structure of the defaut activated elements in the different files | ||
# This is the 1st layer of parameters. It will be overwritten by the | ||
# environment variables | ||
DEFAULT_PARAMS = { | ||
'datadog.conf': { | ||
'non_local_traffic': 'yes', | ||
'log_to_syslog': 'no' | ||
} | ||
} | ||
|
||
class ConfBuilder(object): | ||
''' | ||
This class manages the configuration files | ||
''' | ||
def init(self): | ||
# excludes from the generic variables parsing the ones that have a | ||
# certain logic warpped around them | ||
self.exclude_from_generic = ['DD_API_KEY', 'DD_API_KEY_FILE', 'DD_HOME', | ||
'DD_START_AGENT', 'DD_LOGS_STDOUT'] | ||
|
||
def build_datadog_conf(self): | ||
''' | ||
Builds the datadog.conf based on the environment variables | ||
''' | ||
self.set_api_key() | ||
self.set_generics() | ||
|
||
def set_api_key(self): | ||
''' | ||
Gets the API key from the environment or the key file | ||
and sets it in the configuration | ||
''' | ||
api_key = getenv('DD_API_KEY', getenv('API_KEY', '')) | ||
keyfile = getenv('DD_API_KEY_FILE', '') | ||
if keyfile != '': | ||
try: | ||
with open(keyfile, 'r') as kfile: | ||
api_key = kfile.read() | ||
except Exception: | ||
logging.warning('Unable to read the content of they key file specified in DD_API_KEY_FILE') | ||
if len(api_key) > 0: | ||
logging.error('You must set API_KEY environment variable or include a DD_API_KEY_FILE to run the Datadog Agent container') | ||
exit(1) | ||
self.set_property('api_key', api_key) | ||
|
||
def set_generics(self): | ||
''' | ||
Looks for environment variables starting by 'DD_' and consider that the | ||
rest of the variable name is the name of the property to set | ||
''' | ||
for dd_var in environ: | ||
if dd_var.starts_with('DD_') and dd_var.upper() not in self.exclude_from_generic: | ||
if len(dd_var) > 0: | ||
self.set_property(dd_var[3:].lower(), environ[dd_var]) | ||
|
||
def set_property(self, property_name, property_value): | ||
''' | ||
Sets the given property to the given value in the configuration | ||
''' | ||
print('{}: {}'.format(property_name, property_value)) | ||
|
||
if __name__ == '__main__': | ||
cfg = ConfBuilder() | ||
cfg.build_datadog_conf() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters