OraMonRESTgetMetrics.java 8.46 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
				OraMon monitor = null;
				if(request.getParameter("connectionString").startsWith("jdbc:oracle")) {
					// Full con string
					String[] shortConArray = request.getParameter("connectionString").split("@");
					if(shortConArray.length != 2) {
						// Error, wrong format of string
						response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
						response.getWriter().println("{\"error\":true,\"msg\":\"connectionString needs to be in format jdbc:oracle:thin:@//host:port/servicename with optional :username:password at the end.\"}");
						return;
					}
					String[] shortConArray2 = shortConArray[1].split(":");
					if(shortConArray2[0].startsWith("//") && (shortConArray2.length == 4 || shortConArray2.length == 2)) {
						// We seem to have a correct connection string, if :username:password exist we ignore them here
						monitor = Registry.find(shortConArray[0] +  "@" + shortConArray2[0] + ":" + shortConArray2[1]);
						if(monitor == null) {
							response.setStatus(HttpServletResponse.SC_NOT_FOUND);
							response.getWriter().println("{\"error\":true,\"msg\":\"Can not find a monitor with connection string "+shortConArray[0] +  "@" + shortConArray2[0] + ":" + shortConArray2[1]+"\"}");
							return;
						}
					} else if(!shortConArray2[0].startsWith("//") && (shortConArray2.length == 5 || shortConArray2.length == 3)) {
						// We seem to have a correct connection string, if :username:password exist we ignore them here
						monitor = Registry.find(shortConArray[0] +  "@" + shortConArray2[0] + ":" + shortConArray2[1] + ":" + shortConArray2[2]);
						if(monitor == null) {
							response.setStatus(HttpServletResponse.SC_NOT_FOUND);
							response.getWriter().println("{\"error\":true,\"msg\":\"Can not find a monitor with connection string "+shortConArray[0] +  "@" + shortConArray2[0] + ":" + shortConArray2[1] + ":" + shortConArray2[2]+"\"}");
							return;
						}
					} else {
						// Wrong format
						response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
						response.getWriter().println("{\"error\":true,\"msg\":\"connectionString needs to be in format jdbc:oracle:thin:@//host:port/servicename with optional :username:password at the end.\"}");
						return;
					}
				} else {
					// Check that we have a valid connection string (old style)
					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];
					}
					monitor = Registry.find(conStr);
					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 "+request.getParameter("connectionString")+"\"}");
						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
			
			for (OraMon item : list) {
				sb.append("{\"name\":\"" + item.getDBName() + "\",\"error\":false,\"nvarray\":");
				sb.append("[");
				
				
				sb.append("{\"name\":\"Cpu Usage (%)\",\"value\":" + item.getCPUPercent() + "}");
				sb.append(",{\"name\":\"Logical Reads (#/s)\",\"value\":" + item.getLogicalReadsPerSecond() + "}");
				sb.append(",{\"name\":\"Executes (#/s)\",\"value\":" + item.getPerSecondValue("execute count") + "}");
				sb.append(",{\"name\":\"User Calls (#/s)\",\"value\":" + item.getPerSecondValue("user calls") + "}");
				sb.append(",{\"name\":\"Cache Hit Ratio (%)\",\"value\":" + item.getCacheHitRatioPercent() + "}");
				sb.append(",{\"name\":\"Redo Size (#/s)\",\"value\":" + item.getPerSecondValue("redo size") + "}");
				
				sb.append(",{\"name\":\"OS Busy (%)\",\"value\":" + item.getOsBusyPercent() + "}");
				sb.append(",{\"name\":\"OS Load (#)\",\"value\":" + item.getOsLoad() + "}");
				sb.append(",{\"name\":\"OS Page In (KB/s)\",\"value\":" + item.getPerSecondValue("VM_IN_BYTES", OraMon.OSSTAT)/1024 + "}");
				sb.append(",{\"name\":\"OS Page Out (KB/s)\",\"value\":" + item.getPerSecondValue("VM_OUT_BYTES", OraMon.OSSTAT)/1024 + "}");
				sb.append(",{\"name\":\"OS Load per Cpu (#)\",\"value\":" + item.getOsLoadPerCPU() + "}");
				
				sb.append(",{\"name\":\"Consistent Gets (#/s)\",\"value\":" + item.getPerSecondValue("consistent gets") + "}");
				sb.append(",{\"name\":\"DB Block Gets (#/s)\",\"value\":" + item.getPerSecondValue("db block gets") + "}");
				
				sb.append(",{\"name\":\"Buffer Cache Hit Ratio (%)\",\"value\":" + item.getBufferCacheHitRatioPercent() + "}");
				sb.append(",{\"name\":\"DB Block Changes (#/s)\",\"value\":" + item.getPerSecondValue("db block changes") + "}");
				
				sb.append(",{\"name\":\"Physical Reads (#/s)\",\"value\":" + item.getPerSecondValue("physical reads") + "}");
				sb.append(",{\"name\":\"Physical Writes (#/s)\",\"value\":" + item.getPerSecondValue("physical writes") + "}");
				sb.append(",{\"name\":\"Redo Writes (#/s)\",\"value\":" + item.getPerSecondValue("redo writes") + "}");
				sb.append(",{\"name\":\"Non-idle Wait Time (ms/s)\",\"value\":" + item.getPerSecondValue("non-idle wait time") + "}");
				sb.append(",{\"name\":\"File I/O Wait Time (ms/s)\",\"value\":" + item.getPerSecondValue("file io wait time") + "}");
				
				sb.append(",{\"name\":\"User Commits (#/s)\",\"value\":" + item.getPerSecondValue("user commits") + "}");
				sb.append(",{\"name\":\"User Rollbacks (#/s)\",\"value\":" + item.getPerSecondValue("user rollbacks") + "}");
				sb.append(",{\"name\":\"Parse Count Total (#/s)\",\"value\":" + item.getPerSecondValue("parse count (total)") + "}");
				sb.append(",{\"name\":\"Parse Count Hard (#/s)\",\"value\":" + item.getPerSecondValue("parse count (hard)") + "}");
				sb.append(",{\"name\":\"Cpu Usage xNIWT (%)\",\"value\":" + item.getCPUPercent(true) + "}");
				sb.append(",{\"name\":\"Cpu Time (us/s)\",\"value\":" + item.getCPUTimePerSecond() + "}");
				sb.append(",{\"name\":\"Cpus (#)\",\"value\":" + item.getNumberOfCPUs() + "}");
			}
			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();
		}
	}

}