generate_nagios_objects.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/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 urllib2
  18. import json
  19. import sys
  20. import base64
  21. import traceback
  22. SERVICE_DEFINITION = '''
  23. define service {
  24. use generic-service
  25. host_name %s
  26. normal_check_interval 1
  27. servicegroups AMBARI,%s
  28. service_description %s - %s
  29. check_command check_ambari_alert!%s!%s!%s!%s!%s!%s!%s
  30. }
  31. '''
  32. HOST_DEFINITION = '''
  33. define host{
  34. use linux-server
  35. hostgroups ambari-hosts
  36. host_name %s
  37. alias %s
  38. address %s
  39. }
  40. '''
  41. HOST_GROUP_DEFINITION = '''
  42. define hostgroup{
  43. hostgroup_name %s
  44. alias %s
  45. }
  46. '''
  47. ALERT_CHECK_COMMAND = '''
  48. define command {
  49. command_name check_ambari_alert
  50. command_line %s $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$
  51. }
  52. '''
  53. SERVICE_GROUP_DEFINITION = '''
  54. define servicegroup {
  55. servicegroup_name %s
  56. alias %s
  57. }
  58. '''
  59. try:
  60. host = raw_input("Enter ambari host: ")
  61. port = raw_input("Enter ambari port: ")
  62. cluster = raw_input("Enter ambari cluster: ")
  63. ssl = raw_input("Use SSL [true/false]: ")
  64. login = raw_input("Enter ambari login: ")
  65. password = raw_input("Enter ambari password: ")
  66. alerts_url = 'api/v1/clusters/{0}/alerts?fields=Alert/label,Alert/service_name,Alert/name,Alert/text,Alert/state'
  67. if ssl.lower() == 'true':
  68. protocol = 'https'
  69. else:
  70. protocol = 'http'
  71. url = '{0}://{1}:{2}/{3}'.format(protocol, host, port, alerts_url.format(cluster))
  72. admin_auth = base64.encodestring('%s:%s' % (login, password)).replace('\n', '')
  73. request = urllib2.Request(url)
  74. request.add_header('Authorization', 'Basic %s' % admin_auth)
  75. request.add_header('X-Requested-By', 'ambari')
  76. response = urllib2.urlopen(request)
  77. response_body = response.read()
  78. except Exception as ex:
  79. print "Error during Ambari Alerts data fetch: %s" % ex
  80. sys.exit(1)
  81. try:
  82. alerts = json.loads(response_body)['items']
  83. services = []
  84. service_groups = {'AMBARI': SERVICE_GROUP_DEFINITION % ('AMBARI', 'Ambari services group')}
  85. hosts = {host: HOST_DEFINITION % (host, 'Ambari server', host)}
  86. host_groups = [HOST_GROUP_DEFINITION % ('ambari-hosts', 'Ambari hosts')]
  87. for alert in alerts:
  88. service_name = alert['Alert']['service_name']
  89. label = alert['Alert']['label']
  90. name = alert['Alert']['name']
  91. alert_host = alert['Alert']['host_name']
  92. if alert_host is None:
  93. alert_host = host
  94. if alert_host not in hosts:
  95. hosts[alert_host] = HOST_DEFINITION % (alert_host, 'Ambari host - ' + alert_host, alert_host)
  96. if service_name not in service_groups:
  97. service_groups[service_name] = SERVICE_GROUP_DEFINITION % (service_name, service_name + ' services group')
  98. services.append(SERVICE_DEFINITION % (alert_host, service_name, service_name, label, host, port, cluster, protocol, login, base64.b64encode(password), name))
  99. except Exception as ex:
  100. print "Error during processing Ambari Alerts data: %s" % ex
  101. sys.exit(1)
  102. try:
  103. script_path = raw_input("Enter path to Ambari Alerts Plugin 'ambari_alerts.py': ")
  104. localhost_cfg = raw_input("Enter path to Nagios configuration file 'localhost.cfg' : ")
  105. commands_cfg = raw_input("Enter path to Nagios configuration file 'commands.cfg': ")
  106. with open(localhost_cfg, "a") as localhost_file:
  107. localhost_file.write("# Ambari Alerts HostGroups")
  108. for host_group in host_groups:
  109. localhost_file.write(host_group)
  110. localhost_file.write("# Ambari Alerts Hosts")
  111. for host_def in hosts.values():
  112. localhost_file.write(host_def)
  113. localhost_file.write("# Ambari Alerts ServiceGroups")
  114. for service_group in service_groups.values():
  115. localhost_file.write(service_group)
  116. localhost_file.write("# Ambari Alerts Services")
  117. for service in services:
  118. localhost_file.write(service)
  119. with open(commands_cfg, "a") as commands_file:
  120. commands_file.write(ALERT_CHECK_COMMAND % script_path)
  121. except Exception as ex:
  122. print "Error during creating Nagios objects for Ambari Alerts: %s" % ex
  123. sys.exit(1)