AMBARI-21712. Allow WEB alert accept custom HTTP codes (echekanskiy)

Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/12c05880
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/12c05880
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/12c05880

Branch: refs/heads/branch-feature-logsearch-ui
Commit: 12c05880d4a5fa13f37b58f537bebb292a064d20
Parents: f2379a4
Author: Eugene Chekanskiy <echekans...@apache.org>
Authored: Wed Aug 16 19:15:26 2017 +0300
Committer: Eugene Chekanskiy <echekans...@apache.org>
Committed: Wed Aug 16 19:15:26 2017 +0300

----------------------------------------------------------------------
 .../python/ambari_agent/alerts/base_alert.py     | 15 +++++++++++----
 .../main/python/ambari_agent/alerts/web_alert.py |  4 ++++
 .../src/test/python/ambari_agent/TestAlerts.py   | 19 ++++++++++++++++++-
 .../ambari/server/state/alert/AlertUri.java      | 15 +++++++++++++++
 4 files changed, 48 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/12c05880/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py 
b/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
index add29fc..05f8023 100644
--- a/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
+++ b/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
@@ -255,7 +255,8 @@ class BaseAlert(object):
     
     if uri_structure is None:
       return None
-    
+
+    acceptable_codes_key = None
     http_key = None
     https_key = None
     https_property_key = None
@@ -267,7 +268,10 @@ class BaseAlert(object):
     ha_alias_key = None
     ha_http_pattern = None
     ha_https_pattern = None
-    
+
+    if 'acceptable_codes' in uri_structure:
+      acceptable_codes_key = uri_structure['acceptable_codes']
+
     if 'http' in uri_structure:
       http_key = uri_structure['http']
     
@@ -306,11 +310,14 @@ class BaseAlert(object):
 
 
     AlertUriLookupKeys = namedtuple('AlertUriLookupKeys', 
-      'http https https_property https_property_value default_port '
+      'acceptable_codes http https https_property https_property_value 
default_port '
       'kerberos_keytab kerberos_principal '
       'ha_nameservice ha_alias_key ha_http_pattern ha_https_pattern')
     
-    alert_uri_lookup_keys = AlertUriLookupKeys(http=http_key, https=https_key, 
+    alert_uri_lookup_keys = AlertUriLookupKeys(
+      acceptable_codes=acceptable_codes_key,
+      http=http_key,
+      https=https_key,
       https_property=https_property_key,
       https_property_value=https_property_value_key, default_port=default_port,
       kerberos_keytab=kerberos_keytab, kerberos_principal=kerberos_principal,

http://git-wip-us.apache.org/repos/asf/ambari/blob/12c05880/ambari-agent/src/main/python/ambari_agent/alerts/web_alert.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/alerts/web_alert.py 
b/ambari-agent/src/main/python/ambari_agent/alerts/web_alert.py
index 8ce4405..0e400f7 100644
--- a/ambari-agent/src/main/python/ambari_agent/alerts/web_alert.py
+++ b/ambari-agent/src/main/python/ambari_agent/alerts/web_alert.py
@@ -107,6 +107,10 @@ class WebAlert(BaseAlert):
     if status_code == 0:
       return (self.RESULT_CRITICAL, [status_code, url, time_seconds, 
error_message])
 
+    # check explicit listed codes
+    if self.uri_property_keys.acceptable_codes and status_code in 
self.uri_property_keys.acceptable_codes:
+      return (self.RESULT_OK, [status_code, url, time_seconds])
+
     # anything that's less than 400 is OK
     if status_code < 400:
       return (self.RESULT_OK, [status_code, url, time_seconds])

http://git-wip-us.apache.org/repos/asf/ambari/blob/12c05880/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestAlerts.py 
b/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
index 64479a2..1226900 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
@@ -23,6 +23,7 @@ import socket
 import sys
 import urllib2
 import tempfile
+import random
 from alerts.ams_alert import AmsAlert
 
 from ambari_agent.AlertSchedulerHandler import AlertSchedulerHandler
@@ -616,6 +617,21 @@ class TestAlerts(TestCase):
     self.assertEquals('CRITICAL', alerts[0]['state'])
     self.assertEquals('(Unit Tests) critical: 
https://c6401.ambari.apache.org:443/test/path. error message', 
alerts[0]['text'])
 
+    # test custom codes
+    code = random.choice((600, 700, 800))
+    wa_make_web_request_mock.return_value = WebResponse(code, 1.234 , "Custom 
Code")
+    collector = AlertCollector()
+    alert = WebAlert(definition_json, definition_json['source'], self.config)
+    alert.set_helpers(collector, cluster_configuration)
+    alert.set_cluster("c1", "c6401.ambari.apache.org")
+    alert.collect()
+
+    alerts = collector.alerts()
+    self.assertEquals(0, len(collector.alerts()))
+
+    self.assertEquals('OK', alerts[0]['state'])
+    self.assertEquals('(Unit Tests) ok: {code}'.format(code=code), 
alerts[0]['text'])
+
   def test_reschedule(self):
     test_file_path = os.path.join('ambari_agent', 'dummy_files')
     test_stack_path = os.path.join('ambari_agent', 'dummy_files')
@@ -1559,7 +1575,8 @@ class TestAlerts(TestCase):
           "https": "{{hdfs-site/dfs.datanode.https.address}}",
           "https_property": "{{hdfs-site/dfs.http.policy}}",
           "https_property_value": "HTTPS_ONLY",
-          "connection_timeout": 5.678
+          "connection_timeout": 5.678,
+          "acceptable_codes": [600, 700, 800]
         },
         "reporting": {
           "ok": {

http://git-wip-us.apache.org/repos/asf/ambari/blob/12c05880/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertUri.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertUri.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertUri.java
index 8223db5..93801d5 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertUri.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/alert/AlertUri.java
@@ -17,6 +17,8 @@
  */
 package org.apache.ambari.server.state.alert;
 
+import java.util.Set;
+
 import com.google.gson.annotations.SerializedName;
 
 /**
@@ -28,6 +30,11 @@ import com.google.gson.annotations.SerializedName;
  * URI.
  */
 public class AlertUri {
+  /**
+   * The HTTP URI to use.
+   */
+  @SerializedName("acceptable_codes")
+  private Set<Integer> m_acceptableCodes;
 
   /**
    * The HTTP URI to use.
@@ -181,6 +188,14 @@ public class AlertUri {
     return m_highAvailability;
   }
 
+  public Set<Integer> getAcceptableCodes() {
+    return m_acceptableCodes;
+  }
+
+  public void setAcceptableCodes(Set<Integer> m_acceptableCodes) {
+    this.m_acceptableCodes = m_acceptableCodes;
+  }
+
   /**
    * The {@link HighAvailability} structure is used to hold information about
    * how HA URIs are constructed if the service supports HA mode. For example

Reply via email to