ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject [3/3] ambari git commit: AMBARI-14538. Stale alerts missing host name(s) (dlysnichenko)
Date Tue, 05 Jan 2016 14:40:01 GMT
AMBARI-14538. Stale alerts missing host name(s) (dlysnichenko)


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

Branch: refs/heads/trunk
Commit: 7f291622570900875e15a52ea839b05c037861d1
Parents: 09b55df
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Tue Jan 5 16:39:48 2016 +0200
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Tue Jan 5 16:40:18 2016 +0200

----------------------------------------------------------------------
 .../server/alerts/StaleAlertRunnable.java       | 55 ++++++++++++++++++--
 1 file changed, 50 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/7f291622/ambari-server/src/main/java/org/apache/ambari/server/alerts/StaleAlertRunnable.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/alerts/StaleAlertRunnable.java
b/ambari-server/src/main/java/org/apache/ambari/server/alerts/StaleAlertRunnable.java
index 206b2ca..83abc8e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/alerts/StaleAlertRunnable.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/alerts/StaleAlertRunnable.java
@@ -18,6 +18,7 @@
 package org.apache.ambari.server.alerts;
 
 import java.text.MessageFormat;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -75,6 +76,10 @@ public class StaleAlertRunnable implements Runnable {
    */
   private static final String STALE_ALERTS_MSG = "There are {0} stale alerts from {1} host(s):
{2}";
 
+  private static final String TIMED_LABEL_MSG = "{0} ({1})";
+
+  private static final String HOST_LABEL_MSG = "{0}[{1}]";
+
   /**
    * Convert the minutes for the delay of an alert into milliseconds.
    */
@@ -130,6 +135,7 @@ public class StaleAlertRunnable implements Runnable {
 
         long now = System.currentTimeMillis();
         Set<String> staleAlerts = new HashSet<String>();
+        Map<String, Set<String>> staleHostAlerts = new HashMap<>();
         Set<String> hostsWithStaleAlerts = new HashSet<String>();
 
         // get the cluster's current alerts
@@ -168,16 +174,28 @@ public class StaleAlertRunnable implements Runnable {
           // if the last time it was run is >= 2x the interval, it's stale
           long timeDifference = now - current.getLatestTimestamp();
           if (timeDifference >= 2 * intervalInMillis) {
-            // keep track of the definition
-            staleAlerts.add(definition.getLabel());
-
-            // keek track of the host, if not null
+            String label = definition.getLabel();
             if (null != history.getHostName()) {
-              hostsWithStaleAlerts.add( history.getHostName() );
+              // keep track of the host, if not null
+              String hostName = history.getHostName();
+              hostsWithStaleAlerts.add(hostName);
+              if(!staleHostAlerts.containsKey(hostName)) {
+                staleHostAlerts.put(hostName, new HashSet<String>());
+              }
+
+              staleHostAlerts.get(hostName).add(MessageFormat.format(TIMED_LABEL_MSG, label,
+                  millisToHumanReadableStr(timeDifference)));
+            } else {
+              // non host alerts
+              staleAlerts.add(label);
             }
           }
         }
 
+        for(String host : staleHostAlerts.keySet()) {
+          staleAlerts.add(MessageFormat.format(HOST_LABEL_MSG, host, StringUtils.join(staleHostAlerts.get(host),
", ")));
+        }
+
         AlertState alertState = AlertState.OK;
         String alertText = ALL_ALERTS_CURRENT_MSG;
 
@@ -209,4 +227,31 @@ public class StaleAlertRunnable implements Runnable {
           exception);
     }
   }
+
+  private static final long MILLISECONDS_PER_MINUTE = 1000l * 60l;
+  private static final int MINUTES_PER_DAY = 24 * 60;
+  private static final int MINUTES_PER_HOUR = 60;
+
+  /**
+   * Converts given {@code milliseconds} to human-readable {@link String} like "1d 2h 3m"
or "2h 4m".
+   * @param milliseconds milliseconds to convert
+   * @return human-readable string
+   */
+  private static String millisToHumanReadableStr(long milliseconds){
+    int min, hour, days;
+    min = (int)(milliseconds / MILLISECONDS_PER_MINUTE);
+    days = min / MINUTES_PER_DAY;
+    min = min % MINUTES_PER_DAY;
+    hour = min / MINUTES_PER_HOUR;
+    min = min % MINUTES_PER_HOUR;
+    String result = "";
+    if(days > 0)
+      result += days + "d ";
+    if(hour > 0)
+      result += hour + "h ";
+    if(min > 0)
+      result += min + "m ";
+    return result.trim();
+  }
 }
+


Mime
View raw message