forked from DataDog/dd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
296 lines (245 loc) · 9.74 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
# stdlib
import logging
import modules
import multiprocessing
from optparse import Values
import servicemanager
import sys
import threading
import time
import tornado.httpclient
from win32.common import handle_exe_click
import win32event
import win32evtlogutil
import win32service
import win32serviceutil
# DD
from checks.collector import Collector
from config import (
get_config,
get_confd_path,
get_system_stats,
get_win32service_file,
load_check_directory,
set_win32_cert_path,
PathNotFound,
)
import dogstatsd
from ddagent import Application
from emitter import http_emitter
from jmxfetch import JMXFetch
from util import get_hostname, get_os
log = logging.getLogger(__name__)
SERVICE_SLEEP_INTERVAL = 1
MAX_FAILED_HEARTBEATS = 8 # runs of collector
class AgentSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "DatadogAgent"
_svc_display_name_ = "Datadog Agent"
_svc_description_ = "Sends metrics to Datadog"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
config = get_config(parse_args=False)
# Setup the correct options so the agent will use the forwarder
opts, args = Values({
'autorestart': False,
'dd_url': None,
'use_forwarder': True,
'disabled_dd': False
}), []
agentConfig = get_config(parse_args=False, options=opts)
self.hostname = get_hostname(agentConfig)
# Watchdog for Windows
self._collector_heartbeat, self._collector_send_heartbeat = multiprocessing.Pipe(False)
self._collector_failed_heartbeats = 0
self._max_failed_heartbeats = \
MAX_FAILED_HEARTBEATS * agentConfig['check_freq'] / SERVICE_SLEEP_INTERVAL
# Watch JMXFetch restarts
self._MAX_JMXFETCH_RESTARTS = 3
self._count_jmxfetch_restarts = 0
# Keep a list of running processes so we can start/end as needed.
# Processes will start started in order and stopped in reverse order.
self.procs = {
'forwarder': ProcessWatchDog("forwarder", DDForwarder(config, self.hostname)),
'collector': ProcessWatchDog("collector", DDAgent(agentConfig, self.hostname,
heartbeat=self._collector_send_heartbeat)),
'dogstatsd': ProcessWatchDog("dogstatsd", DogstatsdProcess(config, self.hostname)),
'jmxfetch': ProcessWatchDog("jmxfetch", JMXFetchProcess(config, self.hostname), 3),
}
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
# Stop all services.
self.running = False
for proc in self.procs.values():
proc.terminate()
def SvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.start_ts = time.time()
# Start all services.
for proc in self.procs.values():
proc.start()
# Loop to keep the service running since all DD services are
# running in separate processes
self.running = True
while self.running:
# Restart any processes that might have died.
for name, proc in self.procs.iteritems():
if not proc.is_alive() and proc.is_enabled():
servicemanager.LogInfoMsg("%s has died. Restarting..." % name)
proc.restart()
self._check_collector_blocked()
time.sleep(SERVICE_SLEEP_INTERVAL)
def _check_collector_blocked(self):
if self._collector_heartbeat.poll():
while self._collector_heartbeat.poll():
self._collector_heartbeat.recv()
self._collector_failed_heartbeats = 0
else:
self._collector_failed_heartbeats += 1
if self._collector_failed_heartbeats > self._max_failed_heartbeats:
servicemanager.LogInfoMsg(
"%s was unresponsive for too long. Restarting..." % 'collector')
self.procs['collector'].restart()
self._collector_failed_heartbeats = 0
class ProcessWatchDog(object):
"""
Monitor the attached process.
Restarts when it exits until the limit set is reached.
"""
def __init__(self, name, process, max_restarts=5):
self._name = name
self._process = process
self._count_restarts = 0
self._MAX_RESTARTS = max_restarts
def start(self):
return self._process.start()
def terminate(self):
return self._process.terminate()
def is_alive(self):
return self._process.is_alive()
def is_enabled(self):
return self._process.is_enabled
def restart(self):
self._count_restarts += 1
if self._count_restarts >= self._MAX_RESTARTS:
servicemanager.LogInfoMsg(
"%s reached the limit of restarts. Not restarting..." % self._name)
self._process.is_enabled = False
return
# Make a new proc instances because multiprocessing
# won't let you call .start() twice on the same instance.
if self._process.is_alive():
self._process.terminate()
self._process = self._process.__class__(self._process.config, self._process.hostname)
self._process.start()
class DDAgent(multiprocessing.Process):
def __init__(self, agentConfig, hostname, heartbeat=None):
multiprocessing.Process.__init__(self, name='ddagent')
self.config = agentConfig
self.hostname = hostname
self._heartbeat = heartbeat
# FIXME: `running` flag should be handled by the service
self.running = True
self.is_enabled = True
def run(self):
from config import initialize_logging; initialize_logging('windows_collector')
log.debug("Windows Service - Starting collector")
emitters = self.get_emitters()
systemStats = get_system_stats()
self.collector = Collector(self.config, emitters, systemStats, self.hostname)
# Load the checks.d checks
checksd = load_check_directory(self.config, self.hostname)
# Main agent loop will run until interrupted
while self.running:
if self._heartbeat:
self._heartbeat.send(0)
self.collector.run(checksd=checksd)
time.sleep(self.config['check_freq'])
def stop(self):
log.debug("Windows Service - Stopping collector")
self.collector.stop()
if JMXFetch.is_running():
JMXFetch.stop()
self.running = False
def get_emitters(self):
emitters = [http_emitter]
custom = [s.strip() for s in
self.config.get('custom_emitters', '').split(',')]
for emitter_spec in custom:
if not emitter_spec:
continue
emitters.append(modules.load(emitter_spec, 'emitter'))
return emitters
class DDForwarder(multiprocessing.Process):
def __init__(self, agentConfig, hostname):
multiprocessing.Process.__init__(self, name='ddforwarder')
self.config = agentConfig
self.is_enabled = True
self.hostname = hostname
def run(self):
from config import initialize_logging; initialize_logging('windows_forwarder')
log.debug("Windows Service - Starting forwarder")
set_win32_cert_path()
port = self.config.get('listen_port', 17123)
if port is None:
port = 17123
else:
port = int(port)
app_config = get_config(parse_args=False)
self.forwarder = Application(port, app_config, watchdog=False)
try:
self.forwarder.run()
except Exception:
log.exception("Uncaught exception in the forwarder")
def stop(self):
log.debug("Windows Service - Stopping forwarder")
self.forwarder.stop()
class DogstatsdProcess(multiprocessing.Process):
def __init__(self, agentConfig, hostname):
multiprocessing.Process.__init__(self, name='dogstatsd')
self.config = agentConfig
self.is_enabled = self.config.get('use_dogstatsd', True)
self.hostname = hostname
def run(self):
from config import initialize_logging; initialize_logging('windows_dogstatsd')
if self.is_enabled:
log.debug("Windows Service - Starting Dogstatsd server")
self.reporter, self.server, _ = dogstatsd.init(use_forwarder=True)
self.reporter.start()
self.server.start()
else:
log.info("Dogstatsd is not enabled, not starting it.")
def stop(self):
if self.is_enabled:
log.debug("Windows Service - Stopping Dogstatsd server")
self.server.stop()
self.reporter.stop()
self.reporter.join()
class JMXFetchProcess(multiprocessing.Process):
def __init__(self, agentConfig, hostname):
multiprocessing.Process.__init__(self, name='jmxfetch')
self.config = agentConfig
self.hostname = hostname
try:
osname = get_os()
confd_path = get_confd_path(osname)
self.jmx_daemon = JMXFetch(confd_path, agentConfig)
self.jmx_daemon.configure()
self.is_enabled = self.jmx_daemon.should_run()
except PathNotFound:
self.is_enabled = False
def run(self):
if self.is_enabled:
self.jmx_daemon.run()
def stop(self):
pass
if __name__ == '__main__':
multiprocessing.freeze_support()
if len(sys.argv) == 1:
handle_exe_click(AgentSvc._svc_name_)
else:
win32serviceutil.HandleCommandLine(AgentSvc)