validateYarnComponentStatus.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python2.6
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import optparse
  18. import urllib2, urllib
  19. import json
  20. RESOURCEMANAGER = 'rm'
  21. HISTORYSERVER ='hs'
  22. STARTED_STATE = 'STARTED'
  23. def validate(component, path, address):
  24. try:
  25. url = 'http://' + address + path
  26. opener = urllib2.build_opener()
  27. urllib2.install_opener(opener)
  28. request = urllib2.Request(url)
  29. handler = urllib2.urlopen(request)
  30. response = json.loads(handler.read())
  31. is_valid = validateResponse(component, response)
  32. if is_valid:
  33. exit(0)
  34. else:
  35. exit(1)
  36. except Exception as e:
  37. print 'Error checking status of component', e
  38. exit(1)
  39. def validateResponse(component, response):
  40. try:
  41. if component == RESOURCEMANAGER:
  42. rm_state = response['clusterInfo']['state']
  43. if rm_state == STARTED_STATE:
  44. return True
  45. else:
  46. return False
  47. elif component == HISTORYSERVER:
  48. hs_start_time = response['historyInfo']['startedOn']
  49. if hs_start_time > 0:
  50. return True
  51. else:
  52. return False
  53. else:
  54. return False
  55. except Exception as e:
  56. print 'Error validation of response', e
  57. return False
  58. #
  59. # Main.
  60. #
  61. def main():
  62. parser = optparse.OptionParser(usage="usage: %prog [options] component ")
  63. parser.add_option("-p", "--port", dest="address", help="Host:Port for REST API of a desired component")
  64. (options, args) = parser.parse_args()
  65. component = args[0]
  66. address = options.address
  67. if component == RESOURCEMANAGER:
  68. path = '/ws/v1/cluster/info'
  69. elif component == HISTORYSERVER:
  70. path = '/ws/v1/history/info'
  71. else:
  72. parser.error("Invalid component")
  73. validate(component, path, address)
  74. if __name__ == "__main__":
  75. main()