CollLinuxHost.java
11 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
339
340
341
342
343
package com.sitech.ismp.coll.centercoll;
import com.sitech.ismp.check.downtime.CheckConnectionState;
import com.sitech.ismp.check.downtime.ParamterBean;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Vector;
public class CollLinuxHost implements CollLinuxHostMBean {
private static Logger log= Logger.getLogger(CollLinuxHost.class);
CheckConnectionState server = null;
String kbpClass="10-10-24-10"; // linux 主机的标识
public CollLinuxHost() {
log.info("create CollLinuxHost ");
server = new CheckConnectionState();
}
public Vector collLinux(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.collLinux(params);
}
public Vector getConfig(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getConfig(params);
}// end getConfig
public Vector getCpu(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getCpu(params);
}
public Vector getMemory(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getMemory(params);
}
public Vector getDisk(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getDisk(params);
}// end getDisk
public Vector getFileSystem(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getFileSystem(params);
}
public Vector getUserProNum(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getUserProNum(params);
}
public Vector getSyslog(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getSyslog(params);
}
public Vector getSpecProcess(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getSpecProcess(params);
}
public Vector getBusiProcess(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getBusiProcess(params);
}
public Vector getPortListenings(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getPortListenings(params);
}
@Override
public Vector getProcessTop5Cpu(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getProcessTop5Cpu(params);
}
@Override
public Vector getProcessTop5Mem(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getProcessTop5Mem(params);
}
public Vector getSyslogBylogPathUseGrep(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getSyslogBylogPathUseGrep(params);
}
/**
* 获取linux集中式采集的 ping 连接状态
* @param params
* @return
*/
public Vector getPing(HashMap params) {
Vector<TblATO_KPIDETAIL> v=new Vector<TblATO_KPIDETAIL>();
String isPing = params.get("isPing")+""; // frank 2017-05-19 添加。Y进行ping操作,N不执行
if("Y".equals(isPing)){
String ip=params.get("IP_ADDR")+"";
if(!StringUtils.isEmpty(ip)){
String connectStatCode="FM-00-10-001-999"; // linux 主机连接状态
String hostName=params.get("HOSTNAME")+""; // 资源标识
String unitId = kbpClass+":" + hostName + "-total";
ParamterBean bean =new ParamterBean();
bean.setDEVICE_IP(ip);
bean.setType("linux");
log.info("*** execute linux getPing ** ip=["+ip+"]** unitId=["+unitId+"]*********");
/**
* getPingState 方法执行后会将状态插入到表中,然后取当前时间
* 根基IP查询失败的次数,如果失败次数为3次,则确定为宕机,发送告警。
*/
CheckConnectionState server=new CheckConnectionState();
server.getPingState(bean);
// 根据IP获取ping的错误次数。如果是 DOWN 的话,发送告警信息。
String state=server.getPingErrorState(ip);
TblATO_KPIDETAIL kpidetail= new TblATO_KPIDETAIL();
log.info("***** unitId**"+unitId+"** state **"+state+"*******");
try {
kpidetail.setKPI_ID(connectStatCode);
kpidetail.setUNIT_ID(unitId);
kpidetail.setKPI_VALUE(state);
kpidetail.setCLL_TIME(new Date());
} catch (ParseException e) {
e.printStackTrace();
}
v.add(kpidetail);
}
}
return v;
}
/**
* 获取linux集中式采集的ssh连接状态
* @param params
* @return
*/
@Override
public Vector getSSH(HashMap params) {
Vector<TblATO_KPIDETAIL> v=new Vector<TblATO_KPIDETAIL>();
String ip=params.get("IP_ADDR")+"";
String loginStatCode="FM-00-10-001-998"; // linux 主机登录状态
ParamterBean bean =new ParamterBean();
String tempPort=params.get("PROTOCOL_PORT")+"";
int port=0;
if(!StringUtils.isEmpty(tempPort)){
port=Integer.parseInt(tempPort);
}
String hostName=params.get("HOSTNAME")+"";
String username=params.get("USERNAME")+"";
String protocol=params.get("PROTOCOL")+"";
String password=params.get("PASSWORD")+"";
password=DES3.decrypt(password);
String unitId = kbpClass+":" + hostName + "-total";
log.info("*** execute linux getPing ** ip=["+ip+"]** unitId=["+unitId+"]**port="+port+"***hostName="+hostName+"****");
bean.setDEVICE_IP(ip);
bean.setPROTOCOL_PORT(port);
bean.setUSER_NAME(username);
bean.setPASSWORD(password);
bean.setPROTOCOL(protocol);
String state=server.getConnectionState4SshOrTelnet(bean);
TblATO_KPIDETAIL kpidetail=new TblATO_KPIDETAIL();
try {
kpidetail.setKPI_ID(loginStatCode);
kpidetail.setUNIT_ID(unitId);
kpidetail.setKPI_VALUE(state);
kpidetail.setCLL_TIME(new Date());
} catch (ParseException e) {
e.printStackTrace();
}
v.add(kpidetail);
return v;
}
public Vector getDemsg(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getDemsg(params);
}
public Vector getNet(HashMap params) {
CollLinuxWithCMD cmd = new CollLinuxWithCMD();
return cmd.getNet(params);
}
public static void main(String[] args) {
System.out
.println("***************Linux Center Collect Test Begin*********************");
//System.out.println(DES3.decrypt("cloud"));
// String ipAddr = "172.21.3.38";
// String username = "root";
// String password = "111111";
// String protocal = "ssh";
// String hostName = "app-123";
String ipAddr = "172.21.0.201";
String username = "ssdbomc";
String password = "bomcssd";
String protocal = "ssh";
String hostName = "172_21_0_201";
String processName = "pf,tomcat";
String listeningPort = "22,48781";
String correctListening = "1,1";
String pingIp = "172.21.0.201";
String pingUsername = "ssdbomc";
String pingPwd = "bomcssd";
String pingProtocal = "ssh";
String pointIPs = "172.21.0.31,172.21.0.203";
String demsgKey = "Detected Tx Unit Hang";
// String ipAddr = read("IP_ADDR(remote host ip):\n");
// String username = read("USERNAME:\n");
// String password = read("PASSWORD:\n");
// String protocal = read("PROTOCAL(ssh/telent):\n");
// String hostName = read("HOST_NAME(remote host name):\n");
// String processName = read("PROCESS_NAME:\n");
// String listeningPort = read("LISTENING_PORT:\n");
// String correctListening = read("CORRECT_LISTENING:\n");
// String methodStr = read("METHOD(0:getAll, 1:getConfig, 2:getCpu, 3:getMemory, 4:getDisk, " +
// "5:getFileSystem, 6:getPortListenings, 7:getBusiProcess, 8:getProcessTop5Mem, 9:getProcessTop5Cpu):\n");
String methodStr = "13";
System.out.println("IP_ADDR=" + ipAddr + ", USERNAME=" + username
+ ", PASSWORD=" + password + ", PROTOTAL=" + protocal
+ ", HOST_NAME=" + hostName + ", METHOD=" + methodStr);
HashMap<String, String> params = new HashMap<String, String>();
params.put("IP_ADDR", ipAddr);
params.put("USERNAME", username);
params.put("PASSWORD", DES3.encrypt(password));
params.put("PROTOCOL", protocal);
params.put("HOSTNAME", hostName);
params.put("PROCESS_NAME", processName);
params.put("LISTENING_PORT", listeningPort);
params.put("CORRECT_LISTENING", correctListening);
params.put("PING_IP_ADDR",pingIp);
params.put("PING_USERNAME",pingUsername);
params.put("PING_PASSWORD",DES3.encrypt(pingPwd));
params.put("PING_PROTOCOL",pingProtocal);
params.put("POINT_IPS", pointIPs);
params.put("DEMSG_KEY", demsgKey);
int method = Integer.parseInt(methodStr);
Vector<TblATO_KPIDETAIL> result = new Vector<TblATO_KPIDETAIL>();
CollLinuxHost collector = new CollLinuxHost();
switch (method) {
case 0:
result.addAll(collector.getConfig(params));
result.addAll(collector.getCpu(params));
result.addAll(collector.getMemory(params));
result.addAll(collector.getDisk(params));
result.addAll(collector.getFileSystem(params));
result.addAll(collector.getPortListenings(params));
result.addAll(collector.getBusiProcess(params));
result.addAll(collector.getProcessTop5Mem(params));
result.addAll(collector.getProcessTop5Cpu(params));
break;
case 1:
result.addAll(collector.getConfig(params));
break;
case 2:
result.addAll(collector.getCpu(params));
break;
case 3:
result.addAll(collector.getMemory(params));
break;
case 4:
result.addAll(collector.getDisk(params));
break;
case 5:
result.addAll(collector.getFileSystem(params));
break;
case 6:
result.addAll(collector.getPortListenings(params));
break;
case 7:
result.addAll(collector.getBusiProcess(params));
break;
case 8:
result.addAll(collector.getProcessTop5Mem(params));
break;
case 9:
result.addAll(collector.getProcessTop5Cpu(params));
break;
case 10:
result.addAll(collector.getDemsg(params));
break;
case 11:
result.addAll(collector.getPing(params));
break;
case 12:
result.addAll(collector.getUserProNum(params));
break;
case 13:
result.addAll(collector.getNet(params));
break;
default:
break;
}
System.out
.println("***************Linux Center Collect Test End*********************");
System.out.println();
for (int i = 0; i < result.size(); i++) {
TblATO_KPIDETAIL record = (TblATO_KPIDETAIL) result.get(i);
// System.out.println(record.UNIT_ID + "\t" + record.KPI_ID + "\t" + record.KPI_VALUE);
String insSql = "INSERT INTO TB_ATO_KPIDETAIL (KPI_ID, UNIT_ID, CLL_TIME, KPI_VALUE, EXT_INFO, DB_TIME, EXT_UNIT_ID, `INTERVAL`) " +
"VALUES ('#KPI_ID#', '#UNIT_ID#', now(), '#KPI_VALUE#', null, now(), '10-10-24:172_21_0_201', #INTERVAL#);";
insSql = insSql.replace("#KPI_ID#", record.KPI_ID);
insSql = insSql.replace("#UNIT_ID#", record.UNIT_ID);
insSql = insSql.replace("#KPI_VALUE#", record.KPI_VALUE);
insSql = insSql.replace("#INTERVAL#", record.INTERVAL);
System.out.println(insSql);
}
}
private static String read(String prompt) {
Scanner scanner = new Scanner(System.in);
System.out.print(prompt);
return scanner.nextLine();
}
}