-
Notifications
You must be signed in to change notification settings - Fork 814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic monitoring support for Apache Mesos #919
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import time | ||
import requests | ||
|
||
from checks import AgentCheck | ||
from util import json, headers | ||
from hashlib import md5 | ||
import urllib2 | ||
|
||
class Mesos(AgentCheck): | ||
def check(self, instance): | ||
if 'url' not in instance: | ||
self.log.info("Skipping instance, no url found.") | ||
return | ||
|
||
# Load values from the instance config | ||
url = instance['url'] | ||
default_timeout = self.init_config.get('default_timeout', 5) | ||
timeout = float(instance.get('timeout', default_timeout)) | ||
|
||
response = self.master_roles(url, timeout) | ||
if response is not None: | ||
for role in response['roles']: | ||
tags = ['mesos','role:' + role['name']] | ||
self.gauge('mesos.role.frameworks', len(role['frameworks']), tags=tags) | ||
self.gauge('mesos.role.weight', role['weight'], tags=tags) | ||
resources = role['resources'] | ||
for attr in ['cpus','mem']: | ||
if attr in resources: | ||
self.gauge('mesos.role.' + attr, resources[attr], tags=tags) | ||
|
||
response = self.master_stats(url, timeout) | ||
if response is not None: | ||
for key in iter(response): | ||
self.gauge('mesos.stats.' + key, response[key], tags=['mesos']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that this tag will be useful, do you have a use case for it ? Also could you add support for custom tags (that would be defined in the yaml file) as we do in some other checks ? Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure I'll take a peek at the custom tags tomorrow and dispense with the 'mesos' tag. |
||
|
||
response = self.master_state(url, timeout) | ||
if response is not None: | ||
for attr in ['deactivated_slaves','failed_tasks','finished_tasks','killed_tasks','lost_tasks','staged_tasks','started_tasks']: | ||
tags = ['mesos'] | ||
self.gauge('mesos.state.' + attr, response[attr], tags=tags) | ||
|
||
for framework in response['frameworks']: | ||
tags = ['mesos','framework:' + framework['id']] | ||
resources = framework['resources'] | ||
for attr in ['cpus','mem']: | ||
if attr in resources: | ||
self.gauge('mesos.state.framework.' + attr, resources[attr], tags=tags) | ||
|
||
for slave in response['slaves']: | ||
tags = ['mesos','slave:' + slave['id']] | ||
resources = slave['resources'] | ||
for attr in ['cpus','mem','disk']: | ||
if attr in resources: | ||
self.gauge('mesos.state.slave.' + attr, resources[attr], tags=tags) | ||
|
||
def master_roles(self, url, timeout): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a nitpick but could you prefix these methods with "get_" def get_master_roles(self, url, timeout): It would be more explicit. |
||
return self.get_json(url + "/master/roles.json", timeout) | ||
|
||
def master_stats(self, url, timeout): | ||
return self.get_json(url + "/master/stats.json", timeout) | ||
|
||
def master_state(self, url, timeout): | ||
return self.get_json(url + "/master/state.json", timeout) | ||
|
||
def get_json(self, url, timeout): | ||
# Use a hash of the URL as an aggregation key | ||
aggregation_key = md5(url).hexdigest() | ||
|
||
try: | ||
response = requests.get(url, timeout=timeout) | ||
parsed = response.json() | ||
return parsed | ||
except requests.exceptions.Timeout as e: | ||
# If there's a timeout | ||
self.timeout_event(url, timeout, aggregation_key) | ||
return None | ||
|
||
if r.status_code != 200: | ||
self.status_code_event(url, r, aggregation_key) | ||
return None | ||
|
||
|
||
def timeout_event(self, url, timeout, aggregation_key): | ||
self.event({ | ||
'timestamp': int(time.time()), | ||
'event_type': 'http_check', | ||
'msg_title': 'URL timeout', | ||
'msg_text': '%s timed out after %s seconds.' % (url, timeout), | ||
'aggregation_key': aggregation_key | ||
}) | ||
|
||
def status_code_event(self, url, r, aggregation_key): | ||
self.event({ | ||
'timestamp': int(time.time()), | ||
'event_type': 'http_check', | ||
'msg_title': 'Invalid reponse code for %s' % url, | ||
'msg_text': '%s returned a status of %s' % (url, r.status_code), | ||
'aggregation_key': aggregation_key | ||
}) | ||
|
||
if __name__ == '__main__': | ||
check, instances = Mesos.from_yaml('/etc/dd-agent/conf.d/mesos.yaml') | ||
for instance in instances: | ||
print "\nRunning the check against url: %s" % (instance['url']) | ||
check.check(instance) | ||
if check.has_events(): | ||
print 'Events: %s' % (check.get_events()) | ||
|
||
i = 0 | ||
print 'Metrics:\n' | ||
for metric in check.get_metrics(): | ||
print " %d: %s" % (i, metric) | ||
i += 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
init_config: | ||
# time to wait on a Mesos API request | ||
# default_timeout: 5 | ||
|
||
instances: | ||
# url: the API endpoint of your Mesos master | ||
# - url: "https://server:port" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you raise an Exception here instead ? The exception would then appear in the info page.