-
Notifications
You must be signed in to change notification settings - Fork 814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[windows] new service and packaging #2417
Changes from 33 commits
8db437f
d04fbda
58d2751
1c449ff
f20306f
ef0a7c2
723c5df
4146514
22b5786
d6bc863
7506c39
fee135b
5840f8c
bdc971d
90cf16e
b24ef68
d0ca5f8
21fcfa0
c66ea26
befec72
df5c60b
f075565
34f551a
9c4fa7a
711f8bb
485b81c
bbe9119
f30225a
f003b19
bde7c15
ec26883
6f60943
4b1d0e2
369be52
bc82757
d0f4ca8
a1c2823
8d23019
c94b27b
e9e4eb2
5ad43e8
5fb54f2
b653adc
4f95aca
6f6cfb4
a8a2f91
35e8898
768519d
3a8eaed
dda342e
4ce664e
46fb184
63d6b21
5993b75
068c4b4
af04f0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,12 +157,15 @@ def _windows_confd_path(): | |
common_data = _windows_commondata_path() | ||
return _confd_path(os.path.join(common_data, 'Datadog')) | ||
|
||
def _windows_extra_checksd_path(): | ||
common_data = _windows_commondata_path() | ||
return _checksd_path(os.path.join(common_data, 'Datadog')) | ||
|
||
def _windows_checksd_path(): | ||
if hasattr(sys, 'frozen'): | ||
# we're frozen - from py2exe | ||
prog_path = os.path.dirname(sys.executable) | ||
return _checksd_path(os.path.join(prog_path, '..')) | ||
return _checksd_path(os.path.normpath(os.path.join(prog_path, '..', 'agent'))) | ||
else: | ||
cur_path = os.path.dirname(__file__) | ||
return _checksd_path(cur_path) | ||
|
@@ -325,7 +328,7 @@ def clean_dd_url(url): | |
return url[:-1] if url.endswith('/') else url | ||
|
||
|
||
def get_config(parse_args=True, cfg_path=None, options=None): | ||
def get_config(parse_args=True, cfg_path=None, options=None, can_write_conf=False): | ||
if parse_args: | ||
options, _ = get_parsed_args() | ||
|
||
|
@@ -349,6 +352,8 @@ def get_config(parse_args=True, cfg_path=None, options=None): | |
|
||
if Platform.is_mac(): | ||
agentConfig['additional_checksd'] = '/opt/datadog-agent/etc/checks.d' | ||
elif Platform.is_windows(): | ||
agentConfig['additional_checksd'] = _windows_extra_checksd_path() | ||
|
||
# Config handling | ||
try: | ||
|
@@ -438,10 +443,6 @@ def get_config(parse_args=True, cfg_path=None, options=None): | |
# the linux directory is set by default | ||
if config.has_option('Main', 'additional_checksd'): | ||
agentConfig['additional_checksd'] = config.get('Main', 'additional_checksd') | ||
elif get_os() == 'windows': | ||
# default windows location | ||
common_path = _windows_commondata_path() | ||
agentConfig['additional_checksd'] = os.path.join(common_path, 'Datadog', 'checks.d') | ||
|
||
if config.has_option('Main', 'use_dogstatsd'): | ||
agentConfig['use_dogstatsd'] = config.get('Main', 'use_dogstatsd').lower() in ("yes", "true") | ||
|
@@ -620,9 +621,62 @@ def get_config(parse_args=True, cfg_path=None, options=None): | |
else: | ||
agentConfig['ssl_certificate'] = agentConfig['ca_certs'] | ||
|
||
if agentConfig['api_key'] == 'APIKEYHERE' and Platform.is_windows(): | ||
log.debug('Querying registry to get missing config options') | ||
registry_conf = get_registry_conf(agentConfig) | ||
agentConfig.update(registry_conf) | ||
# If it's the collector, conf is updated | ||
if can_write_conf and registry_conf: | ||
log.debug('Updating conf file options: %s', registry_conf.keys()) | ||
update_conf_file(registry_conf, config_path) | ||
|
||
return agentConfig | ||
|
||
|
||
def get_registry_conf(agentConfig): | ||
registry_conf = {} | ||
try: | ||
import _winreg | ||
with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, | ||
"SOFTWARE\\Datadog\\Datadog Agent") as reg_key: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's create a constant for the registry key |
||
for attribute in ['api_key', 'tags', 'hostname']: | ||
option = _winreg.QueryValueEx(reg_key, attribute)[0] | ||
if option != '': | ||
registry_conf[attribute] = option | ||
except (ImportError, WindowsError) as e: | ||
log.error('Unable to get config keys from Registry: %s', e) | ||
|
||
return registry_conf | ||
|
||
|
||
def update_conf_file(registry_conf, config_path): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like def update_conf_file(registry_conf, config_path):
new_config = ""
with open(config_path, 'r') as f:
for line in f:
for k, v in registry_conf.iteritems():
if k + ":" in line:
line = '{}: {}{}'.format(k, v,os.linesep)
new_config += line
with open(config_path, 'w') as f:
f.write(new_config) is easier to read. That being said, we should move that somewhere else. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conf file uses |
||
buffer = '' | ||
try: | ||
with open(config_path, 'r') as config_file: | ||
for line in config_file: | ||
matched = False | ||
for attribute in registry_conf: | ||
# Is it the line configuring the attribute ? | ||
if conf_match(line, attribute): | ||
buffer += '{}: {}\n'.format(attribute, registry_conf[attribute]) | ||
matched = True | ||
if not matched: | ||
buffer += line | ||
except Exception as e: | ||
log.error('An exception happened while updating the conf file. No change has been done.' | ||
' Error: %s', e) | ||
else: | ||
with open(config_path, 'w') as config_file: | ||
config_file.write(buffer) | ||
|
||
|
||
def conf_match(line, attribute): | ||
attribute += ':' | ||
return (line.startswith(attribute) or | ||
line.startswith('#' + attribute) or | ||
line.startswith('# ' + attribute)) | ||
|
||
|
||
def get_system_stats(proc_path=None): | ||
systemStats = { | ||
'machine': platform.machine(), | ||
|
@@ -1104,10 +1158,13 @@ def get_logging_config(cfg_path=None): | |
'syslog_port': None, | ||
} | ||
if system_os == 'windows': | ||
logging_config['windows_collector_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'collector.log') | ||
logging_config['windows_forwarder_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'forwarder.log') | ||
logging_config['windows_dogstatsd_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'dogstatsd.log') | ||
logging_config['collector_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'collector.log') | ||
logging_config['forwarder_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'forwarder.log') | ||
logging_config['dogstatsd_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'dogstatsd.log') | ||
logging_config['jmxfetch_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'jmxfetch.log') | ||
logging_config['supervisor_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'supervisor.log') | ||
logging_config['service_log_file'] = os.path.join(_windows_commondata_path(), 'Datadog', 'logs', 'service.log') | ||
logging_config['log_to_syslog'] = False | ||
else: | ||
logging_config['collector_log_file'] = '/var/log/datadog/collector.log' | ||
logging_config['forwarder_log_file'] = '/var/log/datadog/forwarder.log' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this new arg ?