forked from fidlej/sd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddagent.py
executable file
·275 lines (206 loc) · 7.96 KB
/
ddagent.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
#!/usr/bin/python
'''
Datadog
www.datadoghq.com
----
Make sense of your IT Data
Licensed under Simplified BSD License (see LICENSE)
(C) Boxed Ice 2010 all rights reserved
(C) Datadog, Inc. 2010 all rights reserved
'''
# Standard imports
import logging
import os
import sys
from subprocess import Popen
from hashlib import md5
from datetime import datetime, timedelta
# Tornado
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.escape import json_decode
from tornado.options import define, parse_command_line, options
# agent import
from util import Watchdog
from emitter import http_emitter, format_body
from config import get_config
from checks.common import getUuid
from checks import gethostname
from transaction import Transaction, TransactionManager
TRANSACTION_FLUSH_INTERVAL = 5000 # Every 5 seconds
WATCHDOG_INTERVAL_MULTIPLIER = 10 # 10x flush interval
# Maximum delay before replaying a transaction
MAX_WAIT_FOR_REPLAY = timedelta(seconds=90)
# Maximum queue size in bytes (when this is reached, old messages are dropped)
MAX_QUEUE_SIZE = 30 * 1024 * 1024 # 30MB
THROTTLING_DELAY = timedelta(microseconds=1000000/2) # 2 msg/second
class MetricTransaction(Transaction):
_application = None
_trManager = None
@classmethod
def set_application(cls, app):
cls._application = app
@classmethod
def set_tr_manager(cls, manager):
cls._trManager = manager
@classmethod
def get_tr_manager(cls):
return cls._trManager
def __init__(self, data):
self._data = data
# Call after data has been set (size is computed in Transaction's init)
Transaction.__init__(self)
# Insert the transaction in the Manager
self._trManager.append(self)
logging.debug("Created transaction %d" % self.get_id())
self._trManager.flush()
def __sizeof__(self):
return sys.getsizeof(self._data)
def get_data(self):
try:
return format_body(self._data, logging)
except:
logging.exception('http_emitter failed')
def get_url(self):
return self._application._agentConfig['dd_url'] + '/intake/'
def flush(self):
# Send Transaction to the intake
req = tornado.httpclient.HTTPRequest(self.get_url(),
method = "POST", body = self.get_data() )
http = tornado.httpclient.AsyncHTTPClient()
logging.debug("Sending transaction %d to datadog" % self.get_id())
http.fetch(req, callback=lambda(x): self.on_response(x))
def on_response(self, response):
if response.error:
logging.error("Response: %s" % response.error)
self._trManager.tr_error(self)
else:
self._trManager.tr_success(self)
self._trManager.flush_next()
class APIMetricTransaction(MetricTransaction):
def get_url(self):
config = self._application._agentConfig
api_key = config['api_key']
base_url = config['dd_url']
return base_url + '/api/v1/series/?api_key=' + api_key
def get_data(self):
return self._data
class StatusHandler(tornado.web.RequestHandler):
def get(self):
threshold = int(self.get_argument('threshold', -1))
m = MetricTransaction.get_tr_manager()
self.write("<table><tr><td>Id</td><td>Size</td><td>Error count</td><td>Next flush</td></tr>")
transactions = m.get_transactions()
for tr in transactions:
self.write("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" %
(tr.get_id(), tr.get_size(), tr.get_error_count(), tr.get_next_flush()))
self.write("</table>")
if threshold >= 0:
if len(transactions) > threshold:
self.set_status(503)
class AgentInputHandler(tornado.web.RequestHandler):
HASH = "hash"
PAYLOAD = "payload"
@staticmethod
def parse_message(message, msg_hash):
c_hash = md5(message).hexdigest()
if c_hash != msg_hash:
logging.error("Malformed message: %s != %s" % (c_hash, msg_hash))
return None
return json_decode(message)
def post(self):
"""Read the message and forward it to the intake"""
# read message
msg = AgentInputHandler.parse_message(self.get_argument(self.PAYLOAD),
self.get_argument(self.HASH))
if msg is not None:
# Setup a transaction for this message
tr = MetricTransaction(msg)
else:
raise tornado.web.HTTPError(500)
self.write("Transaction: %s" % tr.get_id())
class ApiInputHandler(tornado.web.RequestHandler):
def post(self):
"""Read the message and forward it to the intake"""
# read message
msg = self.request.body
if msg is not None:
# Setup a transaction for this message
tr = APIMetricTransaction(msg)
else:
raise tornado.web.HTTPError(500)
class Application(tornado.web.Application):
def __init__(self, port, agentConfig):
self._port = port
self._agentConfig = agentConfig
self._metrics = {}
self._watchdog = Watchdog(TRANSACTION_FLUSH_INTERVAL * WATCHDOG_INTERVAL_MULTIPLIER)
MetricTransaction.set_application(self)
self._tr_manager = TransactionManager(MAX_WAIT_FOR_REPLAY,
MAX_QUEUE_SIZE, THROTTLING_DELAY)
MetricTransaction.set_tr_manager(self._tr_manager)
def appendMetric(self, prefix, name, host, device, ts, value):
if self._metrics.has_key(prefix):
metrics = self._metrics[prefix]
else:
metrics = {}
self._metrics[prefix] = metrics
if metrics.has_key(name):
metrics[name].append([host, device, ts, value])
else:
metrics[name] = [[host, device, ts, value]]
def _postMetrics(self):
if len(self._metrics) > 0:
self._metrics['uuid'] = getUuid()
self._metrics['internalHostname'] = gethostname(self._agentConfig)
self._metrics['apiKey'] = self._agentConfig['api_key']
MetricTransaction(self._metrics)
self._metrics = {}
def run(self):
handlers = [
(r"/intake/?", AgentInputHandler),
(r"/api/v1/series/?", ApiInputHandler),
(r"/status/?", StatusHandler),
]
settings = dict(
cookie_secret="12oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
xsrf_cookies=False,
debug=True,
)
tornado.web.Application.__init__(self, handlers, **settings)
http_server = tornado.httpserver.HTTPServer(self)
http_server.listen(self._port)
logging.info("Listening on port %s" % self._port)
# Register callbacks
mloop = tornado.ioloop.IOLoop.instance()
def flush_trs():
self._watchdog.reset()
self._postMetrics()
self._tr_manager.flush()
tr_sched = tornado.ioloop.PeriodicCallback(flush_trs, TRANSACTION_FLUSH_INTERVAL, io_loop = mloop)
# Register optional Graphite listener
gport = self._agentConfig.get("graphite_listen_port", None)
if gport is not None:
logging.info("Starting graphite listener on port %s" % gport)
from graphite import GraphiteServer
gs = GraphiteServer(self, gethostname(self._agentConfig), io_loop=mloop)
gs.listen(gport)
# Start everything
self._watchdog.reset()
tr_sched.start()
mloop.start()
def main():
define("pycurl", default=1, help="Use pycurl")
parse_command_line()
if options.pycurl == 0 or options.pycurl == "0":
os.environ['USE_SIMPLE_HTTPCLIENT'] = '1'
import tornado.httpclient
agentConfig = get_config(parse_args = False)
port = agentConfig.get('listen_port', None)
if port is None:
port = 17123
app = Application(port, agentConfig)
app.run()
if __name__ == "__main__":
main()