Repository: ambari
Updated Branches:
  refs/heads/branch-2.6 196e6ed04 -> 805de45cd


AMBARI-18543. Option to enable request/access logs in Ambari. (amarnath reddy 
pappu via swagle)


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

Branch: refs/heads/branch-2.6
Commit: 805de45cd1d356752209c8f29a39625126837aaa
Parents: 196e6ed
Author: Siddharth Wagle <swa...@hortonworks.com>
Authored: Thu Sep 7 10:44:06 2017 -0700
Committer: Siddharth Wagle <swa...@hortonworks.com>
Committed: Thu Sep 7 10:44:06 2017 -0700

----------------------------------------------------------------------
 .../server/configuration/Configuration.java     | 23 +++++++++
 .../ambari/server/controller/AmbariServer.java  | 50 +++++++++++++++++++-
 2 files changed, 72 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/805de45c/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
index 7567755..e51a9b2 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
@@ -1779,6 +1779,27 @@ public class Configuration {
       "server.tmp.dir", AmbariPath.getPath("/var/lib/ambari-server/tmp"));
 
   /**
+   * Request logs path .
+   */
+  @Markdown(description = "The location on the Ambari Server where request 
logs can be created.")
+  public static final ConfigurationProperty<String> REQUEST_LOGPATH = new 
ConfigurationProperty<>(
+      "server.requestlogs.path", null);
+
+  /**
+   * The pattern of request  logs .
+   */
+  @Markdown(description = "The pattern of request log file name")
+  public static final ConfigurationProperty<String> REQUEST_LOGNAMEPATTERN = 
new ConfigurationProperty<>(
+          "server.requestlogs.namepattern", "ambari-access-yyyy_mm_dd.log");
+
+  /**
+   * The number of days request logs can be retained.
+   */
+  @Markdown(description = "The number of days that request log would be 
retained.")
+  public static final ConfigurationProperty<Integer> REQUEST_LOG_RETAINDAYS = 
new ConfigurationProperty<>(
+          "server.requestlogs.retaindays", 15);
+
+  /**
    * The time, in {@link TimeUnit#MILLISECONDS}, until an external script is 
killed.
    */
   @Markdown(description = "The time, in milliseconds, until an external script 
is killed.")
@@ -2997,6 +3018,8 @@ public class Configuration {
     configsMap.put(AGENT_PACKAGE_PARALLEL_COMMANDS_LIMIT.getKey(), 
getProperty(AGENT_PACKAGE_PARALLEL_COMMANDS_LIMIT));
     configsMap.put(PROXY_ALLOWED_HOST_PORTS.getKey(), 
getProperty(PROXY_ALLOWED_HOST_PORTS));
     configsMap.put(TLS_EPHEMERAL_DH_KEY_SIZE.getKey(), 
getProperty(TLS_EPHEMERAL_DH_KEY_SIZE));
+    configsMap.put(REQUEST_LOGPATH.getKey(), getProperty(REQUEST_LOGPATH));
+    configsMap.put(REQUEST_LOG_RETAINDAYS.getKey(), 
getProperty(REQUEST_LOG_RETAINDAYS));
 
     File passFile = new File(
         configsMap.get(SRVR_KSTR_DIR.getKey()) + File.separator

http://git-wip-us.apache.org/repos/asf/ambari/blob/805de45c/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
index 1ebcac2..b2a68d6 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
@@ -126,9 +126,13 @@ import org.apache.ambari.server.view.ViewThrottleFilter;
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.PropertyConfigurator;
 import org.apache.velocity.app.Velocity;
+import org.eclipse.jetty.server.Handler;
+import org.eclipse.jetty.server.NCSARequestLog;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.SessionIdManager;
 import org.eclipse.jetty.server.SessionManager;
+import org.eclipse.jetty.server.handler.HandlerCollection;
+import org.eclipse.jetty.server.handler.RequestLogHandler;
 import org.eclipse.jetty.server.nio.SelectChannelConnector;
 import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
 import org.eclipse.jetty.servlet.DefaultServlet;
@@ -161,7 +165,6 @@ import com.google.inject.name.Named;
 import com.google.inject.persist.Transactional;
 import com.sun.jersey.spi.container.servlet.ServletContainer;
 
-
 @Singleton
 public class AmbariServer {
   public static final String VIEWS_URL_PATTERN = "/api/v1/views/*";
@@ -453,6 +456,9 @@ public class AmbariServer {
 
       enableLog4jMonitor(configsMap);
 
+      //Check and load requestlog handler.
+      loadRequestlogHandler(handlerList, serverForAgent, configsMap);
+
       handlerList.addHandler(root);
       server.setHandler(handlerList);
 
@@ -988,6 +994,48 @@ public class AmbariServer {
     }
   }
 
+  /**
+   * For loading requestlog handlers
+   */
+  private static void loadRequestlogHandler(AmbariHandlerList handlerList, 
Server serverForAgent , Map<String, String> configsMap) {
+
+    //Example:  /var/log/ambari-server/ambari-server-access-yyyy_mm_dd.log
+    String requestlogpath =  
configsMap.get(Configuration.REQUEST_LOGPATH.getKey());
+
+    //Request logs can be disable by removing the property from 
ambari.properties file
+    if(!StringUtils.isBlank(requestlogpath)) {
+      String logfullpath = requestlogpath + "//" + 
Configuration.REQUEST_LOGNAMEPATTERN.getDefaultValue();
+      LOG.info("********* Initializing request access log: " + logfullpath);
+      RequestLogHandler requestLogHandler = new RequestLogHandler();
+
+      NCSARequestLog requestLog = new NCSARequestLog(requestlogpath);
+
+      String retaindays = 
configsMap.get(Configuration.REQUEST_LOG_RETAINDAYS.getKey());
+      int retaindaysInt = 
Configuration.REQUEST_LOG_RETAINDAYS.getDefaultValue();
+      if(retaindays != null && !StringUtils.isBlank(retaindays)) {
+        retaindaysInt = Integer.parseInt(retaindays.trim());
+      }
+
+      requestLog.setRetainDays(retaindaysInt);
+      requestLog.setAppend(true);
+      requestLog.setLogLatency(true);
+      requestLog.setExtended(true);
+      requestLogHandler.setRequestLog(requestLog);
+      //Add requestloghandler to existing handlerlist.
+      handlerList.addHandler(requestLogHandler);
+
+      //For agent communication.
+      HandlerCollection handlers = new HandlerCollection();
+      Handler[] handler = serverForAgent.getHandlers();
+      if(handler != null ) {
+        handlers.setHandlers((Handler[])handler);
+        handlers.addHandler(requestLogHandler);
+        serverForAgent.setHandler(handlers);
+      }
+
+    }
+  }
+
   public static void main(String[] args) throws Exception {
     Injector injector = Guice.createInjector(new ControllerModule(), new 
AuditLoggerModule());
 

Reply via email to