check_host.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. Ambari Agent
  17. """
  18. import os
  19. import subprocess
  20. import socket
  21. import getpass
  22. from resource_management.libraries.functions import packages_analyzer
  23. from ambari_commons import os_utils
  24. from ambari_commons.os_check import OSCheck, OSConst
  25. from ambari_commons.inet_utils import download_file
  26. from resource_management import Script, Execute, format
  27. from ambari_agent.HostInfo import HostInfo
  28. from ambari_agent.HostCheckReportFileHandler import HostCheckReportFileHandler
  29. from resource_management.core.resources import Directory, File
  30. from ambari_commons.constants import AMBARI_SUDO_BINARY
  31. from resource_management.core import shell
  32. CHECK_JAVA_HOME = "java_home_check"
  33. CHECK_DB_CONNECTION = "db_connection_check"
  34. CHECK_HOST_RESOLUTION = "host_resolution_check"
  35. CHECK_LAST_AGENT_ENV = "last_agent_env_check"
  36. CHECK_INSTALLED_PACKAGES = "installed_packages"
  37. CHECK_EXISTING_REPOS = "existing_repos"
  38. DB_MYSQL = "mysql"
  39. DB_ORACLE = "oracle"
  40. DB_POSTGRESQL = "postgres"
  41. DB_MSSQL = "mssql"
  42. JDBC_DRIVER_MYSQL = "com.mysql.jdbc.Driver"
  43. JDBC_DRIVER_ORACLE = "oracle.jdbc.driver.OracleDriver"
  44. JDBC_DRIVER_POSTGRESQL = "org.postgresql.Driver"
  45. JDBC_DRIVER_MSSQL = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
  46. JDBC_DRIVER_SYMLINK_MYSQL = "mysql-jdbc-driver.jar"
  47. JDBC_DRIVER_SYMLINK_ORACLE = "oracle-jdbc-driver.jar"
  48. JDBC_DRIVER_SYMLINK_POSTGRESQL = "postgres-jdbc-driver.jar"
  49. JDBC_DRIVER_SYMLINK_MSSQL = "sqljdbc4.jar"
  50. JDBC_AUTH_SYMLINK_MSSQL = "sqljdbc_auth.dll"
  51. class CheckHost(Script):
  52. # Packages that are used to find repos (then repos are used to find other packages)
  53. PACKAGES = [
  54. "hadoop_2_2_*", "hadoop-2-2-.*", "zookeeper_2_2_*", "zookeeper-2-2-.*",
  55. "hadoop", "zookeeper", "webhcat", "*-manager-server-db", "*-manager-daemons"
  56. ]
  57. # ignore packages from repos whose names start with these strings
  58. IGNORE_PACKAGES_FROM_REPOS = [
  59. "ambari", "installed"
  60. ]
  61. # ignore required packages
  62. IGNORE_PACKAGES = [
  63. "epel-release"
  64. ]
  65. # Additional packages to look for (search packages that start with these)
  66. ADDITIONAL_PACKAGES = [
  67. "rrdtool", "rrdtool-python", "ganglia", "gmond", "gweb", "libconfuse",
  68. "ambari-log4j", "hadoop", "zookeeper", "oozie", "webhcat"
  69. ]
  70. # ignore repos from the list of repos to be cleaned
  71. IGNORE_REPOS = [
  72. "ambari", "HDP-UTILS"
  73. ]
  74. def __init__(self):
  75. self.reportFileHandler = HostCheckReportFileHandler()
  76. def actionexecute(self, env):
  77. config = Script.get_config()
  78. tmp_dir = Script.get_tmp_dir()
  79. report_file_handler_dict = {}
  80. #print "CONFIG: " + str(config)
  81. check_execute_list = config['commandParams']['check_execute_list']
  82. structured_output = {}
  83. # check each of the commands; if an unknown exception wasn't handled
  84. # by the functions, then produce a generic exit_code : 1
  85. if CHECK_JAVA_HOME in check_execute_list:
  86. try :
  87. java_home_check_structured_output = self.execute_java_home_available_check(config)
  88. structured_output[CHECK_JAVA_HOME] = java_home_check_structured_output
  89. except Exception, exception:
  90. print "There was an unexpected error while checking for the Java home location: " + str(exception)
  91. structured_output[CHECK_JAVA_HOME] = {"exit_code" : 1, "message": str(exception)}
  92. if CHECK_DB_CONNECTION in check_execute_list:
  93. try :
  94. db_connection_check_structured_output = self.execute_db_connection_check(config, tmp_dir)
  95. structured_output[CHECK_DB_CONNECTION] = db_connection_check_structured_output
  96. except Exception, exception:
  97. print "There was an unknown error while checking database connectivity: " + str(exception)
  98. structured_output[CHECK_DB_CONNECTION] = {"exit_code" : 1, "message": str(exception)}
  99. if CHECK_HOST_RESOLUTION in check_execute_list:
  100. try :
  101. host_resolution_structured_output = self.execute_host_resolution_check(config)
  102. structured_output[CHECK_HOST_RESOLUTION] = host_resolution_structured_output
  103. except Exception, exception :
  104. print "There was an unknown error while checking IP address lookups: " + str(exception)
  105. structured_output[CHECK_HOST_RESOLUTION] = {"exit_code" : 1, "message": str(exception)}
  106. if CHECK_LAST_AGENT_ENV in check_execute_list:
  107. try :
  108. last_agent_env_structured_output = self.execute_last_agent_env_check()
  109. structured_output[CHECK_LAST_AGENT_ENV] = last_agent_env_structured_output
  110. except Exception, exception :
  111. print "There was an unknown error while checking last host environment details: " + str(exception)
  112. structured_output[CHECK_LAST_AGENT_ENV] = {"exit_code" : 1, "message": str(exception)}
  113. # CHECK_INSTALLED_PACKAGES and CHECK_EXISTING_REPOS required to run together for
  114. # reasons of not doing the same common work twice for them as it takes some time, especially on Ubuntu.
  115. if CHECK_INSTALLED_PACKAGES in check_execute_list and CHECK_EXISTING_REPOS in check_execute_list:
  116. try :
  117. installed_packages, repos = self.execute_existing_repos_and_installed_packages_check(config)
  118. structured_output[CHECK_INSTALLED_PACKAGES] = installed_packages
  119. structured_output[CHECK_EXISTING_REPOS] = repos
  120. except Exception, exception :
  121. print "There was an unknown error while checking installed packages and existing repositories: " + str(exception)
  122. structured_output[CHECK_INSTALLED_PACKAGES] = {"exit_code" : 1, "message": str(exception)}
  123. structured_output[CHECK_EXISTING_REPOS] = {"exit_code" : 1, "message": str(exception)}
  124. # this is necessary for HostCleanup to know later what were the results.
  125. self.reportFileHandler.writeHostChecksCustomActionsFile(structured_output)
  126. self.put_structured_out(structured_output)
  127. def execute_existing_repos_and_installed_packages_check(self, config):
  128. installedPackages = []
  129. availablePackages = []
  130. packages_analyzer.allInstalledPackages(installedPackages)
  131. packages_analyzer.allAvailablePackages(availablePackages)
  132. repos = []
  133. packages_analyzer.getInstalledRepos(self.PACKAGES, installedPackages + availablePackages,
  134. self.IGNORE_PACKAGES_FROM_REPOS, repos)
  135. packagesInstalled = packages_analyzer.getInstalledPkgsByRepo(repos, self.IGNORE_PACKAGES, installedPackages)
  136. additionalPkgsInstalled = packages_analyzer.getInstalledPkgsByNames(
  137. self.ADDITIONAL_PACKAGES, installedPackages)
  138. allPackages = list(set(packagesInstalled + additionalPkgsInstalled))
  139. installedPackages = packages_analyzer.getPackageDetails(installedPackages, allPackages)
  140. repos = packages_analyzer.getReposToRemove(repos, self.IGNORE_REPOS)
  141. return installedPackages, repos
  142. def execute_java_home_available_check(self, config):
  143. print "Java home check started."
  144. java_home = config['commandParams']['java_home']
  145. print "Java home to check: " + java_home
  146. java_bin = "java"
  147. if OSCheck.is_windows_family():
  148. java_bin = "java.exe"
  149. if not os.path.isfile(os.path.join(java_home, "bin", java_bin)):
  150. print "Java home doesn't exist!"
  151. java_home_check_structured_output = {"exit_code" : 1, "message": "Java home doesn't exist!"}
  152. else:
  153. print "Java home exists!"
  154. java_home_check_structured_output = {"exit_code" : 0, "message": "Java home exists!"}
  155. return java_home_check_structured_output
  156. def execute_db_connection_check(self, config, tmp_dir):
  157. print "DB connection check started."
  158. # initialize needed data
  159. ambari_server_hostname = config['commandParams']['ambari_server_host']
  160. check_db_connection_jar_name = "DBConnectionVerification.jar"
  161. jdk_location = config['commandParams']['jdk_location']
  162. java_home = config['commandParams']['java_home']
  163. db_name = config['commandParams']['db_name']
  164. if db_name == DB_MYSQL:
  165. jdbc_url = jdk_location + JDBC_DRIVER_SYMLINK_MYSQL
  166. jdbc_driver = JDBC_DRIVER_MYSQL
  167. jdbc_name = JDBC_DRIVER_SYMLINK_MYSQL
  168. elif db_name == DB_ORACLE:
  169. jdbc_url = jdk_location + JDBC_DRIVER_SYMLINK_ORACLE
  170. jdbc_driver = JDBC_DRIVER_ORACLE
  171. jdbc_name = JDBC_DRIVER_SYMLINK_ORACLE
  172. elif db_name == DB_POSTGRESQL:
  173. jdbc_url = jdk_location + JDBC_DRIVER_SYMLINK_POSTGRESQL
  174. jdbc_driver = JDBC_DRIVER_POSTGRESQL
  175. jdbc_name = JDBC_DRIVER_SYMLINK_POSTGRESQL
  176. elif db_name == DB_MSSQL:
  177. jdbc_url = jdk_location + JDBC_DRIVER_SYMLINK_MSSQL
  178. jdbc_driver = JDBC_DRIVER_MSSQL
  179. jdbc_name = JDBC_DRIVER_SYMLINK_MSSQL
  180. db_connection_url = config['commandParams']['db_connection_url']
  181. user_name = config['commandParams']['user_name']
  182. user_passwd = config['commandParams']['user_passwd']
  183. agent_cache_dir = os.path.abspath(config["hostLevelParams"]["agentCacheDir"])
  184. check_db_connection_url = jdk_location + check_db_connection_jar_name
  185. jdbc_path = os.path.join(agent_cache_dir, jdbc_name)
  186. check_db_connection_path = os.path.join(agent_cache_dir, check_db_connection_jar_name)
  187. java_bin = "java"
  188. class_path_delimiter = ":"
  189. if OSCheck.is_windows_family():
  190. java_bin = "java.exe"
  191. class_path_delimiter = ";"
  192. java_exec = os.path.join(java_home, "bin",java_bin)
  193. if ('jdk_name' not in config['commandParams'] or config['commandParams']['jdk_name'] == None \
  194. or config['commandParams']['jdk_name'] == '') and not os.path.isfile(java_exec):
  195. message = "Custom java is not available on host. Please install it. Java home should be the same as on server. " \
  196. "\n"
  197. print message
  198. db_connection_check_structured_output = {"exit_code" : 1, "message": message}
  199. return db_connection_check_structured_output
  200. environment = { "no_proxy": format("{ambari_server_hostname}") }
  201. # download and install java if it doesn't exists
  202. if not os.path.isfile(java_exec):
  203. jdk_name = config['commandParams']['jdk_name']
  204. jdk_url = "{0}/{1}".format(jdk_location, jdk_name)
  205. jdk_download_target = os.path.join(agent_cache_dir, jdk_name)
  206. java_dir = os.path.dirname(java_home)
  207. try:
  208. download_file(jdk_url, jdk_download_target)
  209. except Exception, e:
  210. message = "Error downloading JDK from Ambari Server resources. Check network access to " \
  211. "Ambari Server.\n" + str(e)
  212. print message
  213. db_connection_check_structured_output = {"exit_code" : 1, "message": message}
  214. return db_connection_check_structured_output
  215. if jdk_name.endswith(".exe"):
  216. install_cmd = "{0} /s INSTALLDIR={1} STATIC=1 WEB_JAVA=0 /L \\var\\log\\ambari-agent".format(
  217. os_utils.quote_path(jdk_download_target), os_utils.quote_path(java_home),
  218. )
  219. install_path = [java_dir]
  220. try:
  221. Execute(install_cmd, path = install_path)
  222. except Exception, e:
  223. message = "Error installing java.\n" + str(e)
  224. print message
  225. db_connection_check_structured_output = {"exit_code" : 1, "message": message}
  226. return db_connection_check_structured_output
  227. else:
  228. tmp_java_dir = format("{tmp_dir}/jdk")
  229. sudo = AMBARI_SUDO_BINARY
  230. if jdk_name.endswith(".bin"):
  231. chmod_cmd = ("chmod", "+x", jdk_download_target)
  232. install_cmd = format("mkdir -p {tmp_java_dir} && cd {tmp_java_dir} && echo A | {jdk_download_target} -noregister && {sudo} cp -rp {tmp_java_dir}/* {java_dir}")
  233. elif jdk_name.endswith(".gz"):
  234. chmod_cmd = ("chmod","a+x", java_dir)
  235. install_cmd = format("mkdir -p {tmp_java_dir} && cd {tmp_java_dir} && tar -xf {jdk_download_target} && {sudo} cp -rp {tmp_java_dir}/* {java_dir}")
  236. try:
  237. Directory(java_dir)
  238. Execute(chmod_cmd, not_if = format("test -e {java_exec}"), sudo = True)
  239. Execute(install_cmd, not_if = format("test -e {java_exec}"))
  240. File(format("{java_home}/bin/java"), mode=0755, cd_access="a")
  241. Execute(("chown","-R", getpass.getuser(), java_home), sudo = True)
  242. except Exception, e:
  243. message = "Error installing java.\n" + str(e)
  244. print message
  245. db_connection_check_structured_output = {"exit_code" : 1, "message": message}
  246. return db_connection_check_structured_output
  247. # download DBConnectionVerification.jar from ambari-server resources
  248. try:
  249. download_file(check_db_connection_url, check_db_connection_path)
  250. except Exception, e:
  251. message = "Error downloading DBConnectionVerification.jar from Ambari Server resources. Check network access to " \
  252. "Ambari Server.\n" + str(e)
  253. print message
  254. db_connection_check_structured_output = {"exit_code" : 1, "message": message}
  255. return db_connection_check_structured_output
  256. # download jdbc driver from ambari-server resources
  257. try:
  258. download_file(jdbc_url, jdbc_path)
  259. if db_name == DB_MSSQL and OSCheck.is_windows_family():
  260. jdbc_auth_path = os.path.join(agent_cache_dir, JDBC_AUTH_SYMLINK_MSSQL)
  261. jdbc_auth_url = jdk_location + JDBC_AUTH_SYMLINK_MSSQL
  262. download_file(jdbc_auth_url, jdbc_auth_path)
  263. except Exception, e:
  264. message = format("Error: Ambari Server cannot download the database JDBC driver and is unable to test the " \
  265. "database connection. You must run ambari-server setup --jdbc-db={db_name} " \
  266. "--jdbc-driver=/path/to/your/{db_name}/driver.jar on the Ambari Server host to make the JDBC " \
  267. "driver available for download and to enable testing the database connection.\n") + str(e)
  268. print message
  269. db_connection_check_structured_output = {"exit_code" : 1, "message": message}
  270. return db_connection_check_structured_output
  271. # try to connect to db
  272. db_connection_check_command = format("{java_exec} -cp {check_db_connection_path}{class_path_delimiter}" \
  273. "{jdbc_path} -Djava.library.path={agent_cache_dir} org.apache.ambari.server.DBConnectionVerification \"{db_connection_url}\" " \
  274. "{user_name} {user_passwd!p} {jdbc_driver}")
  275. code, out = shell.call(db_connection_check_command)
  276. if code == 0:
  277. db_connection_check_structured_output = {"exit_code" : 0, "message": "DB connection check completed successfully!" }
  278. else:
  279. db_connection_check_structured_output = {"exit_code" : 1, "message": out }
  280. return db_connection_check_structured_output
  281. # check whether each host in the command can be resolved to an IP address
  282. def execute_host_resolution_check(self, config):
  283. print "IP address forward resolution check started."
  284. FORWARD_LOOKUP_REASON = "FORWARD_LOOKUP"
  285. failedCount = 0
  286. failures = []
  287. if config['commandParams']['hosts'] is not None :
  288. hosts = config['commandParams']['hosts'].split(",")
  289. successCount = len(hosts)
  290. else :
  291. successCount = 0
  292. hosts = ""
  293. socket.setdefaulttimeout(3)
  294. for host in hosts:
  295. try:
  296. host = host.strip()
  297. socket.gethostbyname(host)
  298. except socket.error,exception:
  299. successCount -= 1
  300. failedCount += 1
  301. failure = { "host": host, "type": FORWARD_LOOKUP_REASON,
  302. "cause": exception.args }
  303. failures.append(failure)
  304. if failedCount > 0 :
  305. message = "There were " + str(failedCount) + " host(s) that could not resolve to an IP address."
  306. else :
  307. message = "All hosts resolved to an IP address."
  308. print message
  309. host_resolution_check_structured_output = {
  310. "exit_code" : 0,
  311. "message" : message,
  312. "failed_count" : failedCount,
  313. "success_count" : successCount,
  314. "failures" : failures
  315. }
  316. return host_resolution_check_structured_output
  317. # computes and returns the host information of the agent
  318. def execute_last_agent_env_check(self):
  319. print "Last Agent Env check started."
  320. hostInfo = HostInfo()
  321. last_agent_env_check_structured_output = { }
  322. hostInfo.register(last_agent_env_check_structured_output)
  323. print "Last Agent Env check completed successfully."
  324. return last_agent_env_check_structured_output
  325. if __name__ == "__main__":
  326. CheckHost().execute()