OraMonRESTgetMetrics.java
5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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(":");
if(conStrParamArray.length < 3) {
// 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\"}");
return;
}
String 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 (ms/s)" + aStr + "\",\"value\":" + item.getCPUTimePerSecond() + "}");
sb.append(",{\"name\":\"Cpu Usage (%)" + aStr + "\",\"value\":" + item.getCPUPercent() + "}");
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();
}
}
}