alert_version_select.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 os
  18. import logging
  19. import socket
  20. import json
  21. from resource_management.libraries.script.script import Script
  22. from resource_management.libraries.functions.stack_select import unsafe_get_stack_versions
  23. RESULT_STATE_OK = 'OK'
  24. RESULT_STATE_WARNING = 'WARNING'
  25. RESULT_STATE_CRITICAL = 'CRITICAL'
  26. RESULT_STATE_UNKNOWN = 'UNKNOWN'
  27. STACK_TOOLS = '{{cluster-env/stack_tools}}'
  28. logger = logging.getLogger()
  29. def get_tokens():
  30. """
  31. Returns a tuple of tokens in the format {{site/property}} that will be used
  32. to build the dictionary passed into execute
  33. """
  34. return (STACK_TOOLS,)
  35. def execute(configurations={}, parameters={}, host_name=None):
  36. """
  37. Checks if the stack selector such as hdp-select can find versions installed on this host. E.g.,
  38. hdp-select versions
  39. Returns a tuple containing the result code and a pre-formatted result label
  40. Keyword arguments:
  41. configurations (dictionary): a mapping of configuration key to value
  42. parameters (dictionary): a mapping of script parameter key to value
  43. host_name (string): the name of this host where the alert is running
  44. """
  45. msg = []
  46. try:
  47. if configurations is None:
  48. return (RESULT_STATE_UNKNOWN, ['There were no configurations supplied to the script.'])
  49. # Check required properties
  50. if STACK_TOOLS not in configurations:
  51. return (RESULT_STATE_UNKNOWN, ['{0} is a required parameter for the script'.format(STACK_TOOLS)])
  52. # Of the form,
  53. # { "stack_selector": ["hdp-select", "/usr/bin/hdp-select", "hdp-select"], "conf_selector": ["conf-select", "/usr/bin/conf-select", "conf-select"] }
  54. stack_tools_str = configurations[STACK_TOOLS]
  55. if stack_tools_str is None:
  56. return (RESULT_STATE_UNKNOWN, ['{} is a required parameter for the script and the value is null'.format(STACK_TOOLS)])
  57. distro_select = "unknown-distro-select"
  58. try:
  59. stack_tools = json.loads(stack_tools_str)
  60. distro_select = stack_tools["stack_selector"][0]
  61. except:
  62. pass
  63. # This may not exist if the host does not contain any stack components,
  64. # or only contains components like Ambari Metrics and SmartSense
  65. stack_root_dir = Script.get_stack_root()
  66. if os.path.isdir(stack_root_dir):
  67. (code, out, versions) = unsafe_get_stack_versions()
  68. if code == 0:
  69. msg.append("Ok. {}".format(distro_select))
  70. if versions is not None and type(versions) is list and len(versions) > 0:
  71. msg.append("Versions: {}".format(", ".join(versions)))
  72. return (RESULT_STATE_OK, ["\n".join(msg)])
  73. else:
  74. msg.append("Failed, check dir {} for unexpected contents.".format(stack_root_dir))
  75. if out is not None:
  76. msg.append(out)
  77. return (RESULT_STATE_CRITICAL, ["\n".join(msg)])
  78. else:
  79. msg.append("Ok. No stack root {} to check.".format(stack_root_dir))
  80. return (RESULT_STATE_OK, ["\n".join(msg)])
  81. except Exception, e:
  82. return (RESULT_STATE_CRITICAL, [e.message])