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

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

There is a newer version: 8.2.0.v20160908
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.Connector;
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;

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;

    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"));
        }

    }

    public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
    {
        doGet(sreq, sres);
    }

    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.getSuspends()).append("\n");
        sb.append("    ").append(_statsHandler.getExpires()).append("\n");
        sb.append("    ").append(_statsHandler.getResumes()).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.getName()).append("\n");
        	sb.append("      ").append(connector.getStatsOn()).append("\n");
            if (connector.getStatsOn())
            {
            	sb.append("    ").append(connector.getStatsOnMs()).append("\n");
            	sb.append("    ").append(connector.getConnections()).append("\n");
            	sb.append("    ").append(connector.getConnectionsOpen()).append("\n");
            	sb.append("    ").append(connector.getConnectionsOpenMax()).append("\n");
            	sb.append("    ").append(connector.getConnectionsDurationTotal()).append("\n");
            	sb.append("    ").append(connector.getConnectionsDurationMean()).append("\n");
            	sb.append("    ").append(connector.getConnectionsDurationMax()).append("\n");
                sb.append("    ").append(connector.getConnectionsDurationStdDev()).append("\n");
                sb.append("    ").append(connector.getRequests()).append("\n");
                sb.append("    ").append(connector.getConnectionsRequestsMean()).append("\n");
                sb.append("    ").append(connector.getConnectionsRequestsMax()).append("\n");
                sb.append("    ").append(connector.getConnectionsRequestsStdDev()).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());
    }

    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.getName()).append("

"); if (connector.getStatsOn()) { sb.append("Statistics gathering started ").append(connector.getStatsOnMs()).append("ms ago").append("
\n"); sb.append("Total connections: ").append(connector.getConnections()).append("
\n"); sb.append("Current connections open: ").append(connector.getConnectionsOpen()).append("
\n"); sb.append("Max concurrent connections open: ").append(connector.getConnectionsOpenMax()).append("
\n"); sb.append("Total connections duration: ").append(connector.getConnectionsDurationTotal()).append("
\n"); sb.append("Mean connection duration: ").append(connector.getConnectionsDurationMean()).append("
\n"); sb.append("Max connection duration: ").append(connector.getConnectionsDurationMax()).append("
\n"); sb.append("Connection duration standard deviation: ").append(connector.getConnectionsDurationStdDev()).append("
\n"); sb.append("Total requests: ").append(connector.getRequests()).append("
\n"); sb.append("Mean requests per connection: ").append(connector.getConnectionsRequestsMean()).append("
\n"); sb.append("Max requests per connection: ").append(connector.getConnectionsRequestsMax()).append("
\n"); sb.append("Requests per connection standard deviation: ").append(connector.getConnectionsRequestsStdDev()).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