HostCheckReportFileHandler.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 datetime
  18. import os.path
  19. import logging
  20. import traceback
  21. from AmbariConfig import AmbariConfig
  22. import ConfigParser;
  23. logger = logging.getLogger()
  24. class HostCheckReportFileHandler:
  25. HOST_CHECK_FILE = "hostcheck.result"
  26. HOST_CHECK_CUSTOM_ACTIONS_FILE = "hostcheck_custom_actions.result"
  27. def __init__(self, config=None):
  28. self.hostCheckFilePath = None
  29. if config is None:
  30. config = self.resolve_ambari_config()
  31. hostCheckFileDir = config.get('agent', 'prefix')
  32. self.hostCheckFilePath = os.path.join(hostCheckFileDir, self.HOST_CHECK_FILE)
  33. self.hostCheckCustomActionsFilePath = os.path.join(hostCheckFileDir, self.HOST_CHECK_CUSTOM_ACTIONS_FILE)
  34. def resolve_ambari_config(self):
  35. try:
  36. config = AmbariConfig()
  37. if os.path.exists(AmbariConfig.getConfigFile()):
  38. config.read(AmbariConfig.getConfigFile())
  39. else:
  40. raise Exception("No config found, use default")
  41. except Exception, err:
  42. logger.warn(err)
  43. return config
  44. def writeHostChecksCustomActionsFile(self, structuredOutput):
  45. if self.hostCheckCustomActionsFilePath is None:
  46. return
  47. try:
  48. logger.info("Host check custom action report at " + self.hostCheckCustomActionsFilePath)
  49. config = ConfigParser.RawConfigParser()
  50. config.add_section('metadata')
  51. config.set('metadata', 'created', str(datetime.datetime.now()))
  52. if 'installed_packages' in structuredOutput.keys():
  53. items = []
  54. for itemDetail in structuredOutput['installed_packages']:
  55. items.append(itemDetail['name'])
  56. config.add_section('packages')
  57. config.set('packages', 'pkg_list', ','.join(map(str, items)))
  58. if 'existing_repos' in structuredOutput.keys():
  59. config.add_section('repositories')
  60. config.set('repositories', 'repo_list', ','.join(structuredOutput['existing_repos']))
  61. self.removeFile(self.hostCheckCustomActionsFilePath)
  62. self.touchFile(self.hostCheckCustomActionsFilePath)
  63. with open(self.hostCheckCustomActionsFilePath, 'wb') as configfile:
  64. config.write(configfile)
  65. except Exception, err:
  66. logger.error("Can't write host check file at %s :%s " % (self.hostCheckFilePath, err.message))
  67. traceback.print_exc()
  68. def writeHostCheckFile(self, hostInfo):
  69. if self.hostCheckFilePath is None:
  70. return
  71. try:
  72. logger.info("Host check report at " + self.hostCheckFilePath)
  73. config = ConfigParser.RawConfigParser()
  74. config.add_section('metadata')
  75. config.set('metadata', 'created', str(datetime.datetime.now()))
  76. if 'existingUsers' in hostInfo.keys():
  77. items = []
  78. items2 = []
  79. for itemDetail in hostInfo['existingUsers']:
  80. items.append(itemDetail['name'])
  81. items2.append(itemDetail['homeDir'])
  82. config.add_section('users')
  83. config.set('users', 'usr_list', ','.join(items))
  84. config.set('users', 'usr_homedir_list', ','.join(items2))
  85. if 'alternatives' in hostInfo.keys():
  86. items = []
  87. items2 = []
  88. for itemDetail in hostInfo['alternatives']:
  89. items.append(itemDetail['name'])
  90. items2.append(itemDetail['target'])
  91. config.add_section('alternatives')
  92. config.set('alternatives', 'symlink_list', ','.join(items))
  93. config.set('alternatives', 'target_list', ','.join(items2))
  94. if 'stackFoldersAndFiles' in hostInfo.keys():
  95. items = []
  96. for itemDetail in hostInfo['stackFoldersAndFiles']:
  97. items.append(itemDetail['name'])
  98. config.add_section('directories')
  99. config.set('directories', 'dir_list', ','.join(items))
  100. if 'hostHealth' in hostInfo.keys():
  101. if 'activeJavaProcs' in hostInfo['hostHealth'].keys():
  102. items = []
  103. for itemDetail in hostInfo['hostHealth']['activeJavaProcs']:
  104. items.append(itemDetail['pid'])
  105. config.add_section('processes')
  106. config.set('processes', 'proc_list', ','.join(map(str, items)))
  107. self.removeFile(self.hostCheckFilePath)
  108. self.touchFile(self.hostCheckFilePath)
  109. with open(self.hostCheckFilePath, 'wb') as configfile:
  110. config.write(configfile)
  111. except Exception, err:
  112. logger.error("Can't write host check file at %s :%s " % (self.hostCheckFilePath, err.message))
  113. traceback.print_exc()
  114. def removeFile(self, path):
  115. if os.path.isfile(path):
  116. logger.info("Removing old host check file at %s" % path)
  117. os.remove(path)
  118. def touchFile(self, path):
  119. if not os.path.isfile(path):
  120. logger.info("Creating host check file at %s" % path)
  121. open(path, 'w').close()