forked from fidlej/sd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
227 lines (184 loc) · 8.5 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import ConfigParser
import os
import logging
import logging.config
import platform
import string
import subprocess
import sys
from optparse import OptionParser, Values
from cStringIO import StringIO
# CONSTANTS
DATADOG_CONF = "datadog.conf"
DEFAULT_CHECK_FREQUENCY = 15 # seconds
def get_parsed_args():
parser = OptionParser()
parser.add_option('-d', '--dd_url', action='store', default=None,
dest='dd_url')
parser.add_option('-c', '--clean', action='store_true', default=False,
dest='clean')
parser.add_option('-u', '--use-local-forwarder', action='store_true',
default=False,dest='use_forwarder')
try:
options, args = parser.parse_args()
except SystemExit:
options, args = Values({'dd_url': None, 'clean': False, 'use_forwarder':False}), [] # Ignore parse errors
return options, args
def get_version():
return "3.0.4"
def skip_leading_wsp(f):
"Works on a file, returns a file-like object"
return StringIO("\n".join(map(string.strip, f.readlines())))
def initialize_logging(config_path):
try:
logging.config.fileConfig(config_path)
except Exception, e:
sys.stderr.write("Couldn't initialize logging: %s" % str(e))
def get_config_path(cfg_path=None):
# Find the right config file
path = os.path.realpath(__file__)
path = os.path.dirname(path)
config_path = None
if cfg_path is not None and os.path.exists(cfg_path):
config_path = cfg_path
elif os.path.exists(os.path.join('/etc/dd-agent', DATADOG_CONF)):
config_path = os.path.join('/etc/dd-agent', DATADOG_CONF)
elif os.path.exists(os.path.join(path, DATADOG_CONF)):
config_path = os.path.join(path, DATADOG_CONF)
else:
sys.stderr.write("Please supply a configuration file at /etc/dd-agent/%s or in the directory where the agent is currently deployed.\n" % DATADOG_CONF)
sys.exit(3)
return config_path
def get_config(parse_args = True, cfg_path=None, init_logging=False):
if parse_args:
options, args = get_parsed_args()
else:
options = None
args = None
# General config
agentConfig = {
'check_freq': DEFAULT_CHECK_FREQUENCY,
'debug_mode': False,
'dogstatsd_interval': 10,
'dogstatsd_port': 8125,
'dogstatsd_target': 'http://localhost:17123',
'graphite_listen_port': None,
'hostname': None,
'listen_port': None,
'tags': None,
'use_ec2_instance_id': False,
'version': get_version(),
'watchdog': True,
}
# Config handling
try:
# Find the right config file
path = os.path.realpath(__file__)
path = os.path.dirname(path)
config_path = get_config_path(cfg_path)
config = ConfigParser.ConfigParser()
config.readfp(skip_leading_wsp(open(config_path)))
if init_logging:
initialize_logging(config_path)
# bulk import
for option in config.options('Main'):
agentConfig[option] = config.get('Main', option)
#
# Core config
#
# Where to send the data
if options is not None and options.use_forwarder:
listen_port = 17123
if config.has_option('Main','listen_port'):
listen_port = config.get('Main', 'listen_port')
agentConfig['dd_url'] = "http://localhost:" + str(listen_port)
elif options is not None and options.dd_url:
agentConfig['dd_url'] = options.dd_url
else:
agentConfig['dd_url'] = config.get('Main', 'dd_url')
if agentConfig['dd_url'].endswith('/'):
agentConfig['dd_url'] = agentConfig['dd_url'][:-1]
# Debug mode
agentConfig['debug_mode'] = config.get('Main', 'debug_mode').lower() in ("yes", "true")
if config.has_option('Main', 'use_ec2_instance_id'):
use_ec2_instance_id = config.get('Main', 'use_ec2_instance_id')
# translate yes into True, the rest into False
agentConfig['use_ec2_instance_id'] = (use_ec2_instance_id.lower() == 'yes')
if config.has_option('Main', 'check_freq'):
try:
agentConfig['check_freq'] = int(config.get('Main', 'check_freq'))
except:
pass
# Disable Watchdog (optionally)
if config.has_option('Main', 'watchdog'):
if config.get('Main', 'watchdog').lower() in ('no', 'false'):
agentConfig['watchdog'] = False
# Optional graphite listener
if config.has_option('Main','graphite_listen_port'):
agentConfig['graphite_listen_port'] = int(config.get('Main','graphite_listen_port'))
# Optional config
# FIXME not the prettiest code ever...
if config.has_option('Main', 'use_mount'):
agentConfig['use_mount'] = config.get('Main', 'use_mount').lower() in ("yes", "true", "1")
if config.has_option('datadog', 'ddforwarder_log'):
agentConfig['has_datadog'] = True
# Dogstream config
if config.has_option("Main", "dogstream_log"):
# Older version, single log support
log_path = config.get("Main", "dogstream_log")
if config.has_option("Main", "dogstream_line_parser"):
agentConfig["dogstreams"] = ':'.join([log_path, config.get("Main", "dogstream_line_parser")])
else:
agentConfig["dogstreams"] = log_path
elif config.has_option("Main", "dogstreams"):
agentConfig["dogstreams"] = config.get("Main", "dogstreams")
if config.has_option("Main", "nagios_perf_cfg"):
agentConfig["nagios_perf_cfg"] = config.get("Main", "nagios_perf_cfg")
except ConfigParser.NoSectionError, e:
sys.stderr.write('Config file not found or incorrectly formatted.\n')
sys.exit(2)
except ConfigParser.ParsingError, e:
sys.stderr.write('Config file not found or incorrectly formatted.\n')
sys.exit(2)
except ConfigParser.NoOptionError, e:
sys.stderr.write('There are some items missing from your config file, but nothing fatal [%s]' % e)
if 'apache_status_url' in agentConfig and agentConfig['apache_status_url'] == None:
sys.stderr.write('You must provide a config value for apache_status_url. If you do not wish to use Apache monitoring, leave it as its default value - http://www.example.com/server-status/?auto.\n')
sys.exit(2)
if 'nginx_status_url' in agentConfig and agentConfig['nginx_status_url'] == None:
sys.stderr.write('You must provide a config value for nginx_status_url. If you do not wish to use Nginx monitoring, leave it as its default value - http://www.example.com/nginx_status.\n')
sys.exit(2)
if 'mysql_server' in agentConfig and agentConfig['mysql_server'] != '' and 'mysql_user' in agentConfig and agentConfig['mysql_user'] != '' and 'mysql_pass' in agentConfig:
try:
import MySQLdb
except ImportError:
sys.stderr.write('You have configured MySQL for monitoring, but the MySQLdb module is not installed. For more info, see: http://help.datadoghq.com.\n')
sys.exit(2)
if 'mongodb_server' in agentConfig and agentConfig['mongodb_server'] != '':
try:
import pymongo
except ImportError:
sys.stderr.write('You have configured MongoDB for monitoring, but the pymongo module is not installed.\n')
sys.exit(2)
return agentConfig
def get_system_stats():
systemStats = {
'machine': platform.machine(),
'platform': sys.platform,
'processor': platform.processor(),
'pythonV': platform.python_version()
}
if sys.platform == 'linux2':
grep = subprocess.Popen(['grep', 'model name', '/proc/cpuinfo'], stdout=subprocess.PIPE, close_fds=True)
wc = subprocess.Popen(['wc', '-l'], stdin=grep.stdout, stdout=subprocess.PIPE, close_fds=True)
systemStats['cpuCores'] = int(wc.communicate()[0])
if sys.platform == 'darwin':
systemStats['cpuCores'] = int(subprocess.Popen(['sysctl', 'hw.ncpu'], stdout=subprocess.PIPE, close_fds=True).communicate()[0].split(': ')[1])
if sys.platform == 'linux2':
systemStats['nixV'] = platform.dist()
elif sys.platform == 'darwin':
systemStats['macV'] = platform.mac_ver()
elif sys.platform.find('freebsd') != -1:
version = platform.uname()[2]
systemStats['fbsdV'] = ('freebsd', version, '') # no codename for FreeBSD
return systemStats