CollUnionWorker_NEW.java
4.81 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
/**
* 功能:并发监控agent所在主机的主机状态,实现ping并生成指标文件
* 作者:毛志荣
* 日期:2008-5-2 12:34
* 版本:v1.00
*/
package com.sitech.ismp.coll;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
public class CollUnionWorker_NEW implements Runnable {
private String host;
private String unit_id;
private String flag;
private int count = 0;
public CollUnionWorker_NEW(String host, String unit_id, String flag) {
this.host = host;
this.unit_id = unit_id;
this.flag = flag;
}
/** * -- 构造ping命令 --- * */
public synchronized void run() {
String cmd = "";
System.out.println();
if (CollUnion_NEW.host_type.equalsIgnoreCase("hp")) {
cmd = "ping " + this.host + " -n " + CollUnion_NEW.times;
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("aix")) {
cmd = "ping -c " + CollUnion_NEW.times + " " + this.host;
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("sun")) {
cmd = "ping -s " + this.host + " " + CollUnion_NEW.times + " "
+ CollUnion_NEW.count;
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("linux")) {
cmd = "ping -c " + CollUnion_NEW.times + " " + this.host;
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("windows")) {
cmd = "ping -n " + CollUnion_NEW.times + " " + this.host;
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("sco")) {
}
System.out
.println("[begin moni " + new Date() + " " + this.host + "] ");
String loss = ping(cmd);
if (flag.equals("0") && loss.equals("100")) {
loss = "UP";
} else if (flag.equals("1") && loss.equals("100")) {
// Thread.sleep(5000L);
// String lossother = ping(cmd);
if (loss.equals("100")) {
loss = "DOWN";
} else {
loss = "UP";
}
} else if (flag.equals("1") && loss.equals("0")) {
System.out.print(loss.equals("falg=1的主机可以PING0"));
loss = "UP";
} else if (flag.equals("0") && loss.equals("0")) {
System.out.print(loss.equals("falg=1的主机可以PING0"));
loss = "DOWN";
}
System.out.println("[end " + this.host + new Date() + " ,restult="
+ loss + "]");
create_kpi_files(this.unit_id, loss, this.host);
}
/**
* 执行ping命令
*
* @param cmd
* @return
*/
private synchronized String ping(String cmd) {
BufferedReader br = null;
String loss = "";
try {
System.out.println(cmd);
Process p = Runtime.getRuntime().exec(cmd);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = br.readLine();
System.out.println("读出数据data" + line);
while ((line = br.readLine()) != null) {
if (line.indexOf("%") != -1) { // 根据不同主机类型取出不同丢包率
System.out.println(line);
if (CollUnion_NEW.host_type.equalsIgnoreCase("hp")) {
loss = line.substring(line.indexOf("received") + 9,
line.indexOf("%")).trim();
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("sun")) {
loss = line.substring(line.indexOf("received") + 9,
line.indexOf("%")).trim();
System.out.println("ping sun=" + loss);
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("linux")) {
loss = line.substring(line.indexOf("received") + 9,
line.indexOf("%")).trim();
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("windows")) {
loss = line.substring(line.indexOf("(") + 1,
line.indexOf("%")).trim();
}
if (CollUnion_NEW.host_type.equalsIgnoreCase("aix")) {
loss = line.substring(line.indexOf("received") + 9,
line.indexOf("%")).trim();
System.out.println("ping aix=" + loss);
}
break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("error:" + e.toString());
} finally {
try {
br.close();
Runtime.getRuntime().gc();
} catch (IOException e1) {
e1.printStackTrace();
}
}
// if (!loss.equals("100")) {
// return loss;
// }
//
// while (this.count < 2) {
// try {
// Thread.sleep(1000 * 3); // 停留5秒,再继续
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// this.count++;
// System.out.println(" Again " + this.count + " " + this.host
// + " ping]");
// ping(cmd);
// }
return loss;
}
/**
* 生成指标文件
*/
private synchronized boolean create_kpi_files(String unit_id, String value,
String host_ip) {
String cmd = "";
String kpi_cmd = "";
cmd = CollUnion_NEW.swap_up_cmd;
kpi_cmd = cmd + " " + unit_id + " " + CollUnion_NEW.kpi_id + " "
+ value;
System.out.println("--" + kpi_cmd);
try {
Runtime.getRuntime().exec(kpi_cmd);
} catch (Exception e) {
System.out.println("error:" + e.toString());
return false;
} finally {
try {
Runtime.getRuntime().gc();
System.gc();
} catch (Exception e1) {
e1.printStackTrace();
}
}
return true;
}
}