summaryrefslogtreecommitdiff
path: root/ambari-server/src/main/resources/stacks/HDPWIN/2.1/services/YARN/package/files/validateYarnComponentStatus.py
blob: 073371a38cc37588bc0e61fc5de02326be2d6a0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python

'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''

import optparse
import subprocess
import json
import urllib2

RESOURCEMANAGER = 'rm'
NODEMANAGER = 'nm'
HISTORYSERVER = 'hs'

STARTED_STATE = 'STARTED'
RUNNING_STATE = 'RUNNING'

#Return reponse for given path and address
def getResponse(path, address, ssl_enabled):
  if ssl_enabled:
    url = 'https://' + address + path
  else:
    url = 'http://' + address + path

  try:
    handle = urllib2.urlopen(url)
    output = handle.read()
    handle.close()
    response = json.loads(output)
    if response == None:
      print 'There is no response for url: ' + str(url)
      exit(1)
    return response
  except Exception as e:
    print 'Error getting response for url:' + str(url), e
    exit(1)

#Verify that REST api is available for given component
def validateAvailability(component, path, address, ssl_enabled):

  try:
    response = getResponse(path, address, ssl_enabled)
    is_valid = validateAvailabilityResponse(component, response)
    if not is_valid:
      exit(1)
  except Exception as e:
    print 'Error checking availability status of component', e
    exit(1)

#Validate component-specific response
def validateAvailabilityResponse(component, response):
  try:
    if component == RESOURCEMANAGER:
      rm_state = response['clusterInfo']['state']
      if rm_state == STARTED_STATE:
        return True
      else:
        print 'Resourcemanager is not started'
        return False

    elif component == NODEMANAGER:
      node_healthy = bool(response['nodeInfo']['nodeHealthy'])
      if node_healthy:
        return True
      else:
        return False
    elif component == HISTORYSERVER:
      hs_start_time = response['historyInfo']['startedOn']
      if hs_start_time > 0:
        return True
      else:
        return False
    else:
      return False
  except Exception as e:
    print 'Error validation of availability response for ' + str(component), e
    return False

#Verify that component has required resources to work
def validateAbility(component, path, address, ssl_enabled):

  try:
    response = getResponse(path, address, ssl_enabled)
    is_valid = validateAbilityResponse(component, response)
    if not is_valid:
      exit(1)
  except Exception as e:
    print 'Error checking ability of component', e
    exit(1)

#Validate component-specific response that it has required resources to work
def validateAbilityResponse(component, response):
  try:
    if component == RESOURCEMANAGER:
      nodes = []
      if response.has_key('nodes') and not response['nodes'] == None and response['nodes'].has_key('node'):
        nodes = response['nodes']['node']
      connected_nodes_count = len(nodes)
      if connected_nodes_count == 0:
        print 'There is no connected nodemanagers to resourcemanager'
        return False
      active_nodes = filter(lambda x: x['state'] == RUNNING_STATE, nodes)
      active_nodes_count = len(active_nodes)

      if connected_nodes_count == 0:
        print 'There is no connected active nodemanagers to resourcemanager'
        return False
      else:
        return True
    else:
      return False
  except Exception as e:
    print 'Error validation of ability response', e
    return False

#
# Main.
#
def main():
  parser = optparse.OptionParser(usage="usage: %prog [options] component ")
  parser.add_option("-p", "--port", dest="address", help="Host:Port for REST API of a desired component")
  parser.add_option("-s", "--ssl", dest="ssl_enabled", help="Is SSL enabled for UI of component")

  (options, args) = parser.parse_args()

  component = args[0]

  address = options.address
  ssl_enabled = (options.ssl_enabled) in 'true'
  if component == RESOURCEMANAGER:
    path = '/ws/v1/cluster/info'
  elif component == NODEMANAGER:
    path = '/ws/v1/node/info'
  elif component == HISTORYSERVER:
    path = '/ws/v1/history/info'
  else:
    parser.error("Invalid component")

  validateAvailability(component, path, address, ssl_enabled)

  if component == RESOURCEMANAGER:
    path = '/ws/v1/cluster/nodes'
    validateAbility(component, path, address, ssl_enabled)

if __name__ == "__main__":
  main()