-
Notifications
You must be signed in to change notification settings - Fork 20
/
agent.py
executable file
·393 lines (318 loc) · 12.7 KB
/
agent.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/opt/datadog-agent/embedded/bin/python
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2018 Datadog, Inc.
import os
import signal
import sys
import time
import logging
from optparse import OptionParser
from threading import Thread, Event
import requests
import requests.exceptions
from jinja2 import Environment, FileSystemLoader
from config import config
from config.providers import FileConfigProvider
from config.default import DEFAULT_PATH
from utils.logs import initialize_logging
from utils.hostname import HostnameException, get_hostname
from utils.daemon import Daemon
from utils.signals import SignalHandler
from utils.pidfile import PidFile
from utils.network import get_proxy, get_site_url
from utils.flare import Flare
from metadata import get_metadata
from collector import Collector
from aggregator import MetricsAggregator
from serialize import Serializer
from forwarder import Forwarder
from api import APIServer
from dogstatsd.helpers import (
init_dogstatsd,
DogstatsdRunner,
)
# Globals
AGENT_VERSION = '1.1.6'
PID_NAME = 'datadog-unix-agent'
log = logging.getLogger('agent')
class AgentRunner(Thread):
def __init__(self, collector, serializer, config):
super(AgentRunner, self).__init__()
self._collector = collector
self._serializer = serializer
self._config = config
self._event = Event()
self._meta_ts = None
def collection(self):
while not self._event.is_set():
try:
current_ts = time.monotonic()
if self._meta_ts is None or (current_ts - self._meta_ts) >= self._config.get('host_metadata_interval'):
metadata = get_metadata(get_hostname(), AGENT_VERSION, start_event=(self._meta_ts is None))
self._serializer.submit_metadata(metadata)
self._meta_ts = current_ts
self._collector.run_checks()
self._serializer.serialize_and_push()
except Exception:
log.exception("Unexpected error in last collection run")
time.sleep(self._config.get('min_collection_interval'))
def stop(self):
log.info('Stopping Agent Runner...')
self._event.set()
def run(self):
log.info('Starting Agent Runner...')
self.collection()
def init_config(do_log=True):
# init default search path
config.add_search_path("/etc/datadog-agent")
config.add_search_path(os.path.join(DEFAULT_PATH, "etc/datadog-agent"))
config.add_search_path("./etc/datadog-agent")
config.add_search_path(".")
try:
config.load()
except Exception:
if do_log:
initialize_logging('agent')
raise
# init log
if do_log:
initialize_logging('agent')
# add file provider
file_provider = FileConfigProvider()
file_provider.add_place(os.path.join(os.path.dirname(config.get_loaded_config()), 'conf.d'))
file_provider.add_place(os.path.join(config.get('conf_path'), 'conf.d'))
file_provider.add_place(config.get('additional_checksd'))
config.add_provider('file', file_provider)
# FIXME: perhaps do this elsewhere
config.collect_check_configs()
class Agent(Daemon):
# dictionary k:v - command:log
COMMANDS = {
'start': True,
'stop': True,
'restart': False,
'status': False,
'flare': False,
}
STATUS_TIMEOUT = 5
@classmethod
def usage(cls):
return "Usage: %s %s\n" % (sys.argv[0], "|".join(cls.COMMANDS.keys()))
@classmethod
def status(cls, config, to_screen=True):
status = {}
rendered = None
api_addr = config['api']['bind_host']
api_port = config['api']['port']
target = 'http://{host}:{port}/status'.format(host=api_addr, port=api_port)
try:
r = requests.get(target, timeout=cls.STATUS_TIMEOUT)
r.raise_for_status()
status = r.json()
except requests.exceptions.HTTPError as e:
log.error("HTTP error collecting agent status: %s", e)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
log.error("Problem connecting or connection timed out, is the agent up? Error: %s", e)
except ValueError as e:
log.error("There was a problem unmarshaling JSON response: %s", e)
if status:
here = os.path.dirname(os.path.realpath(__file__))
templates = os.path.join(here, 'templates')
template_env = Environment(loader=FileSystemLoader(templates))
template = template_env.get_template('status.jinja')
rendered = template.render(version=AGENT_VERSION, status=status)
if to_screen:
print(rendered)
return rendered
@classmethod
def flare(cls, config, case_id):
email = input('Please enter your contact email address: ').lower()
case_id = int(case_id) if case_id else None
myflare = Flare(version=AGENT_VERSION, case_id=case_id, email=email)
myflare.add_path(config.get('conf_path'))
myflare.add_path(config.get_loaded_config())
myflare.add_path(config.get('logging').get('agent_log_file'))
myflare.add_path(config.get('logging').get('dogstatsd_log_file'))
myflare.add_path(config.get('additional_checksd'))
flarepath = myflare.create_archive(status=cls.status(config, to_screen=False))
print('The flare is going to be uploaded to Datadog')
choice = input('Do you want to continue [Y/n]? ')
if choice.strip().lower() not in ['yes', 'y', '']:
print('Aborting (you can still use {0})'.format(flarepath))
sys.exit(0)
success, case_id = myflare.submit()
if success:
if case_id:
print('Your flare was uploaded successfully, this is your case id: {}'.format(case_id))
else:
print('Your flare was uploaded successfully, but a case id could not be retrieved.')
myflare.cleanup()
def run(self):
try:
hostname = get_hostname()
except HostnameException as e:
logging.critical("{} - You can define one in datadog.yaml or in your hosts file".format(e))
sys.exit(1)
logging.info("Starting the agent, hostname: %s", hostname)
# init Forwarder
logging.info("Starting the Forwarder")
api_key = config.get('api_key')
dd_url = config.get('dd_url')
if not dd_url:
logging.error('No Datadog URL configured - cannot continue')
sys.exit(1)
if not api_key:
logging.error('No API key configured - cannot continue')
sys.exit(1)
# get proxy settings
proxies = get_proxy()
logging.debug('Proxy configuration used: %s', proxies)
# get site url
forwarder = Forwarder(
api_key,
get_site_url(dd_url, site=config.get('site')),
proxies=proxies,
)
forwarder.start()
# agent aggregator
aggregator = MetricsAggregator(
hostname,
interval=config.get('aggregator_interval'),
expiry_seconds=(config.get('min_collection_interval')
+ config.get('aggregator_expiry_seconds')),
recent_point_threshold=config.get('recent_point_threshold'),
histogram_aggregates=config.get('histogram_aggregates'),
histogram_percentiles=config.get('histogram_percentiles'),
)
# serializer
serializer = Serializer(
aggregator,
forwarder,
)
# instantiate collector
collector = Collector(config, aggregator)
collector.load_check_classes()
collector.instantiate_checks()
# instantiate AgentRunner
runner = AgentRunner(collector, serializer, config)
# instantiate Dogstatsd
reporter = None
dsd_server = None
dsdconf = config['dogstatsd']
# 'enable' was a typo, kept for backwards compatibility
dsd_enable = dsdconf.get('enabled', False) or dsdconf.get('enable', False)
if dsd_enable:
reporter, dsd_server, _ = init_dogstatsd(config, forwarder=forwarder)
dsd = DogstatsdRunner(dsd_server)
# instantiate API
status = {
'agent': aggregator.stats,
'forwarder': forwarder.stats,
'collector': collector.status,
}
if dsd_server:
status['dogstatsd'] = dsd_server.aggregator.stats
api = APIServer(config, status=status)
handler = SignalHandler()
# components
handler.register('runner', runner)
handler.register('forwarder', forwarder)
handler.register('api', api)
if dsd_enable:
handler.register('reporter', reporter)
handler.register('dsd_server', dsd_server)
# signals
handler.handle(signal.SIGTERM)
handler.handle(signal.SIGINT)
# start signal handler
handler.start()
runner.start()
api.start()
if dsd_enable:
reporter.start()
dsd.start()
dsd.join()
logging.info("Dogstatsd server done...")
try:
dsd.raise_for_status()
except Exception as e:
log.error("There was a problem with the dogstatsd server: %s", e)
reporter.stop()
runner.join()
logging.info("Collector done...")
api.join()
logging.info("API done...")
handler.stop()
handler.join()
logging.info("Signal handler done...")
logging.info("Thank you for shopping at DataDog! Come back soon!")
sys.exit(0)
def main():
CRED = '\033[91m'
CEND = '\033[0m'
parser = OptionParser()
parser.add_option('-b', '--background', action='store_true', default=False,
dest='background', help='Run agent on the foreground')
parser.add_option('-l', '--force-logging', action='store_true', default=False,
dest='logging', help='force logging')
parser.add_option('-m', '--manual', action='store_true', default=False,
dest='manual', help='Apply action manually - advanced feature')
options, args = parser.parse_args()
if len(args) < 1:
sys.stderr.write(Agent.usage())
return 2
command = args[0]
if command not in Agent.COMMANDS:
sys.stderr.write(CRED + "Unknown command: {}\n".format(command) + CEND)
return 3
try:
do_log = options.logging or Agent.COMMANDS[command]
init_config(do_log=do_log)
except Exception as e:
logging.error(CRED + "Problem initializing configuration: {}".format(e) + CEND)
return 1
if (os.path.dirname(os.path.realpath(__file__)) != os.path.join(DEFAULT_PATH, 'agent')):
log.info("""You don't seem to be running a package installed agent (expected
at %s). You may need to specify sane locations for your configs,
logs, run path, etc. And remember to drop the configuration
file in one of the supported locations.""" % DEFAULT_PATH)
# if pid_dir below doesn't exist a temporary dir will be used
pid_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'run')
else:
pid_dir = config.get('run_path')
agent = Agent(PidFile(PID_NAME, pid_dir).get_path())
foreground = not options.background
manual = options.manual
if 'start' == command:
if manual:
logging.info('Start daemon')
agent.start(foreground=foreground)
else:
sys.stderr.write(CRED + 'Please use OS facilities to start the agent!\n' + CEND)
return 1
elif 'stop' == command:
logging.info('Stop daemon')
agent.stop()
elif 'restart' == command:
if manual:
logging.info('Restart daemon')
agent.restart()
else:
sys.stderr.write(CRED + 'Please use OS facilities to restart the agent!\n' + CEND)
return 1
elif 'status' == command:
agent.status(config)
elif 'flare' == command:
case_id = input('Do you have a support case id? Please enter it here (otherwise just hit enter): ').lower()
agent.flare(config, case_id)
if __name__ == "__main__":
try:
sys.exit(main())
except Exception:
try:
logging.exception("Uncaught error running the Agent")
except Exception:
pass
raise