org.eclipse.jetty.servlet.StatisticsServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache Show documentation
Show all versions of ehcache Show documentation
Ehcache is an open source, standards-based cache used to boost performance,
offload the database and simplify scalability. Ehcache is robust, proven and full-featured and
this has made it the most widely-used Java-based cache.
//
// ========================================================================
// Copyright (c) 1995-2018 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.io.ConnectionStatistics;
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.component.Container;
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;
@Override
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"));
}
}
@Override
public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
{
doGet(sreq, sres);
}
@Override
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;
}
}
if (Boolean.valueOf( req.getParameter("statsReset")))
{
_statsHandler.statsReset();
return;
}
String wantXml = req.getParameter("xml");
if (wantXml == null)
wantXml = req.getParameter("XML");
if (Boolean.valueOf(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");
ConnectionStatistics connectionStats = null;
if (connector instanceof AbstractConnector)
connectionStats = ((AbstractConnector)connector).getBean(ConnectionStatistics.class);
if (connectionStats != null)
{
sb.append(" true \n");
sb.append(" ").append(connectionStats.getConnectionsTotal()).append(" \n");
sb.append(" ").append(connectionStats.getConnections()).append(" \n");
sb.append(" ").append(connectionStats.getConnectionsMax()).append(" \n");
sb.append(" ").append(connectionStats.getConnectionDurationMean()).append(" \n");
sb.append(" ").append(connectionStats.getConnectionDurationMax()).append(" \n");
sb.append(" ").append(connectionStats.getConnectionDurationStdDev()).append(" \n");
sb.append(" ").append(connectionStats.getReceivedBytes()).append(" \n");
sb.append(" ").append(connectionStats.getSentBytes()).append("\n");
sb.append(" ").append(connectionStats.getReceivedMessages()).append(" \n");
sb.append(" ").append(connectionStats.getSentMessages()).append(" \n");
}
else
{
ConnectorStatistics connectorStats = null;
if (connector instanceof AbstractConnector)
connectorStats = ((AbstractConnector)connector).getBean(ConnectorStatistics.class);
if (connectorStats != null)
{
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");
}
else
{
sb.append(" false \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.getClass().getName()).append("@").append(connector.hashCode()).append("
");
sb.append("Protocols:");
for (String protocol:connector.getProtocols())
sb.append(protocol).append(" ");
sb.append("
\n");
ConnectionStatistics connectionStats = null;
if (connector instanceof Container)
connectionStats = ((Container)connector).getBean(ConnectionStatistics.class);
if (connectionStats != null)
{
sb.append("Total connections: ").append(connectionStats.getConnectionsTotal()).append("
\n");
sb.append("Current connections open: ").append(connectionStats.getConnections()).append("
\n");
sb.append("Max concurrent connections open: ").append(connectionStats.getConnectionsMax()).append("
\n");
sb.append("Mean connection duration: ").append(connectionStats.getConnectionDurationMean()).append("
\n");
sb.append("Max connection duration: ").append(connectionStats.getConnectionDurationMax()).append("
\n");
sb.append("Connection duration standard deviation: ").append(connectionStats.getConnectionDurationStdDev()).append("
\n");
sb.append("Total bytes received: ").append(connectionStats.getReceivedBytes()).append("
\n");
sb.append("Total bytes sent: ").append(connectionStats.getSentBytes()).append("
\n");
sb.append("Total messages received: ").append(connectionStats.getReceivedMessages()).append("
\n");
sb.append("Total messages sent: ").append(connectionStats.getSentMessages()).append("
\n");
}
else
{
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 - 2025 Weber Informatics LLC | Privacy Policy