added find_active_hadoop_yarn_resource_manager.py
69a60114
Hari Sekhon
committed
1 changed file
find_active_hadoop_yarn_resource_manager.py
/find_active_hadoop_yarn_resource_manager.py+104
/find_active_hadoop_yarn_resource_manager.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: Wed Sep 6 14:44:38 CEST 2017
Add comment 6 Plus  #
Add comment 7 Plus  # https://github.com/harisekhon/pytools
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 to help steer this or other code I publish
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  Tool to return the active Hadoop Yarn Resource Manager from an argument list of hosts
Add comment 20 Plus  
Add comment 21 Plus  Can mix and match between a comma separated list of hosts (--host server1,server2 or contents of the $HOST
Add comment 22 Plus  environment variable if not specified) and general free-form space separated arguments, which is useful if piping
Add comment 23 Plus  a host list through xargs.
Add comment 24 Plus  
Add comment 25 Plus  Multi-threaded for speed and exits upon first available host response to minimize delay to ~ 1 second or less.
Add comment 26 Plus  
Add comment 27 Plus  Useful for simplying scripting or generically extending tools that don't directly support Yarn High Availability
Add comment 28 Plus  
Add comment 29 Plus  By default checks the same --port on all servers. Hosts may have optional :<port> suffixes added to individually
Add comment 30 Plus  override each one.
Add comment 31 Plus  
Add comment 32 Plus  Exits with return code 1 and NO_AVAILABLE_SERVER if none of the namenodes are active, --quiet mode will not print
Add comment 33 Plus  NO_AVAILABLE_SERVER.
Add comment 34 Plus  
Add comment 35 Plus  Tested on Hadoop 2.7.3 on HDP 2.6.1
Add comment 36 Plus  
Add comment 37 Plus  """
Add comment 38 Plus  
Add comment 39 Plus  from __future__ import absolute_import
Add comment 40 Plus  from __future__ import division
Add comment 41 Plus  from __future__ import print_function
Add comment 42 Plus  #from __future__ import unicode_literals
Add comment 43 Plus  
Add comment 44 Plus  import os
Add comment 45 Plus  import sys
Add comment 46 Plus  import traceback
Add comment 47 Plus  srcdir = os.path.abspath(os.path.dirname(__file__))
Add comment 48 Plus  libdir = os.path.join(srcdir, 'pylib')
Add comment 49 Plus  sys.path.append(libdir)
Add comment 50 Plus  try:
Add comment 51 Plus   # pylint: disable=wrong-import-position
Add comment 52 Plus   from harisekhon.utils import log_option, uniq_list_ordered
Add comment 53 Plus   from harisekhon.utils import validate_int
Add comment 54 Plus   from find_active_server import FindActiveServer
Add comment 55 Plus  except ImportError as _:
Add comment 56 Plus   print(traceback.format_exc(), end='')
Add comment 57 Plus   sys.exit(4)
Add comment 58 Plus  
Add comment 59 Plus  __author__ = 'Hari Sekhon'
Add comment 60 Plus  __version__ = '0.6.1'
Add comment 61 Plus  
Add comment 62 Plus  
Add comment 63 Plus  class FindActiveHadoopYarnResourceManager(FindActiveServer):
Add comment 64 Plus  
Add comment 65 Plus   def __init__(self):
Add comment 66 Plus   # Python 2.x
Add comment 67 Plus   super(FindActiveHadoopYarnResourceManager, self).__init__()
Add comment 68 Plus   # Python 3.x
Add comment 69 Plus   # super().__init__()
Add comment 70 Plus   self.default_port = 8088
Add comment 71 Plus   self.port = self.default_port
Add comment 72 Plus   self.protocol = 'http'
Add comment 73 Plus   self.url_path = '/ws/v1/cluster'
Add comment 74 Plus   self.regex = r'"haState"\s*:\s*"ACTIVE"'
Add comment 75 Plus  
Add comment 76 Plus   def add_options(self):
Add comment 77 Plus   self.add_hostoption(name='Yarn Resource Manager', default_port=self.default_port)
Add comment 78 Plus   self.add_opt('-S', '--ssl', action='store_true',
Add comment 79 Plus   help='Use SSL to Namenode UI')
Add comment 80 Plus   self.add_opt('-q', '--quiet', action='store_true', help='Returns no output instead of NO_AVAILABLE_SERVER '\
Add comment 81 Plus   + '(convenience for scripting)')
Add comment 82 Plus   self.add_opt('-T', '--request-timeout', metavar='secs', type='int', default=os.getenv('REQUEST_TIMEOUT', 2),
Add comment 83 Plus   help='Timeout for each individual server request in seconds ($REQUEST_TIMEOUT, default: 2 secs)')
Add comment 84 Plus  
Add comment 85 Plus   def process_options(self):
Add comment 86 Plus   hosts = self.get_opt('host')
Add comment 87 Plus   self.port = self.get_opt('port')
Add comment 88 Plus   if hosts:
Add comment 89 Plus   self.host_list = [host.strip() for host in hosts.split(',') if host]
Add comment 90 Plus   self.host_list += self.args
Add comment 91 Plus   self.host_list = uniq_list_ordered(self.host_list)
Add comment 92 Plus   if self.get_opt('ssl'):
Add comment 93 Plus   self.protocol = 'https'
Add comment 94 Plus   log_option('SSL', 'true')
Add comment 95 Plus   else:
Add comment 96 Plus   log_option('SSL', 'false')
Add comment 97 Plus   self.request_timeout = self.get_opt('request_timeout')
Add comment 98 Plus   validate_int(self.request_timeout, 'request timeout', 1, 60)
Add comment 99 Plus   self.validate_options()
Add comment 100 Plus  
Add comment 101 Plus  
Add comment 102 Plus  if __name__ == '__main__':
Add comment 103 Plus   FindActiveHadoopYarnResourceManager().main()
Add comment 104 Plus