All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.servlet.StatisticsServlet Maven / Gradle / Ivy

Go to download

A-Jetty Base can run on Android Java as well as on standard Java 7+ and it can run precompiled JSP/JSTL.

There is a newer version: 1.0.5
Show newest version
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.AbstractConnector;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ConnectorStatistics;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * StatisticsServlet
 *
 *
 */
public class StatisticsServlet extends HttpServlet
{
    private static final Logger LOG = Log.getLogger(StatisticsServlet.class);

    boolean _restrictToLocalhost = true; // defaults to true
    private StatisticsHandler _statsHandler;
    private MemoryMXBean _memoryBean;
    private Connector[] _connectors;

    
    
    /** 
     * @see javax.servlet.GenericServlet#init()
     */
    public void init() throws ServletException
    {
        ServletContext context = getServletContext();
        ContextHandler.Context scontext = (ContextHandler.Context) context;
        Server _server = scontext.getContextHandler().getServer();

        Handler handler = _server.getChildHandlerByClass(StatisticsHandler.class);

        if (handler != null)
        {
            _statsHandler = (StatisticsHandler) handler;
        }
        else
        {
            LOG.warn("Statistics Handler not installed!");
            return;
        }
        
        _memoryBean = ManagementFactory.getMemoryMXBean();
        _connectors = _server.getConnectors();

        if (getInitParameter("restrictToLocalhost") != null)
        {
            _restrictToLocalhost = "true".equals(getInitParameter("restrictToLocalhost"));
        }
    }

    
    
