OraMonRESTgetMetrics.java 9.17 KB


import java.io.IOException;

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");
		OraMon monitor = null;
		try {
			StringBuffer sb = new StringBuffer();
			
			if(request.getParameterMap().containsKey("connectionString")) {
				// We have a connection string, find the monitor and report only on that monitor
				
				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 or jdbc:oracle:thin:@(<TNS Connection String>) with optional :username:password at the end.\"}");
						return;
					}
					String[] shortConArray2 = shortConArray[1].split(":");
					if(shortConArray2[0].startsWith("(")) { 
						// TNS Format
						if(shortConArray2.length >= 1) {
							monitor = Registry.find(shortConArray[0] + "@" + shortConArray2[0]);
							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]+"\"}");
								return;
							}
						}
					} else {
						// JDBC Format
						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, response with 404
				response.setStatus(HttpServletResponse.SC_NOT_FOUND);
				response.getWriter().println("{\"error\":true,\"msg\":\"Can not find a monitor with connection string "+request.getParameter("connectionString")+"\"}");
				return;
			}
			
			String pdbName = null;
			if(request.getParameterMap().containsKey("pdbName")) { 
				String temp = request.getParameter("pdbName");
				if (!temp.isEmpty()) pdbName = temp;
			}
			
			// Write out the monitors data
			sb.append("{\"name\":\"" + monitor.getFriendlyName(pdbName) + "\",\"error\":false,\"nvarray\":");
			sb.append("[");
			
			
			sb.append("{\"name\":\"Cpu Usage (%)\",\"value\":" + monitor.getCPUPercent(pdbName) + "}");
			sb.append(",{\"name\":\"Logical Reads (#/s)\",\"value\":" + monitor.getLogicalReadsPerSecond(pdbName) + "}");
			sb.append(",{\"name\":\"Executes (#/s)\",\"value\":" + monitor.getPerSecondValue("execute count",pdbName) + "}");
			sb.append(",{\"name\":\"User Calls (#/s)\",\"value\":" + monitor.getPerSecondValue("user calls",pdbName) + "}");
			sb.append(",{\"name\":\"Cache Hit Ratio (%)\",\"value\":" + monitor.getCacheHitRatioPercent(pdbName) + "}");
			sb.append(",{\"name\":\"Redo Size (#/s)\",\"value\":" + monitor.getPerSecondValue("redo size",pdbName) + "}");
			
			sb.append(",{\"name\":\"OS Busy (%)\",\"value\":" + monitor.getOsBusyPercent() + "}");
			sb.append(",{\"name\":\"OS Load (#)\",\"value\":" + monitor.getOsLoad() + "}");
			sb.append(",{\"name\":\"OS Page In (KB/s)\",\"value\":" + monitor.getPerSecondValue("VM_IN_BYTES", OraMon.OSSTAT)/1024 + "}");
			sb.append(",{\"name\":\"OS Page Out (KB/s)\",\"value\":" + monitor.getPerSecondValue("VM_OUT_BYTES", OraMon.OSSTAT)/1024 + "}");
			sb.append(",{\"name\":\"OS Load per Cpu (#)\",\"value\":" + monitor.getOsLoadPerCPU() + "}");
			
			sb.append(",{\"name\":\"Consistent Gets (#/s)\",\"value\":" + monitor.getPerSecondValue("consistent gets",pdbName) + "}");
			sb.append(",{\"name\":\"DB Block Gets (#/s)\",\"value\":" + monitor.getPerSecondValue("db block gets",pdbName) + "}");
			
			sb.append(",{\"name\":\"Buffer Cache Hit Ratio (%)\",\"value\":" + monitor.getBufferCacheHitRatioPercent(pdbName) + "}");
			sb.append(",{\"name\":\"DB Block Changes (#/s)\",\"value\":" + monitor.getPerSecondValue("db block changes",pdbName) + "}");
			
			sb.append(",{\"name\":\"Physical Reads (#/s)\",\"value\":" + monitor.getPerSecondValue("physical reads",pdbName) + "}");
			sb.append(",{\"name\":\"Physical Writes (#/s)\",\"value\":" + monitor.getPerSecondValue("physical writes",pdbName) + "}");
			sb.append(",{\"name\":\"Redo Writes (#/s)\",\"value\":" + monitor.getPerSecondValue("redo writes",pdbName) + "}");
			sb.append(",{\"name\":\"Non-idle Wait Time (ms/s)\",\"value\":" + monitor.getPerSecondValue("non-idle wait time",pdbName) + "}");
			sb.append(",{\"name\":\"File I/O Wait Time (ms/s)\",\"value\":" + monitor.getPerSecondValue("file io wait time",pdbName) + "}");
			
			sb.append(",{\"name\":\"User Commits (#/s)\",\"value\":" + monitor.getPerSecondValue("user commits",pdbName) + "}");
			sb.append(",{\"name\":\"User Rollbacks (#/s)\",\"value\":" + monitor.getPerSecondValue("user rollbacks",pdbName) + "}");
			sb.append(",{\"name\":\"Parse Count Total (#/s)\",\"value\":" + monitor.getPerSecondValue("parse count (total)",pdbName) + "}");
			sb.append(",{\"name\":\"Parse Count Hard (#/s)\",\"value\":" + monitor.getPerSecondValue("parse count (hard)",pdbName) + "}");
			sb.append(",{\"name\":\"Cpu Usage xNIWT (%)\",\"value\":" + monitor.getCPUPercent(true,pdbName) + "}");
			sb.append(",{\"name\":\"Cpu Time (us/s)\",\"value\":" + monitor.getCPUTimePerSecond(pdbName) + "}");
			sb.append(",{\"name\":\"Cpus (#)\",\"value\":" + monitor.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();
		}
	}

}