CollTomcatStatusWithHttp.java
9.01 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.sitech.ismp.coll.centercoll;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
public class CollTomcatStatusWithHttp implements CollTomcatStatusMBean {
private Logger logger = Logger.getLogger("COLL");
private Vector result = new Vector();
private String KBP_CLASS = "10-12-13";
public CollTomcatStatusWithHttp(){
}
public Vector collStatus(HashMap params) {
// TODO Auto-generated method stub
// String
result.clear();
String url = (String) params.get("URL");
String username = (String) params.get("USERNAME");
String password = (String) params.get("PASSWORD");
String hostname = (String) params.get("HOSTNAME");
if (username == null) {
username = "";
}
if (password == null) {
password = "";
}
String UNIT_ID="";
String status_htm = this.request(url, username, password);
if (status_htm != null && !status_htm.equals("")) {
// <h1>JVM</h1><p> Free memory: 0.58 MB Total memory: 8.55 MB Max
// memory: 63.56 MB</p>
// 解析出JVM信息
int index_jvm = 0;
index_jvm = status_htm.indexOf("<h1>JVM</h1>");
if (index_jvm > 1) {
int index_jvm_end = status_htm.indexOf("</p>", index_jvm);
String jvm_info = status_htm.substring(index_jvm
+ "<h1>JVM</h1><p>".length(), index_jvm_end);
//System.out.println(jvm_info);
String[] jvm_infos = jvm_info.trim().split(" ");
/*for(int i=0;i<jvm_infos.length;i++){
System.out.println(i+"="+jvm_infos[i]);
}*/
//CM-00-04-004-01 当前使用的内存 total
// CM-00-04-003-02 允许支配的内存堆 free
//PM-00-04-004-02 持久内存堆大小 max
try{
UNIT_ID = this.KBP_CLASS+"-10:"+hostname;
this.setResult(jvm_infos[2], UNIT_ID, "CM-00-04-003-02");
this.setResult(jvm_infos[6], UNIT_ID, "CM-00-04-004-01");
this.setResult(jvm_infos[10], UNIT_ID, "PM-00-04-004-02");
}catch(Exception e){
e.printStackTrace();
}
}
/*
* //<h1>http-8080</h1><p> Max threads: 150 Min spare threads: 25
* Max spare threads: 75 Current thread count: 25 Current thread
* busy: 2<br> Max processing time: 183 ms Processing time: 0 s
* Request count: 15 Error count: 8 Bytes received: 0.00 MB Bytes
* sent: 0.04 MB</p>
*/
// 解析出http端口
int index_http = 0;
index_http = status_htm.indexOf("<h1>http-");
if (index_http > 1) {
int index_http_end = status_htm.indexOf("</h1>", index_http);
String port = status_htm.substring(index_http+"<h1>http-".length(), index_http_end);
//System.out.println(port);
//CM-00-04-003-07 10-12 监听的端口
try{
this.setResult(port, UNIT_ID, "CM-00-04-003-07");
}catch(Exception e){
e.printStackTrace();
}
int index_http_info = status_htm.indexOf("<p>", index_http_end);
int index_http_info_end = status_htm.indexOf("</p>", index_http_end);
String http_info=status_htm.substring(index_http_info+"<p>".length(),index_http_info_end);
//System.out.println(http_info);
String[] http_infos = http_info.trim().split(" ");
// for(int i=0;i<http_infos.length;i++){
//// System.out.println(i+"="+http_infos[i]);
// logger.info("for each collTomcat :" + i + " : " + http_infos[i] +"/n");
// }
if(http_infos.length == 34){
try{
UNIT_ID = this.KBP_CLASS+"-11:"+hostname;
this.setResult(http_infos[2], UNIT_ID, "PM-00-04-004-05");
this.setResult(http_infos[6], UNIT_ID, "PM-00-04-004-06");
this.setResult(http_infos[10].substring(0,http_infos[10].indexOf("<")), UNIT_ID, "PM-00-04-004-07");
this.setResult(http_infos[14], UNIT_ID, "PM-00-04-800-03");
this.setResult(http_infos[18], UNIT_ID, "PM-00-04-800-04");
this.setResult(http_infos[22], UNIT_ID, "PM-00-04-800-05");
this.setResult(http_infos[25], UNIT_ID, "PM-00-04-800-06");
this.setResult(http_infos[28], UNIT_ID, "PM-00-04-800-07");
this.setResult(http_infos[32], UNIT_ID, "PM-00-04-800-08");
}catch(Exception e){
e.printStackTrace();
}
try{
UNIT_ID = this.KBP_CLASS+"-10:"+hostname;
this.setResult("UP", UNIT_ID, "FM-00-04-001-01");
}catch(Exception e){
e.printStackTrace();
}
}
//PM-00-04-004-05 最大线程
//PM-00-04-004-06 当前线程数
//PM-00-04-004-07 繁忙线程数
//PM-00-04-004-08 最小空闲线程数
//PM-00-04-004-09 最大空闲线程数
//PM-00-04-800-03 最大处理时间 float ms
// PM-00-04-800-04 总处理时间 float s
//PM-00-04-800-05 总请求数 int 个
// PM-00-04-800-06 发生错误 int 个
// PM-00-04-800-07 接收字节 float MB
// PM-00-04-800-08 发送字节 float MB
if(http_infos.length == 42){
try{
UNIT_ID = this.KBP_CLASS+"-11:"+hostname;
this.setResult(http_infos[2], UNIT_ID, "PM-00-04-004-05");
this.setResult(http_infos[6], UNIT_ID, "PM-00-04-004-08");
this.setResult(http_infos[10], UNIT_ID, "PM-00-04-004-09");
this.setResult(http_infos[14], UNIT_ID, "PM-00-04-004-06");
this.setResult(http_infos[18].substring(0,http_infos[18].indexOf("<")), UNIT_ID, "PM-00-04-004-07");
this.setResult(http_infos[22], UNIT_ID, "PM-00-04-800-03");
this.setResult(http_infos[26], UNIT_ID, "PM-00-04-800-04");
this.setResult(http_infos[30], UNIT_ID, "PM-00-04-800-05");
this.setResult(http_infos[33], UNIT_ID, "PM-00-04-800-06");
this.setResult(http_infos[36], UNIT_ID, "PM-00-04-800-07");
this.setResult(http_infos[40], UNIT_ID, "PM-00-04-800-08");
}catch(Exception e){
e.printStackTrace();
}
try{
UNIT_ID = this.KBP_CLASS+"-10:"+hostname;
this.setResult("UP", UNIT_ID, "FM-00-04-001-01");
}catch(Exception e){
e.printStackTrace();
}
}
} else {
System.out.println("index_http <=0");
}
} else {
// 未能请求到status_html信息。视为中间件当掉了.
//FM-00-04-001-01 10-12 应用服务状态
try{
UNIT_ID = this.KBP_CLASS+"-10:"+hostname;
this.setResult("DOWN", UNIT_ID, "FM-00-04-001-01");
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
/**
* request "url" with username and password to get response as String
* @param url
* @param username
* @param password
* @return String of response
*/
private String request(String url, String username, String password) {
StringBuffer sb = new StringBuffer();
try {
HttpMethod method = null;
HttpClient client = new HttpClient();
method = new GetMethod(url);
URL url1 = new URL(url);
password = DES3.decrypt(password);
Credentials defaultcreds = new UsernamePasswordCredentials(
username, password);
client.getState().setCredentials(
new AuthScope(url1.getHost(), url1.getPort(),
AuthScope.ANY_REALM), defaultcreds);
method.setDoAuthentication(true);
method.setQueryString(url);
client.executeMethod(method);
int status = method.getStatusCode();
// System.out.println("status="+status);
/*
* Header[] headers = method.getResponseHeaders(); for(int
* i=0;i<headers.length;i++){
* System.out.println(headers[i].getName()
* +":"+headers[i].getValue()); }
*/
if (status == 200) {
String s = method.getResponseBodyAsString();
sb.append(s);
} else {
logger.info("tomcat_status:"+status);
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
// logger.info("collTomcat's result :"+sb.toString());
return sb.toString();
}
private void setResult(String value, String unitID, String kpiID) {
try {
TblATO_KPIDETAIL record;
record = new TblATO_KPIDETAIL();
record.setCLL_TIME(new java.util.Date()); // 采集所消耗的时间
record.setKPI_ID(kpiID); // kpi编号
record.setKPI_VALUE(value); // 具体采集得到的值信息
record.setUNIT_ID(unitID); // unit
record.setCLL_TIME(new Date()); // 日期
result.add(record);
System.out.println("add record="+record.KPI_VALUE);
} catch (Exception e) {
logger.error(e);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CollTomcatStatusWithHttp cs = new CollTomcatStatusWithHttp();
/*String url = "http://192.168.3.254:8080/status";
String username="";
String password="";*/
HashMap map = new HashMap();
map.put("URL", "http://136.64.21.81:8080/manager/status");
map.put("USERNAME", "admin");
map.put("PASSWORD", "admin");
//System.out.println(cs.request(url, username, password));
cs.collStatus(map);
}
}