LiveStatus.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  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 json
  18. import logging
  19. from StatusCheck import StatusCheck
  20. import AmbariConfig
  21. from StackVersionsFileHandler import StackVersionsFileHandler
  22. from ActualConfigHandler import ActualConfigHandler
  23. logger = logging.getLogger()
  24. class LiveStatus:
  25. SERVICES = []
  26. CLIENT_COMPONENTS = []
  27. COMPONENTS = []
  28. LIVE_STATUS = "STARTED"
  29. DEAD_STATUS = "INSTALLED"
  30. def __init__(self, cluster, service, component, globalConfig, config,
  31. configTags):
  32. self.cluster = cluster
  33. self.service = service
  34. self.component = component
  35. self.globalConfig = globalConfig
  36. versionsFileDir = config.get('agent', 'prefix')
  37. self.versionsHandler = StackVersionsFileHandler(versionsFileDir)
  38. self.configTags = configTags
  39. self.actualConfigHandler = ActualConfigHandler(config, configTags)
  40. def belongsToService(self, component):
  41. #TODO: Should also check belonging of server to cluster
  42. return component['serviceName'] == self.service
  43. def build(self, forsed_component_status = None):
  44. """
  45. If forsed_component_status is explicitly defined, than StatusCheck methods are
  46. not used. This feature has been added to support custom (ver 2.0) services.
  47. """
  48. global SERVICES, CLIENT_COMPONENTS, COMPONENTS, LIVE_STATUS, DEAD_STATUS
  49. component = {"serviceName" : self.service, "componentName" : self.component}
  50. if forsed_component_status: # If already determined
  51. status = forsed_component_status # Nothing to do
  52. elif component in self.CLIENT_COMPONENTS:
  53. status = self.DEAD_STATUS # CLIENT components can't have status STARTED
  54. elif component in self.COMPONENTS:
  55. statusCheck = StatusCheck(AmbariConfig.servicesToPidNames,
  56. AmbariConfig.pidPathesVars, self.globalConfig,
  57. AmbariConfig.servicesToLinuxUser)
  58. serviceStatus = statusCheck.getStatus(self.component)
  59. if serviceStatus is None:
  60. logger.warn("There is no service to pid mapping for " + self.component)
  61. status = self.LIVE_STATUS if serviceStatus else self.DEAD_STATUS
  62. livestatus = {"componentName" : self.component,
  63. "msg" : "",
  64. "status" : status,
  65. "clusterName" : self.cluster,
  66. "serviceName" : self.service,
  67. "stackVersion": self.versionsHandler.
  68. read_stack_version(self.component)
  69. }
  70. active_config = self.actualConfigHandler.read_actual_component(self.component)
  71. if not active_config is None:
  72. livestatus['configurationTags'] = active_config
  73. logger.debug("The live status for component " + str(self.component) +\
  74. " of service " + str(self.service) + " is " + str(livestatus))
  75. return livestatus
  76. def main(argv=None):
  77. for service in SERVICES:
  78. livestatus = LiveStatus('', service)
  79. print json.dumps(livestatus.build())
  80. if __name__ == '__main__':
  81. main()