Skip to content

Commit c1bfa1c

Browse files
committed
added check_mesos_slave.py
1 parent e7ff711 commit c1bfa1c

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

check_mesos_slave.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env python
2+
# vim:ts=4:sts=4:sw=4:et
3+
#
4+
# Author: Hari Sekhon
5+
# Date: 2016-02-19 19:21:30 +0000 (Fri, 19 Feb 2016)
6+
#
7+
# https://github.com/harisekhon/nagios-plugins
8+
#
9+
# License: see accompanying Hari Sekhon LICENSE file
10+
#
11+
# If you're using my code you're welcome to connect with me on LinkedIn
12+
# and optionally send me feedback
13+
#
14+
# https://www.linkedin.com/in/harisekhon
15+
#
16+
17+
"""
18+
19+
Nagios Plugin to check a given Mesos slave is registered with the Mesos Master
20+
21+
"""
22+
23+
from __future__ import absolute_import
24+
from __future__ import division
25+
from __future__ import print_function
26+
27+
import json
28+
import logging
29+
import os
30+
import re
31+
import sys
32+
import traceback
33+
try:
34+
import requests
35+
except ImportError:
36+
print(traceback.format_exc(), end='')
37+
sys.exit(4)
38+
libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'pylib'))
39+
sys.path.append(libdir)
40+
try:
41+
# pylint: disable=wrong-import-position
42+
from harisekhon.utils import log, qquit
43+
from harisekhon.utils import validate_host, validate_port, isJson, support_msg_api, jsonpp, dict_lines
44+
from harisekhon import NagiosPlugin
45+
except ImportError as _:
46+
print(traceback.format_exc(), end='')
47+
sys.exit(4)
48+
49+
__author__ = 'Hari Sekhon'
50+
__version__ = '0.1'
51+
52+
class CheckMesosSlave(NagiosPlugin):
53+
54+
def __init__(self):
55+
# Python 2.x
56+
super(CheckMesosSlave, self).__init__()
57+
# Python 3.x
58+
# super().__init__()
59+
60+
def add_options(self):
61+
self.add_hostoption(name='Mesos Master', default_host='localhost', default_port=5050)
62+
self.add_opt('-s', '--slave', help='Mesos slave name to check is registered on Mesos Master')
63+
self.add_opt('-l', '--list-slaves', action='store_true', help='List slaves and exit')
64+
65+
def run(self):
66+
self.no_args()
67+
host = self.get_opt('host')
68+
port = self.get_opt('port')
69+
slave = self.get_opt('slave')
70+
list_slaves = self.get_opt('list_slaves')
71+
validate_host(host)
72+
validate_port(port)
73+
if not list_slaves:
74+
validate_host(slave, 'slave')
75+
76+
url = 'http://%(host)s:%(port)s/master/slaves' % locals()
77+
log.debug('GET %s' % url)
78+
try:
79+
req = requests.get(url)
80+
except requests.exceptions.RequestException as _:
81+
qquit('CRITICAL', _)
82+
log.debug("response: %s %s" % (req.status_code, req.reason))
83+
log.debug("content:\n{0}\n{1}\n{2}".format('='*80, req.content.strip(), '='*80))
84+
if req.status_code != 200:
85+
qquit('CRITICAL', "Non-200 response! %s %s" % (req.status_code, req.reason))
86+
content = req.content
87+
if not isJson(content):
88+
qquit('UNKNOWN', 'invalid JSON returned by Mesos Master')
89+
data = json.loads(content)
90+
if log.isEnabledFor(logging.DEBUG):
91+
log.debug('\n' + jsonpp(data))
92+
slaves = {}
93+
regex = re.compile(r'^slave\(\d+\)\@(.+):\d+')
94+
try:
95+
for item in data['slaves']:
96+
match = regex.match(item['pid'])
97+
if match:
98+
slaves[item['hostname']] = match.group(1)
99+
else:
100+
slaves[item['hostname']] = item['pid']
101+
except KeyError:
102+
qquit('UNKNOWN', 'failed to parse slaves from Mesos API output. {0}'.format(support_msg_api))
103+
if list_slaves:
104+
qquit('UNKNOWN', 'Slaves list:\n\n{0}'.format(dict_lines(slaves)))
105+
log.info('found slaves:\n\n{0}\n'.format(dict_lines(slaves)))
106+
slave = slave.lower()
107+
for _ in slaves:
108+
if slave == _.lower() or slave == slaves[_].lower():
109+
qquit('OK', "Mesos slave '{0}' registered with master".format(slave))
110+
break
111+
else:
112+
qquit('CRITICAL', "Mesos slave '{0}' not registered with master".format(slave))
113+
114+
115+
if __name__ == '__main__':
116+
CheckMesosSlave().main()

0 commit comments

Comments
 (0)