001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.io.PrintStream;
023import java.io.PrintWriter;
024import java.util.Date;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.http.HttpServer;
029import org.apache.hadoop.hbase.monitoring.StateDumpServlet;
030import org.apache.hadoop.hbase.util.LogMonitoring;
031import org.apache.hadoop.hbase.util.Threads;
032import org.apache.yetus.audience.InterfaceAudience;
033
034@InterfaceAudience.Private
035public class RESTDumpServlet extends StateDumpServlet {
036  private static final long serialVersionUID = 1L;
037  private static final String LINE = "===========================================================";
038
039  @Override
040  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
041    if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), request, response)) {
042      return;
043    }
044
045    RESTServer restServer = (RESTServer) getServletContext().getAttribute(RESTServer.REST_SERVER);
046    assert restServer != null : "No REST Server in context!";
047
048    response.setContentType("text/plain");
049    OutputStream os = response.getOutputStream();
050    try (PrintWriter out = new PrintWriter(os)) {
051
052      out.println("REST Server status for " + restServer.getServerName() + " as of " + new Date());
053
054      out.println("\n\nVersion Info:");
055      out.println(LINE);
056      dumpVersionInfo(out);
057
058      out.println("\n\nStacks:");
059      out.println(LINE);
060      out.flush();
061      PrintStream ps = new PrintStream(response.getOutputStream(), false, "UTF-8");
062      Threads.printThreadInfo(ps, "");
063      ps.flush();
064
065      out.println("\n\nREST Server configuration:");
066      out.println(LINE);
067      Configuration conf = restServer.conf;
068      out.flush();
069      conf.writeXml(os);
070      os.flush();
071
072      out.println("\n\nLogs");
073      out.println(LINE);
074      long tailKb = getTailKbParam(request);
075      LogMonitoring.dumpTailOfLogs(out, tailKb);
076
077      out.flush();
078    }
079  }
080}