Skip to content
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

Merged
merged 3 commits into from
May 5, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 113 additions & 0 deletions checks.d/mesos.py
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:
raise Exception('Mesos instance missing "url" value.')
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.get_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.get_master_stats(url, timeout)
if response is not None:
for key in iter(response):
self.gauge('mesos.stats.' + key, response[key], tags=['mesos'])
Copy link
Contributor

Choose a reason for hiding this comment

The 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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.get_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 get_master_roles(self, url, timeout):
return self.get_json(url + "/master/roles.json", timeout)

def get_master_stats(self, url, timeout):
return self.get_json(url + "/master/stats.json", timeout)

def get_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
7 changes: 7 additions & 0 deletions conf.d/mesos.yaml.example
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"