HttpCollThread.java
5.1 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
package com.sitech.ismp.coll.busi;
import java.util.HashMap;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.CollBase;
import com.sitech.util.Formater;
/**
* ClassName:HttpCollThread
* Description: HTTP采集
*
* @author Linxc
* @version
* @since Ver 1.1
* @Date 2012 Feb 23, 2012 9:43:02 AM
*/
public class HttpCollThread implements Runnable {
static Logger logger = Logger.getLogger("BUSI_COLL");
/** 业务类型 */
private String unitId;
/** 根据关键字过滤 */
private String key;
/** post/get */
private String type;
private String url;
/** 超时时间 */
private int timeout;
/** 参数 */
private NameValuePair[] nameValuePair;
/** 采集周期 */
private String interval;
public HttpCollThread(HashMap<String, String> params) {
interval = params.get("COLL_INTERVAL");
key = params.get("CONTAINSKEYWORD");
// post/get
type = params.get("MEANS").toLowerCase();
url = params.get("ADDR");
String kbpClass = params.get("KBPCLASS");
unitId = kbpClass + ":" + Formater.neatenunitid(url);
String sTimeout = params.get("TIMEOUT");
if (sTimeout == null || sTimeout.trim().equals("")) {
timeout = 10000;
} else {
timeout = Integer.parseInt(sTimeout) * 1000;
}
String sHttpParams = params.get("PARAMS");
if (sHttpParams != null && !sHttpParams.trim().equals("")
&& !sHttpParams.trim().equals("null")) {
String httpParams[] = sHttpParams.split("#");
int paramsCount = httpParams.length;
nameValuePair = new NameValuePair[paramsCount];
for (int i = 0; i < paramsCount; i++) {
String key = httpParams[i].split("=")[0];
String value = httpParams[i].split("=")[1];
nameValuePair[i] = new NameValuePair(key, value);
}
}
}
public void run(){
logger.info("Begin collHttp...");
CollBase collResult = new CollBase();
HttpClient client = new HttpClient();
String content = "";
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
if (type != null && type.toLowerCase().equals("post")) {
// 通过Post方式探测
PostMethod method = new PostMethod(url);
method.addRequestHeader("SOAPAction", "");
if (nameValuePair != null && nameValuePair.length > 0) {
method.setRequestBody(nameValuePair);
}
try {
client.executeMethod(method);
content = method.getResponseBodyAsString();
} catch (Exception e) {
logger.error("Exception while collHttp", e);
}
}else{
// 通过Get方式探测
GetMethod method = new GetMethod(url);
method.addRequestHeader("SOAPAction", "");
try {
client.executeMethod(method);
content = method.getResponseBodyAsString();
} catch (Exception e) {
logger.error("Exception while collHttp", e);
}
}
logger.info(content);
String info = "";
if(content == null || content.trim().equals("")){
info = "FAIL";
}else if (key != null && !key.equals("") && !key.equals("null")) {
// 根据关键字判断
if (content.indexOf("SAXParseException") == -1 && content.indexOf("Error 404") == -1 && content.indexOf("Error 500") == -1) {
String[] keyArray = key.split(",");
for (int i = 0; i < keyArray.length; i++) {
if (content.indexOf(key) < 0) {
info = "FAIL";
}
}
info = "SUCCESS";
} else if (content.indexOf("Error 4") != -1) {
info = "FAIL";
// info = "找不到对应请求地址!";
} else if (content.indexOf("Error 5") != -1) {
info = "FAIL";
// info = "访问服务器失败,请检查请求的URL是否合法!";
} else if (content.indexOf("SAXParseException") != -1) {
info = "FAIL";
// info = "报文格式错误,请核对报文格式是否合法!";
}
} else {
// 没有关键字,直接返回成功/失败
if (content.indexOf("SAXParseException") == -1 && content.indexOf("Error 404") == -1 && content.indexOf("Error 500") == -1) {
info = "SUCCESS";
// info = content;
} else if (content.indexOf("Error 4") != -1) {
info = "FAIL";
// info = "找不到对应请求地址!";
} else if (content.indexOf("Error 5") != -1) {
info = "FAIL";
// info = "访问服务器失败,请检查请求的URL是否合法!";
} else if (content.indexOf("SAXParseException") != -1) {
info = "FAIL";
// info = "报文格式错误,请核对报文格式是否合法!";
}
}
collResult.addKPI(unitId, "CM-01-11-009-01", url, interval);
collResult.addKPI(unitId, "PM-01-11-009-01", info, interval);
collResult.saveKPI2File();
collResult.displayKPI();
logger.info("End collHttp...");
}
public static void main(String[] args) {
HashMap<String, String> params = new HashMap<String, String>();
params.put("COLL_INTERVAL", "30000");
params.put("CONTAINSKEYWORD", "综合运用管理门户");
params.put("MEANS", "Get");
params.put("ADDR", "http://172.21.0.117:8161/");
params.put("KBPCLASS", "11-10");
params.put("HTTP_PARAMS", "");
new HttpCollThread(params).run();
}
}