added check_tachyon.py, check_tachyon_master.py, check_tachyon_worker.py
ffc15fb9
Hari Sekhon
committed
3 changed files
check_tachyon_master.py
/check_tachyon_master.py+1
/check_tachyon_master.py
Add comment 1 Plus  check_tachyon.py
check_tachyon_worker.py
/check_tachyon_worker.py+1
/check_tachyon_worker.py
Add comment 1 Plus  check_tachyon.py
check_tachyon.py
/check_tachyon.py+113
/check_tachyon.py
Add comment 1 Plus  #!/usr/bin/env python
Add comment 2 Plus  # vim:ts=4:sts=4:sw=4:et
Add comment 3 Plus  #
Add comment 4 Plus  # Author: Hari Sekhon
Add comment 5 Plus  # Date: 2016-02-02 17:46:18 +0000 (Tue, 02 Feb 2016)
Add comment 6 Plus  #
Add comment 7 Plus  # https://github.com/harisekhon/nagios-plugins
Add comment 8 Plus  #
Add comment 9 Plus  # License: see accompanying Hari Sekhon LICENSE file
Add comment 10 Plus  #
Add comment 11 Plus  # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
Add comment 12 Plus  # to help improve or steer this or other code I publish
Add comment 13 Plus  #
Add comment 14 Plus  # http://www.linkedin.com/in/harisekhon
Add comment 15 Plus  #
Add comment 16 Plus  
Add comment 17 Plus  """
Add comment 18 Plus  
Add comment 19 Plus  Nagios Plugin to check a Tachyon Master/Worker is online
Add comment 20 Plus  
Add comment 21 Plus  Queries the WebUI and displays the uptime
Add comment 22 Plus  
Add comment 23 Plus  """
Add comment 24 Plus  
Add comment 25 Plus  from __future__ import absolute_import
Add comment 26 Plus  from __future__ import division
Add comment 27 Plus  from __future__ import print_function
Add comment 28 Plus  #from __future__ import unicode_literals
Add comment 29 Plus  
Add comment 30 Plus  import os
Add comment 31 Plus  import re
Add comment 32 Plus  import sys
Add comment 33 Plus  try:
Add comment 34 Plus   from bs4 import BeautifulSoup
Add comment 35 Plus   import requests
Add comment 36 Plus  except ImportError as _:
Add comment 37 Plus   print(_)
Add comment 38 Plus   sys.exit(3)
Add comment 39 Plus  libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'pylib'))
Add comment 40 Plus  sys.path.append(libdir)
Add comment 41 Plus  try:
Add comment 42 Plus   # pylint: disable=wrong-import-position
Add comment 43 Plus   from harisekhon.utils import log, qquit, prog
Add comment 44 Plus   from harisekhon.utils import validate_host, validate_port, isStr, space_prefix
Add comment 45 Plus   from harisekhon import NagiosPlugin
Add comment 46 Plus  except ImportError as _:
Add comment 47 Plus   print('module import failed: %s' % _, file=sys.stderr)
Add comment 48 Plus   print("Did you remember to build the project by running 'make'?", file=sys.stderr)
Add comment 49 Plus   print("Alternatively perhaps you tried to copy this program out without it's adjacent libraries?", file=sys.stderr)
Add comment 50 Plus   sys.exit(4)
Add comment 51 Plus  
Add comment 52 Plus  __author__ = 'Hari Sekhon'
Add comment 53 Plus  __version__ = '0.1'
Add comment 54 Plus  
Add comment 55 Plus  
Add comment 56 Plus  class CheckTachyon(NagiosPlugin):
Add comment 57 Plus  
Add comment 58 Plus   def __init__(self):
Add comment 59 Plus   # Python 2.x
Add comment 60 Plus   super(CheckTachyon, self).__init__()
Add comment 61 Plus   # Python 3.x
Add comment 62 Plus   # super().__init__()
Add comment 63 Plus   name = ''
Add comment 64 Plus   default_port = None
Add comment 65 Plus   if re.search('master', prog, re.I):
Add comment 66 Plus   name = 'Master'
Add comment 67 Plus   default_port = 19999
Add comment 68 Plus   elif re.search('worker|slave', prog, re.I):
Add comment 69 Plus   name = 'Worker'
Add comment 70 Plus   default_port = 30000
Add comment 71 Plus   self.name = space_prefix(name)
Add comment 72 Plus   self.default_port = default_port
Add comment 73 Plus  
Add comment 74 Plus   def add_options(self):
Add comment 75 Plus   self.add_hostoption(name='Tachyon%(name)s' % self.__dict__,
Add comment 76 Plus   default_host='localhost',
Add comment 77 Plus   default_port=self.default_port)
Add comment 78 Plus  
Add comment 79 Plus   def run(self):
Add comment 80 Plus   self.no_args()
Add comment 81 Plus   host = self.get_opt('host')
Add comment 82 Plus   port = self.get_opt('port')
Add comment 83 Plus   validate_host(host)
Add comment 84 Plus   validate_port(port)
Add comment 85 Plus  
Add comment 86 Plus   log.info('querying Tachyon%(name)s' % self.__dict__)
Add comment 87 Plus   url = 'http://%(host)s:%(port)s/home' % locals()
Add comment 88 Plus   log.debug('GET %s' % url)
Add comment 89 Plus   try:
Add comment 90 Plus   req = requests.get(url)
Add comment 91 Plus   except requests.exceptions.RequestException as _:
Add comment 92 Plus   qquit('CRITICAL', _)
Add comment 93 Plus   log.debug("response: %s %s" % (req.status_code, req.reason))
Add comment 94 Plus   log.debug("content:\n{0}\n{1}\n{2}".format('='*80, req.content.strip(), '='*80))
Add comment 95 Plus   if req.status_code != 200:
Add comment 96 Plus   qquit('CRITICAL', "Non-200 response! %s %s" % (req.status_code, req.reason))
Add comment 97 Plus   soup = BeautifulSoup(req.content, 'html.parser')
Add comment 98 Plus   link = soup.find('th', text=re.compile('Uptime:?', re.I))
Add comment 99 Plus   if link is None:
Add comment 100 Plus   qquit('UNKNOWN', 'failed to find Tachyon%(name)s uptime' % self.__dict__)
Add comment 101 Plus   link = link.find_next_sibling()
Add comment 102 Plus   if link is None:
Add comment 103 Plus   qquit('UNKNOWN', 'failed to find Tachyon%(name)s uptime (next sibling tag not found)' % self.__dict__)
Add comment 104 Plus   uptime = link.get_text()
Add comment 105 Plus   if not uptime or not isStr(uptime) or not re.search(r'\d+\s+second', uptime):
Add comment 106 Plus   qquit('UNKNOWN', 'Tachyon{0} uptime format not recognized: {1}'.format(self.name, uptime))
Add comment 107 Plus   self.msg = 'Tachyon{0} uptime: {1}'.format(self.name, uptime) # pylint: disable=attribute-defined-outside-init
Add comment 108 Plus   self.ok()
Add comment 109 Plus  
Add comment 110 Plus  
Add comment 111 Plus  if __name__ == '__main__':
Add comment 112 Plus   CheckTachyon().main()
Add comment 113 Plus