OraMonRESTgetPrometheus.java
12.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
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 OraMonRESTgetPrometheus
*/
@WebServlet(description = "Calls getData() and getMetrics() and reports in Prometheus format", urlPatterns = { "/OraMonREST/getPrometheus" })
public class OraMonRESTgetPrometheus extends HttpServlet {
private static final long serialVersionUID = 1L;
private final static Logger LOGGER = Logger.getLogger(OraMonRESTgetPrometheus.class.getName());
/**
* @see HttpServlet#HttpServlet()
*/
public OraMonRESTgetPrometheus() {
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();
int age = 0;
if(request.getParameterMap().containsKey("age")) {
try {
age = Integer.parseInt(request.getParameter("age"));
} catch (Exception e) {
response.setStatus(400);
response.getWriter().println("{\"error\":true,\"msg\":\"The specified age parameter is not a valid integer number\"}");
return;
}
}
if(request.getParameterMap().containsKey("connectionString")) {
// We have a connection string, find the monitor or create it and call getData() on the 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 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 == 3) {
// We have a TNS connection string and username + password
monitor = Registry.findOrCreate(shortConArray[0] + "@" + shortConArray2[0], shortConArray2[1], shortConArray2[2]);
} else if (shortConArray2.length == 1) {
// We only have the TNS connection string, no username or password
monitor = Registry.find(shortConArray[0] + "@" + shortConArray2[0]);
} else {
// We have something else, not implemented
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().println("{\"error\":true,\"msg\":\"connectionString needs to be in format jdbc:oracle:thin:@(<TNS Connection String>) with optional :username:password at the end.\"}");
return;
}
} else {
// JDBC Format
if(shortConArray2[0].startsWith("//") && shortConArray2.length == 4) {
// We have new style and username and password, we can try to create the monitor if it doesnt exist
monitor = Registry.findOrCreate(shortConArray[0] + "@" + shortConArray2[0] + ":" + shortConArray2[1], shortConArray2[2], shortConArray2[3]);
} else if(!shortConArray2[0].startsWith("//") && shortConArray2.length == 5) {
// We have old style with sid with :username:password, try to find or create it
monitor = Registry.findOrCreate(shortConArray[0] + "@" + shortConArray2[0] + ":" + shortConArray2[1] + ":" + shortConArray2[2], shortConArray2[3], shortConArray2[4]);
} else if(shortConArray2[0].startsWith("//") && shortConArray2.length == 2) {
// We have new style but only have the connection string, try to find it (we cant create one without username/password)
monitor = Registry.find(request.getParameter("connectionString"));
} else if(!shortConArray2[0].startsWith("//") && shortConArray2.length == 3) {
// We have old style sid and no :username:password
monitor = Registry.find(request.getParameter("connectionString"));
} 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 {
// Short con string
// Check that we have a valid connection string (added support to handle diffrent kinds of connection strings)
String[] conStrParamArray = request.getParameter("connectionString").split(":");
Boolean serviceName = false;
if(conStrParamArray.length == 4) {
// Check if service name is used ( / instead of : )
if(conStrParamArray[1].indexOf("/") >= 0) {
serviceName = true;
}
}
if(conStrParamArray.length != 5 && serviceName == false) {
// Error, we need 5 parts in the connection string
response.setStatus(400);
response.getWriter().println("{\"error\":true,\"msg\":\"connectionString needs to be in format host:port:sid:username:password or host:port/servicename:username:password\"}");
return;
}
String conStr, usrStr, pwdStr;
if(serviceName) {
conStr = "jdbc:oracle:thin:@//" + conStrParamArray[0] + ":" + conStrParamArray[1];
usrStr = conStrParamArray[2];
pwdStr = conStrParamArray[3];
} else {
conStr = "jdbc:oracle:thin:@" + conStrParamArray[0] + ":" + conStrParamArray[1] + ":" + conStrParamArray[2];
usrStr = conStrParamArray[3];
pwdStr = conStrParamArray[4];
}
// Try to find the monitor in our list
monitor = Registry.findOrCreate(conStr, usrStr, pwdStr);
}
String pdbName = null;
if(request.getParameterMap().containsKey("pdbName")) {
String temp = request.getParameter("pdbName");
if (!temp.isEmpty()) pdbName = temp;
}
if(monitor != null) {
if(request.getParameterMap().containsKey("block")) {
// Call setBlocked()
boolean blockedState = false;
try {
blockedState = Boolean.parseBoolean(request.getParameter("block"));
} catch (Exception e) {
response.setStatus(400);
response.getWriter().println("{\"error\":true,\"msg\":\"The specified block parameter is not a valid boolean\"}");
return;
}
monitor.setBlockedState(blockedState);
if(blockedState == monitor.getBlockedState()) {
response.setStatus(200);
response.getWriter().println("{\"error\":false,\"msg\":\"The blocked state is now set to "+blockedState+"\"}");
return;
} else {
response.setStatus(500);
response.getWriter().println("{\"error\":true,\"msg\":\"The blocked state could not be set to "+blockedState+"\"}");
return;
}
} else {
// Call getData()
monitor.getData(age,pdbName);
String fName = monitor.getFriendlyName(pdbName);
String dbName = monitor.getDBName();
// Now return the metrics in Prometheus format
sb.append("Cpu Usage (%){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getCPUPercent(pdbName) + "\n");
sb.append("Logical Reads (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getLogicalReadsPerSecond(pdbName) + "\n");
sb.append("Executes (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("execute count",pdbName) + "\n");
sb.append("User Calls (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("user calls",pdbName) + "\n");
sb.append("Cache Hit Ratio (%){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getCacheHitRatioPercent(pdbName) + "\n");
sb.append("Redo Size (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("redo size",pdbName) + "\n");
sb.append("OS Busy (%){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getOsBusyPercent() + "}");
sb.append("OS Load (#){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getOsLoad() + "}");
sb.append("OS Page In (KB/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("VM_IN_BYTES", OraMon.OSSTAT)/1024 + "\n");
sb.append("OS Page Out (KB/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("VM_OUT_BYTES", OraMon.OSSTAT)/1024 + "\n");
sb.append("OS Load per Cpu (#){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getOsLoadPerCPU() + "\n");
sb.append("Consistent Gets (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("consistent gets",pdbName) + "\n");
sb.append("DB Block Gets (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("db block gets",pdbName) + "\n");
sb.append("Buffer Cache Hit Ratio (%){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getBufferCacheHitRatioPercent(pdbName) + "\n");
sb.append("DB Block Changes (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("db block changes",pdbName) + "\n");
sb.append("Physical Reads (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("physical reads",pdbName) + "\n");
sb.append("Physical Writes (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("physical writes",pdbName) + "\n");
sb.append("Redo Writes (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("redo writes",pdbName) + "\n");
sb.append("Non-idle Wait Time (ms/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("non-idle wait time",pdbName) + "\n");
sb.append("File I/O Wait Time (ms/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("file io wait time",pdbName) + "\n");
sb.append("User Commits (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("user commits",pdbName) + "\n");
sb.append("User Rollbacks (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("user rollbacks",pdbName) + "\n");
sb.append("Parse Count Total (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("parse count (total)",pdbName) + "\n");
sb.append("Parse Count Hard (#/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getPerSecondValue("parse count (hard)",pdbName) + "\n");
sb.append("Cpu Usage xNIWT (%){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getCPUPercent(true,pdbName) + "\n");
sb.append("Cpu Time (us/s){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getCPUTimePerSecond(pdbName) + "\n");
sb.append("Cpus (#){pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getNumberOfCPUs() + "\n");
sb.append("OraMon Response Time{pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getLastRTms() + "\n");
sb.append("OraMon Blocked{pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getBlockedState() + "\n");
sb.append("OraMon Age{pdb=\""+fName+"\", cdb=\""+dbName+"\"} " + monitor.getAgeTs() + "\n");
}
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
sb.append("{\"error\":true,\"msg\":\"Monitor not found and can't create one without :username:password '" + request.getParameter("connectionString") + "'\"");
sb.append("}");
}
} else {
// No input, just return the list of monitors
sb.append("{\"count\":" + Registry.getList().size() + "}");
}
response.getWriter().println(sb.toString());
} catch (Throwable e) {
response.setStatus(500);
response.getWriter().println("{\"error\":true,\"msg\":\""+e.toString().replace("\n"," ").replace("\"","\\\"").trim()+"\"}");
LOGGER.log(Level.SEVERE, e.toString());
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}