added check_hiveserver2_llap_peers.py
573eadc2
Hari Sekhon
committed
1 changed file
check_hiveserver2_llap_peers.py
/check_hiveserver2_llap_peers.py+115
/check_hiveserver2_llap_peers.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: 2017-06-20 13:58:39 +0200 (Tue, 20 Jun 2017)
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
Add comment 12 Plus  # and optionally send me feedback
Add comment 13 Plus  #
Add comment 14 Plus  # https://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 HiveServer2 Interactive LLAP peers via the HTTP Rest API
Add comment 20 Plus  
Add comment 21 Plus  Optional checks:
Add comment 22 Plus  
Add comment 23 Plus  - number of peers online vs warning/critical thresholds
Add comment 24 Plus  - specific peer available (regex against node FQDN)
Add comment 25 Plus  
Add comment 26 Plus  Tested on Hive 1.2.1 on Hortonworks HDP 2.6.0
Add comment 27 Plus  
Add comment 28 Plus  """
Add comment 29 Plus  
Add comment 30 Plus  from __future__ import absolute_import
Add comment 31 Plus  from __future__ import division
Add comment 32 Plus  from __future__ import print_function
Add comment 33 Plus  #from __future__ import unicode_literals
Add comment 34 Plus  
Add comment 35 Plus  import os
Add comment 36 Plus  import re
Add comment 37 Plus  import sys
Add comment 38 Plus  import traceback
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 UnknownError, support_msg_api
Add comment 44 Plus   from harisekhon.utils import isList, validate_regex, plural
Add comment 45 Plus   from harisekhon import RestNagiosPlugin
Add comment 46 Plus  except ImportError as _:
Add comment 47 Plus   print(traceback.format_exc(), end='')
Add comment 48 Plus   sys.exit(4)
Add comment 49 Plus  
Add comment 50 Plus  __author__ = 'Hari Sekhon'
Add comment 51 Plus  __version__ = '0.5'
Add comment 52 Plus  
Add comment 53 Plus  
Add comment 54 Plus  class CheckHiveServer2InteractivePeers(RestNagiosPlugin):
Add comment 55 Plus  
Add comment 56 Plus   def __init__(self):
Add comment 57 Plus   # Python 2.x
Add comment 58 Plus   super(CheckHiveServer2InteractivePeers, self).__init__()
Add comment 59 Plus   # Python 3.x
Add comment 60 Plus   # super().__init__()
Add comment 61 Plus   self.name = 'HiveServer2 Interactive LLAP'
Add comment 62 Plus   self.msg = self.name + ': '
Add comment 63 Plus   self.default_port = 15002
Add comment 64 Plus   self.path = 'peers'
Add comment 65 Plus   self.json = True
Add comment 66 Plus   self.auth = False
Add comment 67 Plus   self.regex = None
Add comment 68 Plus  
Add comment 69 Plus   def add_options(self):
Add comment 70 Plus   super(CheckHiveServer2InteractivePeers, self).add_options()
Add comment 71 Plus   self.add_opt('-r', '--regex', metavar='host', help='Regex of host fqdn to expect in peer list (optional)')
Add comment 72 Plus   self.add_thresholds()
Add comment 73 Plus  
Add comment 74 Plus   def process_options(self):
Add comment 75 Plus   super(CheckHiveServer2InteractivePeers, self).process_options()
Add comment 76 Plus   self.regex = self.get_opt('regex')
Add comment 77 Plus   if self.regex is not None:
Add comment 78 Plus   validate_regex(self.regex, 'peer')
Add comment 79 Plus   self.validate_thresholds(simple='lower', optional=True)
Add comment 80 Plus  
Add comment 81 Plus   def get_key(self, json_data, key):
Add comment 82 Plus   try:
Add comment 83 Plus   return json_data[key]
Add comment 84 Plus   except KeyError:
Add comment 85 Plus   raise UnknownError('\'{0}\' key was not returned in output from '.format(key) +
Add comment 86 Plus   'HiveServer2 Interactive instance at {0}:{1}. {2}'\
Add comment 87 Plus   .format(self.host, self.port, support_msg_api()))
Add comment 88 Plus  
Add comment 89 Plus   def find_peer(self, regex, peers):
Add comment 90 Plus   for peer in peers:
Add comment 91 Plus   host = self.get_key(peer, 'host')
Add comment 92 Plus   if regex.match(host):
Add comment 93 Plus   return host
Add comment 94 Plus   return False
Add comment 95 Plus  
Add comment 96 Plus   def parse_json(self, json_data):
Add comment 97 Plus   dynamic = self.get_key(json_data, 'dynamic')
Add comment 98 Plus   peers = self.get_key(json_data, 'peers')
Add comment 99 Plus   if not isList(peers):
Add comment 100 Plus   raise UnknownError('\'peers\' field is not a list as expected! {0}'.format(support_msg_api()))
Add comment 101 Plus   peer_count = len(peers)
Add comment 102 Plus   if self.regex:
Add comment 103 Plus   regex = re.compile(self.regex, re.I)
Add comment 104 Plus   if not self.find_peer(regex, peers):
Add comment 105 Plus   self.msg += 'no peer found matching \'{0}\', '.format(self.regex)
Add comment 106 Plus   self.critical()
Add comment 107 Plus   self.msg += '{0} peer{1} found'.format(peer_count, plural(peer_count))
Add comment 108 Plus   self.check_thresholds(peer_count)
Add comment 109 Plus   self.msg += ', dynamic = {0}'.format(dynamic)
Add comment 110 Plus   self.msg += ' | hiveserver2_llap_peers={0}{1}'.format(peer_count, self.get_perf_thresholds(boundary='lower'))
Add comment 111 Plus  
Add comment 112 Plus  
Add comment 113 Plus  if __name__ == '__main__':
Add comment 114 Plus   CheckHiveServer2InteractivePeers().main()
Add comment 115 Plus