forked from DataDog/dd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarathon.py
81 lines (66 loc) · 2.62 KB
/
marathon.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
# (C) Datadog, Inc. 2014-2016
# (C) graemej <[email protected]> 2014
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
# stdlib
from urlparse import urljoin
# 3rd party
import requests
# project
from checks import AgentCheck
class Marathon(AgentCheck):
DEFAULT_TIMEOUT = 5
SERVICE_CHECK_NAME = 'marathon.can_connect'
APP_METRICS = [
'backoffFactor',
'backoffSeconds',
'cpus',
'disk',
'instances',
'mem',
'taskRateLimit',
'tasksRunning',
'tasksStaged'
]
def check(self, instance):
if 'url' not in instance:
raise Exception('Marathon instance missing "url" value.')
# Load values from the instance config
url = instance['url']
user = instance.get('user')
password = instance.get('password')
if user is not None and password is not None:
auth = (user,password)
else:
auth = None
instance_tags = instance.get('tags', [])
default_timeout = self.init_config.get('default_timeout', self.DEFAULT_TIMEOUT)
timeout = float(instance.get('timeout', default_timeout))
response = self.get_json(urljoin(url, "/v2/apps"), timeout, auth)
if response is not None:
self.gauge('marathon.apps', len(response['apps']), tags=instance_tags)
for app in response['apps']:
tags = ['app_id:' + app['id'], 'version:' + app['version']] + instance_tags
for attr in self.APP_METRICS:
if attr in app:
self.gauge('marathon.' + attr, app[attr], tags=tags)
def get_json(self, url, timeout, auth):
try:
r = requests.get(url, timeout=timeout, auth=auth)
r.raise_for_status()
except requests.exceptions.Timeout:
# If there's a timeout
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
message='%s timed out after %s seconds.' % (url, timeout),
tags = ["url:{0}".format(url)])
raise Exception("Timeout when hitting %s" % url)
except requests.exceptions.HTTPError:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
message='%s returned a status of %s' % (url, r.status_code),
tags = ["url:{0}".format(url)])
raise Exception("Got %s when hitting %s" % (r.status_code, url))
else:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,
tags = ["url:{0}".format(url)]
)
return r.json()