You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2014/12/11 19:33:59 UTC
ambari git commit: Alerts: Nagios Target for Notifications (Yurii
Shylov via jonathanhurley)
Repository: ambari
Updated Branches:
refs/heads/trunk da7643e51 -> aa696e0d8
Alerts: Nagios Target for Notifications (Yurii Shylov via jonathanhurley)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/aa696e0d
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/aa696e0d
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/aa696e0d
Branch: refs/heads/trunk
Commit: aa696e0d83364b39cff50725bbb020cc6ab536b5
Parents: da7643e
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Thu Dec 11 13:33:41 2014 -0500
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Thu Dec 11 13:33:49 2014 -0500
----------------------------------------------------------------------
.../package/deb/create_nagios_addon_deb.sh | 2 +
.../package/rpm/create_nagios_addon_rpm.sh | 1 +
contrib/nagios-alerts/plugins/ambari_alerts.py | 56 ++++++++
.../plugins/generate_nagios_objects.py | 130 +++++++++++++++++++
4 files changed, 189 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/aa696e0d/contrib/addons/package/deb/create_nagios_addon_deb.sh
----------------------------------------------------------------------
diff --git a/contrib/addons/package/deb/create_nagios_addon_deb.sh b/contrib/addons/package/deb/create_nagios_addon_deb.sh
index a64e045..35f3da7 100644
--- a/contrib/addons/package/deb/create_nagios_addon_deb.sh
+++ b/contrib/addons/package/deb/create_nagios_addon_deb.sh
@@ -41,12 +41,14 @@ PKG_FULL_NAME="${PKG_NAME}-$VERSION"
MON_TAR_DIR="${BUILD_DIR}/${PKG_FULL_NAME}/"
SRC_DIR="${BASEDIR}/../../src/addOns/nagios/"
+NAGIOS_ALERTS_DIR="${BASEDIR}/../../../nagios-alerts"
############ Mapping
mkdir -p "${MON_TAR_DIR}/usr/lib64/nagios"
cp -r "${SRC_DIR}/plugins" "${MON_TAR_DIR}/usr/lib64/nagios"
+cp -r "${NAGIOS_ALERTS_DIR}/plugins" "${MON_TAR_DIR}/usr/lib64/nagios"
mkdir -p "${MON_TAR_DIR}/etc/apache2"
cp -r "${SRC_DIR}/conf.d" "${MON_TAR_DIR}/etc/apache2"
http://git-wip-us.apache.org/repos/asf/ambari/blob/aa696e0d/contrib/addons/package/rpm/create_nagios_addon_rpm.sh
----------------------------------------------------------------------
diff --git a/contrib/addons/package/rpm/create_nagios_addon_rpm.sh b/contrib/addons/package/rpm/create_nagios_addon_rpm.sh
index 42a2e28..7f3e201 100644
--- a/contrib/addons/package/rpm/create_nagios_addon_rpm.sh
+++ b/contrib/addons/package/rpm/create_nagios_addon_rpm.sh
@@ -42,6 +42,7 @@ MON_TAR_DIR="${BUILD_DIR}/${PKG_NAME}-$VERSION/"
mkdir -p "${MON_TAR_DIR}"
cp -r ${BASEDIR}/../../src/addOns/nagios/* ${MON_TAR_DIR}
+cp -r ${BASEDIR}/../../../nagios-alerts/* ${MON_TAR_DIR}
TAR_DEST="${BUILD_DIR}/${PKG_NAME}-$VERSION.tar.gz"
http://git-wip-us.apache.org/repos/asf/ambari/blob/aa696e0d/contrib/nagios-alerts/plugins/ambari_alerts.py
----------------------------------------------------------------------
diff --git a/contrib/nagios-alerts/plugins/ambari_alerts.py b/contrib/nagios-alerts/plugins/ambari_alerts.py
new file mode 100644
index 0000000..182bf3d
--- /dev/null
+++ b/contrib/nagios-alerts/plugins/ambari_alerts.py
@@ -0,0 +1,56 @@
+#!/usr/bin/python
+
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+import urllib2
+import json
+import sys
+import base64
+
+try:
+ host = sys.argv[1]
+ port = sys.argv[2]
+ cluster = sys.argv[3]
+ protocol = sys.argv[4]
+ login = sys.argv[5]
+ password = base64.b64decode(sys.argv[6])
+ name = sys.argv[7]
+ alerts_url = 'api/v1/clusters/{0}/alerts?fields=Alert/label,Alert/service_name,Alert/name,Alert/text,Alert/state&Alert/name={1}'.format(cluster, name)
+ url = '{0}://{1}:{2}/{3}'.format(protocol, host, port, alerts_url)
+ admin_auth = base64.encodestring('%s:%s' % (login, password)).replace('\n', '')
+ request = urllib2.Request(url)
+ request.add_header('Authorization', 'Basic %s' % admin_auth)
+ request.add_header('X-Requested-By', 'ambari')
+ response = urllib2.urlopen(request)
+ response_body = response.read()
+ alert = json.loads(response_body)['items'][0]
+ state = alert['Alert']['state']
+ text = alert['Alert']['text']
+except Exception as exc:
+ text = 'Unable to retrieve alert info: %s' % exc
+ state = 'UNKNOWN'
+finally:
+ print text
+ exit_code = {
+ 'OK': 0,
+ 'WARNING': 1,
+ 'CRITICAL': 2,
+ 'UNKNOWN': 3,
+ }.get(state, 3)
+ sys.exit(exit_code)
http://git-wip-us.apache.org/repos/asf/ambari/blob/aa696e0d/contrib/nagios-alerts/plugins/generate_nagios_objects.py
----------------------------------------------------------------------
diff --git a/contrib/nagios-alerts/plugins/generate_nagios_objects.py b/contrib/nagios-alerts/plugins/generate_nagios_objects.py
new file mode 100644
index 0000000..6c00402
--- /dev/null
+++ b/contrib/nagios-alerts/plugins/generate_nagios_objects.py
@@ -0,0 +1,130 @@
+#!/usr/bin/python
+
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+import urllib2
+import json
+import sys
+import base64
+import traceback
+
+SERVICE_DEFINITION = '''
+define service {
+ use generic-service
+ host_name %s
+ normal_check_interval 1
+ servicegroups AMBARI,%s
+ service_description %s - %s
+ check_command check_ambari_alert!%s!%s!%s!%s!%s!%s!%s
+}
+
+'''
+HOST_DEFINITION = '''
+define host{
+ use linux-server
+ hostgroups ambari-hosts
+ host_name %s
+ alias %s
+ address %s
+ }
+'''
+HOST_GROUP_DEFINITION = '''
+define hostgroup{
+ hostgroup_name %s
+ alias %s
+ }
+'''
+ALERT_CHECK_COMMAND = '''
+define command {
+ command_name check_ambari_alert
+ command_line %s $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$
+}
+'''
+SERVICE_GROUP_DEFINITION = '''
+define servicegroup {
+ servicegroup_name %s
+ alias %s
+}
+'''
+
+try:
+ host = raw_input("Enter ambari host: ")
+ port = raw_input("Enter ambari port: ")
+ cluster = raw_input("Enter ambari cluster: ")
+ ssl = raw_input("Use SSL [true/false]: ")
+ login = raw_input("Enter ambari login: ")
+ password = raw_input("Enter ambari password: ")
+ alerts_url = 'api/v1/clusters/{0}/alerts?fields=Alert/label,Alert/service_name,Alert/name,Alert/text,Alert/state'
+ if ssl.lower() == 'true':
+ protocol = 'https'
+ else:
+ protocol = 'http'
+ url = '{0}://{1}:{2}/{3}'.format(protocol, host, port, alerts_url.format(cluster))
+ admin_auth = base64.encodestring('%s:%s' % (login, password)).replace('\n', '')
+ request = urllib2.Request(url)
+ request.add_header('Authorization', 'Basic %s' % admin_auth)
+ request.add_header('X-Requested-By', 'ambari')
+ response = urllib2.urlopen(request)
+ response_body = response.read()
+except Exception as ex:
+ print "Error during Ambari Alerts data fetch: %s" % ex
+ sys.exit(1)
+try:
+ alerts = json.loads(response_body)['items']
+ services = []
+ service_groups = {'AMBARI': SERVICE_GROUP_DEFINITION % ('AMBARI', 'Ambari services group')}
+ hosts = {host: HOST_DEFINITION % (host, 'Ambari server', host)}
+ host_groups = [HOST_GROUP_DEFINITION % ('ambari-hosts', 'Ambari hosts')]
+ for alert in alerts:
+ service_name = alert['Alert']['service_name']
+ label = alert['Alert']['label']
+ name = alert['Alert']['name']
+ alert_host = alert['Alert']['host_name']
+ if alert_host is None:
+ alert_host = host
+ if alert_host not in hosts:
+ hosts[alert_host] = HOST_DEFINITION % (alert_host, 'Ambari host - ' + alert_host, alert_host)
+ if service_name not in service_groups:
+ service_groups[service_name] = SERVICE_GROUP_DEFINITION % (service_name, service_name + ' services group')
+ services.append(SERVICE_DEFINITION % (alert_host, service_name, service_name, label, host, port, cluster, protocol, login, base64.b64encode(password), name))
+except Exception as ex:
+ print "Error during processing Ambari Alerts data: %s" % ex
+ sys.exit(1)
+try:
+ script_path = raw_input("Enter path to Ambari Alerts Plugin 'ambari_alerts.py': ")
+ localhost_cfg = raw_input("Enter path to Nagios configuration file 'localhost.cfg' : ")
+ commands_cfg = raw_input("Enter path to Nagios configuration file 'commands.cfg': ")
+ with open(localhost_cfg, "a") as localhost_file:
+ localhost_file.write("# Ambari Alerts HostGroups")
+ for host_group in host_groups:
+ localhost_file.write(host_group)
+ localhost_file.write("# Ambari Alerts Hosts")
+ for host_def in hosts.values():
+ localhost_file.write(host_def)
+ localhost_file.write("# Ambari Alerts ServiceGroups")
+ for service_group in service_groups.values():
+ localhost_file.write(service_group)
+ localhost_file.write("# Ambari Alerts Services")
+ for service in services:
+ localhost_file.write(service)
+ with open(commands_cfg, "a") as commands_file:
+ commands_file.write(ALERT_CHECK_COMMAND % script_path)
+except Exception as ex:
+ print "Error during creating Nagios objects for Ambari Alerts: %s" % ex
+ sys.exit(1)