NetConnState.java
8.67 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Created on 2006-8-1 NetConnState.java responsibility :maozr base class: ping
* destination net address ,return package lost rate
*
*/
package com.sitech.ismp.coll;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.apache.log4j.Logger;
public class NetConnState {
private Logger logger = Logger.getLogger("COLL");
private NetConnObject NetConnObject = null;
final boolean UNIX = true;// unix
private String IP = null;
/**
* this ip is you will ping
*
* @param ip
*/
public NetConnState(String ip) {
if (ip != null && ip.length() > 0) {
NetConnObject = new NetConnObject();
IP = ip;
} else {
logger.error("NetConnState IP IS NULL,Failture!!!");
}
}
public NetConnObject getListenState() {
logger.info("[hosttype:" + getHostType() + "]");
listen(getHostType());
return NetConnObject;
}
/*
* private String getCmd(String serverName) { String result = null; if
* (serverName != null && serverName.length() > 0) { if
* (serverName.equals("HP")) { result = "ping " + IP + " -n 6"; } if
* (serverName.equals("WINDOWS")) { result = "ping -n 6 " + IP; } if
* (serverName.equals("AIX")) { result = "ping -c 6 " + IP; } } return
* result; }
*/
/**
* @author Administrator
* @param host
* @version 1.0 2006-08-03 listen the others host
*/
private void listen(String host) {
String result = null;
String cmd = null;
String ip[] = null;
int clock = 1;
if (host != null && host.length() > 0) {
ip = IP.split(",");
for (int i = 0; i < ip.length; i++) {
clock++;
if (host.equals("hp")) {
cmd = "ping " + ip[i] + " -n 3";
logger.info("ping first-" + cmd);
result = ListenUNIXServer(cmd);
if (result.equalsIgnoreCase("down")) { // update by
// maozr@2008312,为down再执行三遍
for (int j = 0; j < 3; j++) {
logger.info("ping second-" + j + ":" + cmd);
result = ListenUNIXServer(cmd);
if (result.equalsIgnoreCase("up")) {
break;
}
}
}
}
if (host.equals("windows")) {
cmd = "ping -n 3 " + ip[i];
logger.info("ping-" + cmd);
result = ListenWin32Server(cmd); // WINDOWS CALL
for (int j = 0; j < 3; j++) {
logger.info("ping second-" + j + ":" + cmd);
result = ListenUNIXServer(cmd);
if (result.equalsIgnoreCase("up")) {
break;
}
}
}
if (host.equals("aix")) {
cmd = "ping -c 3 " + ip[i];
logger.info("ping-" + cmd);
result = ListenUNIXServer(cmd);
for (int j = 0; j < 3; j++) {
logger.info("ping second-" + j + ":" + cmd);
result = ListenUNIXServer(cmd);
if (result.equalsIgnoreCase("up")) {
break;
}
}
}
if (result != null && result.equalsIgnoreCase("UP"))
;
NetConnObject.setLostRate("UP");
break;
}
if (clock == ip.length && result != null && result.equals("DOWN"))
result = "DOWN";
logger.info("----[" + IP + "]---final result----[" + result + "]");
NetConnObject.setLostRate(result);
}
}
/**
* UNIX SYS call this Method fun is listen and ping destination Address
* example for ping 172.16.9.90 command param '-n 6' must to place at last
* position
*/
private String ListenUNIXServer(String cmd) {
BufferedReader br = null;
String result = null;
try {
Process p = Runtime.getRuntime().exec(cmd);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = br.readLine();
while ((line = br.readLine()) != null) {
if (line.trim().indexOf("packets") != -1
&& !line.trim().endsWith("packets")) {
result = setResultPackage(line, "UNIX");
}
}
} catch (Exception e) {
logger.error("error:" + e.toString());
} finally {
try {
br.close();
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return result;
}
/**
* WINDOWS SYS call this Method for example: ping -n 6 172.16.9.90
*
*/
private String ListenWin32Server(String cmd) {
BufferedReader br = null;
String result = null;
try {
Process p = Runtime.getRuntime().exec(cmd);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = br.readLine();
while ((line = br.readLine()) != null) {
logger.info(line);
if (line.trim().startsWith("Packets")) {
result = setResultPackage(line);
}
if (line.trim().startsWith("Minimum")) {
setResultTime(line);
}
}
} catch (Exception e) {
logger.error("ListenWin32Server:" + e.toString());
} finally {
try {
br.close();
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
} catch (IOException e1) {
logger.error("ListenWin32Server:" + e1.toString());
}
}
return result;
}
/**
* express: Process Package value and set it into NetConnObject param str
* instand of package value it's called WINDOWS platform
*
* @version 1.0 this method no return value
* @version 1.1 06-08-03 add this return param type String
*
*/
private String setResultPackage(String str) {
String allMessage[] = null;
String subMessage[] = null;
int flag = 0;
int subFlag = 1;
String result = "none"; // regard one host have more MAC,will to split
// more IP
try {
if (str != null && str.length() > 0) {
allMessage = str.split(",");
while (flag < allMessage.length) {
subMessage = allMessage[flag].split("=");
if (flag == 0)
NetConnObject.setSend(subMessage[subFlag]);
if (flag == 1)
NetConnObject.setReceived(subMessage[subFlag]);
if (flag == 2) {
int p = subMessage[subFlag].indexOf("%");
int pp = subMessage[subFlag].indexOf("(");
if (p != -1) {
if (Integer.parseInt(subMessage[subFlag].trim()
.substring(pp, p - 1)) == 100) {
result = "DOWN";
} else {
result = "UP";
}
NetConnObject.setLost(subMessage[subFlag].trim()
.substring(0, pp - 1));
}
}
flag++;
}
}
} catch (Exception e) {
logger.error("setResultPackage:" + e.toString());
}
return result;
}
/**
* express: get sent and received value and put into NetConnObject in
* accordance with UNIX SYS
*
* @param str
* @param system
* @version 1.0
* @version 1.1 2006-08-03 add return param
*/
public String setResultPackage(String str, String system) {
String allMessage[] = null;
int flag = 0;
String result = "none";
try {
if (str != null && str.length() > 0) {
allMessage = str.trim().split(",");
while (flag < allMessage.length) {
if (flag == 0)
NetConnObject.setSend(allMessage[flag].substring(0,
allMessage[flag].indexOf("packets")));
if (flag == 1)
NetConnObject.setReceived(allMessage[flag].substring(0,
allMessage[flag].indexOf("packets")));
if (flag == 2) {
if (Integer.parseInt(allMessage[flag].substring(0,
allMessage[flag].indexOf("%")).trim()) == 100) {
result = "DOWN";
} else {
result = "UP";
}
NetConnObject.setLost(allMessage[flag].substring(0,
allMessage[flag].indexOf("packet")));
}
flag++;
}
}
} catch (Exception e) {
logger.error("setResultPackage:" + e.toString());
}
logger.info("[" + this.IP + "],[" + str + "],[" + result + "]");
return result;
}
/**
* Minimum = 0ms, Maximum = 0ms, Average = 0ms
*
* @param str
*/
private void setResultTime(String str) {
int flag = 0;
int subflag = 1;
String callTime[] = null;
String subTime[] = null;
try {
if (str != null && str.length() > 0) {
callTime = str.split(",");
while (flag < callTime.length) {
subTime = callTime[flag].split("=");
if (flag == 0)
NetConnObject.setMinimum(subTime[subflag]);
if (flag == 1)
NetConnObject.setMaximum(subTime[subflag]);
if (flag == 2)
NetConnObject.setAverage(subTime[subflag]);
flag++;
}
}
} catch (Exception e) {
logger.error("setResultTime:" + e.toString());
}
}
public String getHostType() {
String s_config = "/NetListenType.properties";
String host = "";
InputStream in = this.getClass().getResourceAsStream(s_config);
if (in == null) {
logger.info(" OPEN " + s_config + " Failture!");
} else {
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
logger.error("get NETConnTYpe ERROR");
}
host = properties.getProperty("Host_Type");
}
return host;
}
public static void main(String[] args) {
String str = new NetConnState("localhost").setResultPackage(
"6 packets transmitted, 6 packets received, 99% packet loss",
"HP");
System.out.println(str);
}
}