    /** 
     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
    {
        doGet(sreq, sres);
    }

    
    
    /** 
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        if (_statsHandler == null)
        {
            LOG.warn("Statistics Handler not installed!");
            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }
        if (_restrictToLocalhost)
        {
            if (!isLoopbackAddress(req.getRemoteAddr()))
            {
                resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
                return;
            }
        }

        String wantXml = req.getParameter("xml");
        if (wantXml == null)
          wantXml = req.getParameter("XML");

        if (wantXml != null && "true".equalsIgnoreCase(wantXml))
        {
            sendXmlResponse(resp);
        }
        else
        {
            sendTextResponse(resp);
        }

    }

    private boolean isLoopbackAddress(String address)
    {
        try
        {
            InetAddress addr = InetAddress.getByName(address); 
            return addr.isLoopbackAddress();
        }
        catch (UnknownHostException e )
        {
            LOG.warn("Warning: attempt to access statistics servlet from " + address, e);
            return false;
        }
    }

    private void sendXmlResponse(HttpServletResponse response) throws IOException
    {
        StringBuilder sb = new StringBuilder();

        sb.append("\n");

        sb.append("  \n");
        sb.append("    ").append(_statsHandler.getStatsOnMs()).append("\n");
        
        sb.append("    ").append(_statsHandler.getRequests()).append("\n");
        sb.append("    ").append(_statsHandler.getRequestsActive()).append("\n");
        sb.append("    ").append(_statsHandler.getRequestsActiveMax()).append("\n");
        sb.append("    ").append(_statsHandler.getRequestTimeTotal()).append("\n");
        sb.append("    ").append(_statsHandler.getRequestTimeMean()).append("\n");
        sb.append("    ").append(_statsHandler.getRequestTimeMax()).append("\n");
        sb.append("    ").append(_statsHandler.getRequestTimeStdDev()).append("\n");

        sb.append("    ").append(_statsHandler.getDispatched()).append("\n");
        sb.append("    ").append(_statsHandler.getDispatchedActive()).append("\n");
        sb.append("    ").append(_statsHandler.getDispatchedActiveMax()).append("\n");
        sb.append("    ").append(_statsHandler.getDispatchedTimeTotal()).append("\n");
        sb.append("    ").append(_statsHandler.getDispatchedTimeMean()).append("\n");
        sb.append("    ").append(_statsHandler.getDispatchedTimeMax()).append("\n");
        sb.append("    ").append(_statsHandler.getDispatchedTimeStdDev()).append("\n");
 
        sb.append("    ").append(_statsHandler.getAsyncRequests()).append("\n");
        sb.append("    ").append(_statsHandler.getAsyncRequestsWaiting()).append("\n");
        sb.append("    ").append(_statsHandler.getAsyncRequestsWaitingMax()).append("\n");
        sb.append("    ").append(_statsHandler.getAsyncDispatches()).append("\n");
        sb.append("    ").append(_statsHandler.getExpires()).append("\n");
        sb.append("  \n");

        sb.append("  \n");
        sb.append("    ").append(_statsHandler.getResponses1xx()).append("\n");
        sb.append("    ").append(_statsHandler.getResponses2xx()).append("\n");
        sb.append("    ").append(_statsHandler.getResponses3xx()).append("\n");
        sb.append("    ").append(_statsHandler.getResponses4xx()).append("\n");
        sb.append("    ").append(_statsHandler.getResponses5xx()).append("\n");
        sb.append("    ").append(_statsHandler.getResponsesBytesTotal()).append("\n");
        sb.append("  \n");

        sb.append("  \n");
        for (Connector connector : _connectors)
        {
            sb.append("    \n");
            sb.append("      ").append(connector.getClass().getName()).append("@").append(connector.hashCode()).append("\n");
            sb.append("      \n");
            for (String protocol:connector.getProtocols())
                sb.append("      ").append(protocol).append("\n");
            sb.append("      \n");

            ConnectorStatistics connectorStats = null;

            if (connector instanceof AbstractConnector)
                connectorStats = ((AbstractConnector)connector).getBean(ConnectorStatistics.class);
            if (connectorStats == null)
                sb.append("      false\n");
            else
            {
                sb.append("      true\n");
                sb.append("      ").append(connectorStats.getConnections()).append("\n");
                sb.append("      ").append(connectorStats.getConnectionsOpen()).append("\n");
                sb.append("      ").append(connectorStats.getConnectionsOpenMax()).append("\n");
                sb.append("      ").append(connectorStats.getConnectionDurationMean()).append("\n");
                sb.append("      ").append(connectorStats.getConnectionDurationMax()).append("\n");
                sb.append("      ").append(connectorStats.getConnectionDurationStdDev()).append("\n");
                sb.append("      ").append(connectorStats.getMessagesIn()).append("\n");
                sb.append("      ").append(connectorStats.getMessagesIn()).append("\n");
                sb.append("      ").append(connectorStats.getStartedMillis()).append("\n");
            }
            sb.append("    \n");
        }
        sb.append("  \n");

        sb.append("  \n");
        sb.append("    ").append(_memoryBean.getHeapMemoryUsage().getUsed()).append("\n");
        sb.append("    ").append(_memoryBean.getNonHeapMemoryUsage().getUsed()).append("\n");
        sb.append("  \n");
        
        sb.append("\n");

        response.setContentType("text/xml");
        PrintWriter pout = response.getWriter();
        pout.write(sb.toString());
    }

    
    
    /**
     * @param response
     * @throws IOException
     */
    private void sendTextResponse(HttpServletResponse response) throws IOException
    {
        StringBuilder sb = new StringBuilder();
        sb.append(_statsHandler.toStatsHTML());

        sb.append("

Connections:

\n"); for (Connector connector : _connectors) { sb.append("

").append(connector.getClass().getName()).append("@").append(connector.hashCode()).append("

"); sb.append("Protocols:"); for (String protocol:connector.getProtocols()) sb.append(protocol).append(" "); sb.append("
\n"); ConnectorStatistics connectorStats = null; if (connector instanceof AbstractConnector) connectorStats = ((AbstractConnector)connector).getBean(ConnectorStatistics.class); if (connectorStats != null) { sb.append("Statistics gathering started ").append(connectorStats.getStartedMillis()).append("ms ago").append("
\n"); sb.append("Total connections: ").append(connectorStats.getConnections()).append("
\n"); sb.append("Current connections open: ").append(connectorStats.getConnectionsOpen()).append("
\n");; sb.append("Max concurrent connections open: ").append(connectorStats.getConnectionsOpenMax()).append("
\n"); sb.append("Mean connection duration: ").append(connectorStats.getConnectionDurationMean()).append("
\n"); sb.append("Max connection duration: ").append(connectorStats.getConnectionDurationMax()).append("
\n"); sb.append("Connection duration standard deviation: ").append(connectorStats.getConnectionDurationStdDev()).append("
\n"); sb.append("Total messages in: ").append(connectorStats.getMessagesIn()).append("
\n"); sb.append("Total messages out: ").append(connectorStats.getMessagesOut()).append("
\n"); } else { sb.append("Statistics gathering off.\n"); } } sb.append("

Memory:

\n"); sb.append("Heap memory usage: ").append(_memoryBean.getHeapMemoryUsage().getUsed()).append(" bytes").append("
\n"); sb.append("Non-heap memory usage: ").append(_memoryBean.getNonHeapMemoryUsage().getUsed()).append(" bytes").append("
\n"); response.setContentType("text/html"); PrintWriter pout = response.getWriter(); pout.write(sb.toString()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy