瀏覽代碼

AMBARI-5057. Maintenance info not passed on Nagios restart like START/STOP does (ncole)

Nate Cole 11 年之前
父節點
當前提交
e40bf7090a

+ 10 - 0
ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariCustomCommandExecutionHelper.java

@@ -21,6 +21,7 @@ package org.apache.ambari.server.controller;
 import com.google.gson.Gson;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.Role;
 import org.apache.ambari.server.RoleCommand;
@@ -52,6 +53,7 @@ import org.apache.ambari.server.utils.StageUtils;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -60,6 +62,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
+
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.COMMAND_TIMEOUT;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.COMPONENT_CATEGORY;
 import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.CUSTOM_COMMAND;
@@ -299,6 +302,13 @@ public class AmbariCustomCommandExecutionHelper {
       }
       roleParams.put(COMPONENT_CATEGORY, componentInfo.getCategory());
       execCmd.setRoleParams(roleParams);
+      
+      // if the target is NAGIOS (for example: restart command), make passive info always available
+      if (execCmd.getRole().equals(Role.NAGIOS_SERVER.name())) {
+        execCmd.setPassiveInfo(
+          MaintenanceStateHelper.getMaintenanceHostComponents(clusters, cluster));
+      }
+      
     }
   }
 

+ 3 - 2
ambari-server/src/main/java/org/apache/ambari/server/controller/MaintenanceStateHelper.java

@@ -119,6 +119,8 @@ public class MaintenanceStateHelper {
   public static Set<Map<String, String>> getMaintenanceHostComponents(Clusters clusters, Cluster cluster) throws AmbariException {
     
     Set<Map<String, String>> set = new HashSet<Map<String, String>>();
+
+    Map<String, Host> hosts = clusters.getHostsForCluster(cluster.getClusterName());
     
     for (Service service : cluster.getServices().values()) {
       for (ServiceComponent sc : service.getServiceComponents().values()) {
@@ -126,8 +128,7 @@ public class MaintenanceStateHelper {
           continue;
 
         for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
-          Host host = clusters.getHostsForCluster(
-              cluster.getClusterName()).get(sch.getHostName());
+          Host host = hosts.get(sch.getHostName());
           
           if (MaintenanceState.OFF != getEffectiveState(cluster.getClusterId(),
               service, host, sch)) {

+ 7 - 0
ambari-server/src/main/resources/stacks/HDP/2.0.6/services/NAGIOS/package/scripts/nagios_server.py

@@ -79,12 +79,14 @@ def update_ignorable(params):
     return
   else:
     buf = ""
+    count = 0
     for define in params.config['passiveInfo']:
       try:
         host = str(define['host'])
         service = str(define['service'])
         component = str(define['component'])
         buf += host + " " + service + " " + component + "\n"
+        count += 1
       except KeyError:
         pass
 
@@ -92,7 +94,12 @@ def update_ignorable(params):
     try:
       f = open('/var/nagios/ignore.dat', 'w')
       f.write(buf)
+      if 1 == count:
+        Logger.info("Persisted '/var/nagios/ignore.dat' with 1 entry")
+      elif count > 1:
+        Logger.info("Persisted '/var/nagios/ignore.dat' with " + str(count) + " entries")
     except:
+      Logger.info("Could not persist '/var/nagios/ignore.dat'")
       pass
     finally:
       if f is not None:

+ 99 - 0
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java

@@ -4008,8 +4008,107 @@ public class AmbariManagementControllerTest {
     Assert.assertTrue(hostParams.containsKey(ExecutionCommand.KeyNames.MYSQL_JDBC_URL));
     Assert.assertTrue(hostParams.containsKey(ExecutionCommand.KeyNames.ORACLE_JDBC_URL));
     Assert.assertEquals("CLIENT", roleParams.get(ExecutionCommand.KeyNames.COMPONENT_CATEGORY));
+    
+    // verify passive info is not passed when not NAGIOS
+    Assert.assertNull(hrc.getExecutionCommandWrapper().getExecutionCommand().getPassiveInfo());
   }
 
+  @Test
+  public void testPassiveSentWithNagiosRestart() throws AmbariException {
+    setupClusterWithHosts("c1", "HDP-2.0.7", Arrays.asList("h1"), "centos5");
+
+    Cluster cluster = clusters.getCluster("c1");
+    cluster.setDesiredStackVersion(new StackId("HDP-2.0.7"));
+    cluster.setCurrentStackVersion(new StackId("HDP-2.0.7"));
+
+    Service hdfs = cluster.addService("HDFS");
+    hdfs.persist();
+    hdfs.addServiceComponent(Role.HDFS_CLIENT.name()).persist();
+    hdfs.addServiceComponent(Role.NAMENODE.name()).persist();
+    hdfs.addServiceComponent(Role.DATANODE.name()).persist();
+    
+    hdfs.getServiceComponent(Role.HDFS_CLIENT.name()).addServiceComponentHost("h1").persist();
+    hdfs.getServiceComponent(Role.NAMENODE.name()).addServiceComponentHost("h1").persist();
+    hdfs.getServiceComponent(Role.DATANODE.name()).addServiceComponentHost("h1").persist();
+    
+
+    
+    Service nagios = cluster.addService("NAGIOS");
+    nagios.persist();
+    nagios.addServiceComponent(Role.NAGIOS_SERVER.name()).persist();
+    nagios.getServiceComponent(Role.NAGIOS_SERVER.name()).addServiceComponentHost("h1").persist();
+    
+    installService("c1", "HDFS", false, false);
+    installService("c1", "NAGIOS", false, false);
+
+    startService("c1", "HDFS", false, false);
+    startService("c1", "NAGIOS", false, false);
+
+    // set this after starting - setting it before will skip it due to rules
+    // around bulk starts
+    hdfs.getServiceComponent(Role.DATANODE.name()).getServiceComponentHost(
+        "h1").setMaintenanceState(MaintenanceState.ON);    
+    
+    Cluster c = clusters.getCluster("c1");
+    Service s = c.getService("HDFS");
+
+    Assert.assertEquals(State.STARTED, s.getDesiredState());
+    for (ServiceComponent sc : s.getServiceComponents().values()) {
+      for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
+        if (sc.isClientComponent()) {
+          Assert.assertEquals(State.INSTALLED, sch.getDesiredState());
+        } else {
+          Assert.assertEquals(State.STARTED, sch.getDesiredState());
+        }
+      }
+    }
+
+    Map<String, String> params = new HashMap<String, String>() {{
+      put("test", "test");
+    }};
+    RequestResourceFilter resourceFilter = new RequestResourceFilter(
+      "NAGIOS",
+      "NAGIOS_SERVER",
+      new ArrayList<String>() {{ add("h1"); }});
+    ExecuteActionRequest actionRequest = new ExecuteActionRequest("c1",
+      "RESTART", params);
+    actionRequest.getResourceFilters().add(resourceFilter);
+
+    Map<String, String> requestProperties = new HashMap<String, String>();
+    requestProperties.put(REQUEST_CONTEXT_PROPERTY, "Called from a test");
+
+    RequestStatusResponse response = controller.createAction(actionRequest, requestProperties);
+
+    List<Stage> stages = actionDB.getAllStages(response.getRequestId());
+    Assert.assertNotNull(stages);
+
+    HostRoleCommand hrc = null;
+    for (Stage stage : stages) {
+      for (HostRoleCommand cmd : stage.getOrderedHostRoleCommands()) {
+        if (cmd.getRole().equals(Role.NAGIOS_SERVER)) {
+          hrc = cmd;
+        }
+      }
+    }
+    Assert.assertNotNull(hrc);
+    Assert.assertEquals("RESTART NAGIOS/NAGIOS_SERVER", hrc.getCommandDetail());
+
+    
+    Set<Map<String, String>> pi =
+        hrc.getExecutionCommandWrapper().getExecutionCommand().getPassiveInfo();
+    
+    Assert.assertNotNull(pi);
+    Assert.assertTrue(pi.size() > 0);
+    Map<String, String> map = pi.iterator().next();
+    Assert.assertTrue(map.containsKey("host"));
+    Assert.assertTrue(map.containsKey("service"));
+    Assert.assertTrue(map.containsKey("component"));
+    Assert.assertEquals("h1", map.get("host"));
+    Assert.assertEquals("HDFS", map.get("service"));
+    Assert.assertEquals("DATANODE", map.get("component"));
+  }  
+  
+  
   @SuppressWarnings("serial")
   @Test
   public void testCreateActionsFailures() throws Exception {