forked from DataDog/dd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
612 lines (508 loc) · 20.1 KB
/
util.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# (C) Datadog, Inc. 2010-2016
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
# stdlib
from collections import deque
import logging
import os
import platform
import re
import signal
import socket
import sys
import time
import types
import urllib2
import uuid
# 3p
import simplejson as json
import yaml # noqa, let's guess, probably imported somewhere
from tornado import ioloop
try:
from yaml import CLoader as yLoader
from yaml import CDumper as yDumper
except ImportError:
# On source install C Extensions might have not been built
from yaml import Loader as yLoader # noqa, imported from here elsewhere
from yaml import Dumper as yDumper # noqa, imported from here elsewhere
# These classes are now in utils/, they are just here for compatibility reasons,
# if a user actually uses them in a custom check
# If you're this user, please use utils.pidfile or utils.platform instead
# FIXME: remove them at a point (6.x)
from utils.dockerutil import DockerUtil
from utils.pidfile import PidFile # noqa, see ^^^
from utils.platform import Platform
from utils.proxy import get_proxy
from utils.subprocess_output import get_subprocess_output
VALID_HOSTNAME_RFC_1123_PATTERN = re.compile(r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")
MAX_HOSTNAME_LEN = 255
COLON_NON_WIN_PATH = re.compile(':(?!\\\\)')
log = logging.getLogger(__name__)
NumericTypes = (float, int, long)
def plural(count):
if count == 1:
return ""
return "s"
def get_tornado_ioloop():
return ioloop.IOLoop.current()
def get_uuid():
# Generate a unique name that will stay constant between
# invocations, such as platform.node() + uuid.getnode()
# Use uuid5, which does not depend on the clock and is
# recommended over uuid3.
# This is important to be able to identify a server even if
# its drives have been wiped clean.
# Note that this is not foolproof but we can reconcile servers
# on the back-end if need be, based on mac addresses.
return uuid.uuid5(uuid.NAMESPACE_DNS, platform.node() + str(uuid.getnode())).hex
def get_os():
"Human-friendly OS name"
if sys.platform == 'darwin':
return 'mac'
elif sys.platform.find('freebsd') != -1:
return 'freebsd'
elif sys.platform.find('linux') != -1:
return 'linux'
elif sys.platform.find('win32') != -1:
return 'windows'
elif sys.platform.find('sunos') != -1:
return 'solaris'
else:
return sys.platform
def headers(agentConfig):
# Build the request headers
return {
'User-Agent': 'Datadog Agent/%s' % agentConfig['version'],
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/html, */*',
}
def windows_friendly_colon_split(config_string):
'''
Perform a split by ':' on the config_string
without splitting on the start of windows path
'''
if Platform.is_win32():
# will split on path/to/module.py:blabla but not on C:\\path
return COLON_NON_WIN_PATH.split(config_string)
else:
return config_string.split(':')
def cast_metric_val(val):
# ensure that the metric value is a numeric type
if not isinstance(val, NumericTypes):
# Try the int conversion first because want to preserve
# whether the value is an int or a float. If neither work,
# raise a ValueError to be handled elsewhere
for cast in [int, float]:
try:
val = cast(val)
return val
except ValueError:
continue
raise ValueError
return val
_IDS = {}
def get_next_id(name):
global _IDS
current_id = _IDS.get(name, 0)
current_id += 1
_IDS[name] = current_id
return current_id
def is_valid_hostname(hostname):
if hostname.lower() in set([
'localhost',
'localhost.localdomain',
'localhost6.localdomain6',
'ip6-localhost',
]):
log.warning("Hostname: %s is local" % hostname)
return False
if len(hostname) > MAX_HOSTNAME_LEN:
log.warning("Hostname: %s is too long (max length is %s characters)" % (hostname, MAX_HOSTNAME_LEN))
return False
if VALID_HOSTNAME_RFC_1123_PATTERN.match(hostname) is None:
log.warning("Hostname: %s is not complying with RFC 1123" % hostname)
return False
return True
def check_yaml(conf_path):
with open(conf_path) as f:
check_config = yaml.load(f.read(), Loader=yLoader)
assert 'init_config' in check_config, "No 'init_config' section found"
assert 'instances' in check_config, "No 'instances' section found"
valid_instances = True
if check_config['instances'] is None or not isinstance(check_config['instances'], list):
valid_instances = False
else:
for i in check_config['instances']:
if not isinstance(i, dict):
valid_instances = False
break
if not valid_instances:
raise Exception('You need to have at least one instance defined in the YAML file for this check')
else:
return check_config
def get_hostname(config=None):
"""
Get the canonical host name this agent should identify as. This is
the authoritative source of the host name for the agent.
Tries, in order:
* agent config (datadog.conf, "hostname:")
* 'hostname -f' (on unix)
* socket.gethostname()
"""
hostname = None
# first, try the config
if config is None:
from config import get_config
config = get_config(parse_args=True)
config_hostname = config.get('hostname')
if config_hostname and is_valid_hostname(config_hostname):
return config_hostname
# Try to get GCE instance name
if hostname is None:
gce_hostname = GCE.get_hostname(config)
if gce_hostname is not None:
if is_valid_hostname(gce_hostname):
return gce_hostname
# Try to get the docker hostname
docker_util = DockerUtil()
if hostname is None and docker_util.is_dockerized():
docker_hostname = docker_util.get_hostname()
if docker_hostname is not None and is_valid_hostname(docker_hostname):
hostname = docker_hostname
# then move on to os-specific detection
if hostname is None:
def _get_hostname_unix():
try:
# try fqdn
out, _, rtcode = get_subprocess_output(['/bin/hostname', '-f'], log)
if rtcode == 0:
return out.strip()
except Exception:
return None
os_name = get_os()
if os_name in ['mac', 'freebsd', 'linux', 'solaris']:
unix_hostname = _get_hostname_unix()
if unix_hostname and is_valid_hostname(unix_hostname):
hostname = unix_hostname
# if we have an ec2 default hostname, see if there's an instance-id available
if (Platform.is_ecs_instance()) or (hostname is not None and EC2.is_default(hostname)):
instanceid = EC2.get_instance_id(config)
if instanceid:
hostname = instanceid
# fall back on socket.gethostname(), socket.getfqdn() is too unreliable
if hostname is None:
try:
socket_hostname = socket.gethostname()
except socket.error:
socket_hostname = None
if socket_hostname and is_valid_hostname(socket_hostname):
hostname = socket_hostname
if hostname is None:
log.critical('Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file')
raise Exception('Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file')
else:
return hostname
class GCE(object):
URL = "http://169.254.169.254/computeMetadata/v1/?recursive=true"
TIMEOUT = 0.1 # second
SOURCE_TYPE_NAME = 'google cloud platform'
metadata = None
EXCLUDED_ATTRIBUTES = ["kube-env", "startup-script", "sshKeys", "user-data",
"cli-cert", "ipsec-cert", "ssl-cert"]
@staticmethod
def _get_metadata(agentConfig):
if GCE.metadata is not None:
return GCE.metadata
if not agentConfig['collect_instance_metadata']:
log.info("Instance metadata collection is disabled. Not collecting it.")
GCE.metadata = {}
return GCE.metadata
socket_to = None
try:
socket_to = socket.getdefaulttimeout()
socket.setdefaulttimeout(GCE.TIMEOUT)
except Exception:
pass
try:
opener = urllib2.build_opener()
opener.addheaders = [('X-Google-Metadata-Request','True')]
GCE.metadata = json.loads(opener.open(GCE.URL).read().strip())
except Exception:
GCE.metadata = {}
try:
if socket_to is None:
socket_to = 3
socket.setdefaulttimeout(socket_to)
except Exception:
pass
return GCE.metadata
@staticmethod
def get_tags(agentConfig):
if not agentConfig['collect_instance_metadata']:
return None
try:
host_metadata = GCE._get_metadata(agentConfig)
tags = []
for key, value in host_metadata['instance'].get('attributes', {}).iteritems():
if key in GCE.EXCLUDED_ATTRIBUTES:
continue
tags.append("%s:%s" % (key, value))
tags.extend(host_metadata['instance'].get('tags', []))
tags.append('zone:%s' % host_metadata['instance']['zone'].split('/')[-1])
tags.append('instance-type:%s' % host_metadata['instance']['machineType'].split('/')[-1])
tags.append('internal-hostname:%s' % host_metadata['instance']['hostname'])
tags.append('instance-id:%s' % host_metadata['instance']['id'])
tags.append('project:%s' % host_metadata['project']['projectId'])
tags.append('numeric_project_id:%s' % host_metadata['project']['numericProjectId'])
GCE.metadata['hostname'] = host_metadata['instance']['hostname'].split('.')[0]
return tags
except Exception:
return None
@staticmethod
def get_hostname(agentConfig):
try:
host_metadata = GCE._get_metadata(agentConfig)
hostname = host_metadata['instance']['hostname']
if agentConfig.get('gce_updated_hostname'):
return hostname
else:
return hostname.split('.')[0]
except Exception:
return None
@staticmethod
def get_host_aliases(agentConfig):
try:
host_metadata = GCE._get_metadata(agentConfig)
project_id = host_metadata['project']['projectId']
instance_name = host_metadata['instance']['hostname'].split('.')[0]
return ['%s.%s' % (instance_name, project_id)]
except Exception:
return None
class EC2(object):
"""Retrieve EC2 metadata
"""
EC2_METADATA_HOST = "http://169.254.169.254"
METADATA_URL_BASE = EC2_METADATA_HOST + "/latest/meta-data"
INSTANCE_IDENTITY_URL = EC2_METADATA_HOST + "/latest/dynamic/instance-identity/document"
TIMEOUT = 0.1 # second
DEFAULT_PREFIXES = [u'ip-', u'domu']
metadata = {}
class NoIAMRole(Exception):
"""
Instance has no associated IAM role.
"""
pass
@staticmethod
def is_default(hostname):
hostname = hostname.lower()
for prefix in EC2.DEFAULT_PREFIXES:
if hostname.startswith(prefix):
return True
return False
@staticmethod
def get_iam_role():
"""
Retrieve instance's IAM role.
Raise `NoIAMRole` when unavailable.
"""
try:
return urllib2.urlopen(EC2.METADATA_URL_BASE + "/iam/security-credentials/").read().strip()
except urllib2.HTTPError as err:
if err.code == 404:
raise EC2.NoIAMRole()
raise
@staticmethod
def get_tags(agentConfig):
"""
Retrieve AWS EC2 tags.
"""
if not agentConfig['collect_instance_metadata']:
log.info("Instance metadata collection is disabled. Not collecting it.")
return []
EC2_tags = []
socket_to = None
try:
socket_to = socket.getdefaulttimeout()
socket.setdefaulttimeout(EC2.TIMEOUT)
except Exception:
pass
try:
iam_role = EC2.get_iam_role()
iam_params = json.loads(urllib2.urlopen(EC2.METADATA_URL_BASE + "/iam/security-credentials/" + unicode(iam_role)).read().strip())
instance_identity = json.loads(urllib2.urlopen(EC2.INSTANCE_IDENTITY_URL).read().strip())
region = instance_identity['region']
import boto.ec2
proxy_settings = get_proxy(agentConfig) or {}
connection = boto.ec2.connect_to_region(
region,
aws_access_key_id=iam_params['AccessKeyId'],
aws_secret_access_key=iam_params['SecretAccessKey'],
security_token=iam_params['Token'],
proxy=proxy_settings.get('host'), proxy_port=proxy_settings.get('port'),
proxy_user=proxy_settings.get('user'), proxy_pass=proxy_settings.get('password')
)
tag_object = connection.get_all_tags({'resource-id': EC2.metadata['instance-id']})
EC2_tags = [u"%s:%s" % (tag.name, tag.value) for tag in tag_object]
if agentConfig.get('collect_security_groups') and EC2.metadata.get('security-groups'):
EC2_tags.append(u"security-group-name:{0}".format(EC2.metadata.get('security-groups')))
except EC2.NoIAMRole:
log.warning(
u"Unable to retrieve AWS EC2 custom tags: "
u"an IAM role associated with the instance is required"
)
except Exception:
log.exception("Problem retrieving custom EC2 tags")
try:
if socket_to is None:
socket_to = 3
socket.setdefaulttimeout(socket_to)
except Exception:
pass
return EC2_tags
@staticmethod
def get_metadata(agentConfig):
"""Use the ec2 http service to introspect the instance. This adds latency if not running on EC2
"""
# >>> import urllib2
# >>> urllib2.urlopen('http://169.254.169.254/latest/', timeout=1).read()
# 'meta-data\nuser-data'
# >>> urllib2.urlopen('http://169.254.169.254/latest/meta-data', timeout=1).read()
# 'ami-id\nami-launch-index\nami-manifest-path\nhostname\ninstance-id\nlocal-ipv4\npublic-keys/\nreservation-id\nsecurity-groups'
# >>> urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id', timeout=1).read()
# 'i-deadbeef'
# Every call may add TIMEOUT seconds in latency so don't abuse this call
# python 2.4 does not support an explicit timeout argument so force it here
# Rather than monkey-patching urllib2, just lower the timeout globally for these calls
if not agentConfig['collect_instance_metadata']:
log.info("Instance metadata collection is disabled. Not collecting it.")
return {}
socket_to = None
try:
socket_to = socket.getdefaulttimeout()
socket.setdefaulttimeout(EC2.TIMEOUT)
except Exception:
pass
for k in ('instance-id', 'hostname', 'local-hostname', 'public-hostname', 'ami-id', 'local-ipv4', 'public-keys/', 'public-ipv4', 'reservation-id', 'security-groups'):
try:
v = urllib2.urlopen(EC2.METADATA_URL_BASE + "/" + unicode(k)).read().strip()
assert type(v) in (types.StringType, types.UnicodeType) and len(v) > 0, "%s is not a string" % v
EC2.metadata[k.rstrip('/')] = v
except Exception:
pass
try:
if socket_to is None:
socket_to = 3
socket.setdefaulttimeout(socket_to)
except Exception:
pass
return EC2.metadata
@staticmethod
def get_instance_id(agentConfig):
try:
return EC2.get_metadata(agentConfig).get("instance-id", None)
except Exception:
return None
class Watchdog(object):
"""
Simple signal-based watchdog. Restarts the process when:
* no reset was made for more than a specified duration
* (optional) a specified memory threshold is exceeded
* (optional) a suspicious high activity is detected, i.e. too many resets for a given timeframe.
**Warning**: Not thread-safe.
Can only be invoked once per process, so don't use with multiple threads.
If you instantiate more than one, you're also asking for trouble.
"""
# Activity history timeframe
_RESTART_TIMEFRAME = 60
def __init__(self, duration, max_mem_mb=None, max_resets=None):
import resource
# Set the duration
self._duration = int(duration)
signal.signal(signal.SIGALRM, Watchdog.self_destruct)
# Set memory usage threshold
if max_mem_mb is not None:
self._max_mem_kb = 1024 * max_mem_mb
max_mem_bytes = 1024 * self._max_mem_kb
resource.setrlimit(resource.RLIMIT_AS, (max_mem_bytes, max_mem_bytes))
self.memory_limit_enabled = True
else:
self.memory_limit_enabled = False
# Set high activity monitoring
self._restarts = deque([])
self._max_resets = max_resets
@staticmethod
def self_destruct(signum, frame):
"""
Kill the process. It will be eventually restarted.
"""
try:
import traceback
log.error("Self-destructing...")
log.error(traceback.format_exc())
finally:
os.kill(os.getpid(), signal.SIGKILL)
def _is_frenetic(self):
"""
Detect suspicious high activity, i.e. the number of resets exceeds the maximum limit set
on the watchdog timeframe.
Flush old activity history
"""
now = time.time()
while(self._restarts and self._restarts[0] < now - self._RESTART_TIMEFRAME):
self._restarts.popleft()
return len(self._restarts) > self._max_resets
def reset(self):
"""
Reset the watchdog state, i.e.
* re-arm alarm signal
* (optional) check memory consumption
* (optional) save reset history, flush old entries and check frequency
"""
# Check memory consumption: restart if too high as tornado will swallow MemoryErrors
if self.memory_limit_enabled:
mem_usage_kb = int(os.popen('ps -p %d -o %s | tail -1' % (os.getpid(), 'rss')).read())
if mem_usage_kb > (0.95 * self._max_mem_kb):
Watchdog.self_destruct(signal.SIGKILL, sys._getframe(0))
# Check activity
if self._max_resets:
self._restarts.append(time.time())
if self._is_frenetic():
Watchdog.self_destruct(signal.SIGKILL, sys._getframe(0))
# Re arm alarm signal
log.debug("Resetting watchdog for %d" % self._duration)
signal.alarm(self._duration)
class Timer(object):
""" Helper class """
def __init__(self):
self.start()
def _now(self):
return time.time()
def start(self):
self.started = self._now()
self.last = self.started
return self
def step(self):
now = self._now()
step = now - self.last
self.last = now
return step
def total(self, as_sec=True):
return self._now() - self.started
"""
Iterable Recipes
"""
def chunks(iterable, chunk_size):
"""Generate sequences of `chunk_size` elements from `iterable`."""
iterable = iter(iterable)
while True:
chunk = [None] * chunk_size
count = 0
try:
for _ in range(chunk_size):
chunk[count] = iterable.next()
count += 1
yield chunk[:count]
except StopIteration:
if count:
yield chunk[:count]
break