OraMonRESTgetMetrics.java 6.68 KB


import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import se.lil.om.OraMon;
import se.lil.om.Registry;

/**
 * Servlet implementation class OraMonRESTgetMetrics
 */
@WebServlet(description = "Gets a default set of metrics from an existing monitor", urlPatterns = { "/OraMonREST/getMetrics" })
public class OraMonRESTgetMetrics extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OraMonRESTgetMetrics() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		try {
			StringBuffer sb = new StringBuffer();
			ArrayList<OraMon> list = Registry.getList();
			
			if(request.getParameterMap().containsKey("connectionString")) {
				// We have a connection string, find the monitor and report only on that monitor
				// Check that we have a valid connection string
				String[] conStrParamArray = request.getParameter("connectionString").split(":");
				Boolean serviceName = false;
				if(conStrParamArray.length > 1) {
					// Check if service name is used ( / instead of : )
					if(conStrParamArray[1].indexOf("/") >= 0) {
						serviceName = true;
					}
				}
				if(conStrParamArray.length < 3 && serviceName == false) {
					// Error, we need at least 3 parts in the connection string
					response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
					response.getWriter().println("{\"error\":true,\"msg\":\"connectionString needs to be in format host:port:sid or host:port/servicename\"}");
					return;
				}
				String conStr;
				if(serviceName) {
					conStr = "jdbc:oracle:thin:@" + conStrParamArray[0] + ":" + conStrParamArray[1];
				} else {
					conStr = "jdbc:oracle:thin:@" + conStrParamArray[0] + ":" + conStrParamArray[1] + ":" + conStrParamArray[2];
				}
				
				// Try to find the monitor in our list
				OraMon monitor = null;
				for (OraMon item : Registry.getList()) {
					if(item.getConString().equals(conStr)) {
						monitor = item;
					}
				}
				
				// If not found return error
				if(monitor == null) {
					// Not found, create it
					response.setStatus(HttpServletResponse.SC_NOT_FOUND);
					response.getWriter().println("{\"error\":true,\"msg\":\"Can not find a monitor with connection string "+conStr+"\"}");
					return;
				}
				
				// Set this monitor to the only one on the list
				list = new ArrayList<OraMon>();
				list.add(monitor);
			}
			
			// Loop over all monitors and output a Json array with the default set of metrics
			sb.append("{\"error\":false,\"nvarray\":");
			String aStr = "";
			sb.append("[");
			for (OraMon item : list) {
				if (list.size() > 1) aStr = " " + item.getDBName();
				sb.append("{\"name\":\"Cpus (#)" + aStr + "\",\"value\":" + item.getNumberOfCPUs() + "}");
				sb.append(",{\"name\":\"Cpu Time (us/s)" + aStr + "\",\"value\":" + item.getCPUTimePerSecond() + "}");
				sb.append(",{\"name\":\"Cpu Usage (%)" + aStr + "\",\"value\":" + item.getCPUPercent() + "}");
				sb.append(",{\"name\":\"Cpu Usage xNIWT (%)" + aStr + "\",\"value\":" + item.getCPUPercent(true) + "}");
				sb.append(",{\"name\":\"OS Busy (%)" + aStr + "\",\"value\":" + item.getOsBusyPercent() + "}");
				sb.append(",{\"name\":\"OS Load (#)" + aStr + "\",\"value\":" + item.getOsLoad() + "}");
				sb.append(",{\"name\":\"OS Page In (KB/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("VM_IN_BYTES", OraMon.OSSTAT)/1024 + "}");
				sb.append(",{\"name\":\"OS Page Out (KB/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("VM_OUT_BYTES", OraMon.OSSTAT)/1024 + "}");
				sb.append(",{\"name\":\"OS Load per Cpu (#)" + aStr + "\",\"value\":" + item.getOsLoadPerCPU() + "}");
				sb.append(",{\"name\":\"Logical Reads (#/s)" + aStr + "\",\"value\":" + item.getLogicalReadsPerSecond() + "}");
				sb.append(",{\"name\":\"Consistent Gets (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("consistent gets") + "}");
				sb.append(",{\"name\":\"DB Block Gets (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("db block gets") + "}");
				sb.append(",{\"name\":\"Cache Hit Ratio (%)" + aStr + "\",\"value\":" + item.getCacheHitRatioPercent() + "}");
				sb.append(",{\"name\":\"Buffer Cache Hit Ratio (%)" + aStr + "\",\"value\":" + item.getBufferCacheHitRatioPercent() + "}");
				sb.append(",{\"name\":\"DB Block Changes (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("db block changes") + "}");
				sb.append(",{\"name\":\"Redo Size (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("redo size") + "}");
				sb.append(",{\"name\":\"Physical Reads (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("physical reads") + "}");
				sb.append(",{\"name\":\"Physical Writes (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("physical writes") + "}");
				sb.append(",{\"name\":\"Redo Writes (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("redo writes") + "}");
				sb.append(",{\"name\":\"Non-idle Wait Time (ms/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("non-idle wait time") + "}");
				sb.append(",{\"name\":\"File I/O Wait Time (ms/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("file io wait time") + "}");
				sb.append(",{\"name\":\"Executes (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("execute count") + "}");
				sb.append(",{\"name\":\"User Calls (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("user calls") + "}");
				sb.append(",{\"name\":\"User Commits (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("user commits") + "}");
				sb.append(",{\"name\":\"User Rollbacks (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("user rollbacks") + "}");
				sb.append(",{\"name\":\"Parse Count Total (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("parse count (total)") + "}");
				sb.append(",{\"name\":\"Parse Count Hard (#/s)" + aStr + "\",\"value\":" + item.getPerSecondValue("parse count (hard)") + "}");
			}
			sb.append("]}");
			
			response.getWriter().println(sb.toString());
			
		} catch (Throwable e) {
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			response.getWriter().println("{\"error\":true,\"msg\":\""+e.toString().replace("\n"," ").trim()+"\"}");
			e.printStackTrace();
		}
	}

}