OraMonRESTgetMetrics.java
8.46 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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();
}
}
}