CollJbossStatusWithHttp.java
5.57 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
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;
public class CollJbossStatusWithHttp implements CollJbossStatusMBean {
private Logger logger = Logger.getLogger("COLL");
private Vector result = new Vector();
private String KBP_CLASS = "10-12-15";
public CollJbossStatusWithHttp() {
}
public Vector collStatus(HashMap params) {
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("")) {
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);
String[] jvm_infos = jvm_info.trim().split(" ");
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();
}
}
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);
String[] http_infos = http_info.trim().split(" ");
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 {
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
*/
public 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);
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 {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
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);
record.setKPI_VALUE(value);
record.setUNIT_ID(unitID);
record.setCLL_TIME(new Date());
result.add(record);
System.out.println("add record=" + record.KPI_VALUE);
} catch (Exception e) {
logger.error(e);
}
}
}