CollSdfsWithCMD.java
5.92 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
package com.sitech.ismp.coll.centercoll;
import com.sitech.ismp.coll.CollBase;
import com.sitech.util.DES3;
import com.sitech.util.Formater;
import com.sitech.util.upload.RomoteController;
import com.sitech.util.upload.SSHThread;
import com.sitech.util.upload.TelnetThread;
import org.apache.log4j.Logger;
import java.util.Date;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* Created by mooker on 15/6/23.
*/
public class CollSdfsWithCMD {
private static Logger logger = Logger.getLogger("COLL");
String PRE_UNITID = "10-32-17";
String SDFSTABLE = "sh #SDFS_HOME#tools/GetHtableInfo.sh 2>/dev/null";
String SDFSFILENUM = "sh #SDFS_HOME#tools/GetFileNum.sh #FILESYSTEM#";
RomoteController tt = null;
String neat_host_name = "";
String hostname = "";
String ipAddr;
String sdfshome = "";
Vector result = new Vector();
String value = null;
boolean isConnect = false;
boolean flag = false;
public String split(String str, int pos) {
StringTokenizer st = new StringTokenizer(str);
Vector values = new Vector();
while (st.hasMoreTokens()) {
values.add(st.nextToken());
}
if (pos >= 0 && pos < values.size()) {
return (String) values.elementAt(pos);
}
return "";
}
public void init(HashMap params) {
String user = null;
String passwd = null;
String protol = null;
if (this.flag) {
ipAddr = (String) params.get("PING_IP_ADDR");
user = (String) params.get("PING_USERNAME");
passwd = (String) params.get("PING_PASSWORD");
protol = (String) params.get("PING_PROTOCOL");
} else {
ipAddr = (String) params.get("IP_ADDR");
user = (String) params.get("USERNAME");
passwd = (String) params.get("PASSWORD");
protol = (String) params.get("PROTOCOL");
}
passwd = DES3.decrypt(passwd);
hostname = (String) params.get("SDFSNAME");
this.neat_host_name = Formater.neatenunitid(hostname);
sdfshome = (String) params.get("SDFSHOME");
if (sdfshome != null && !sdfshome.endsWith("/")) {
sdfshome = sdfshome + "/";
}
String protocolPort = null;
if (params.get("PROTOCOL_PORT") != null) {
protocolPort = ((String) params.get("PROTOCOL_PORT")).trim();
}
if (protol != null && protol.equals("telnet")) {
if (null != protocolPort && !"".equals(protocolPort)) {
tt = new TelnetThread(ipAddr, Integer.parseInt(protocolPort), user, passwd);
} else {
tt = new TelnetThread(ipAddr, 23, user, passwd);
}
} else {
if (null != protocolPort && !"".equals(protocolPort)) {
tt = new SSHThread(ipAddr, Integer.parseInt(protocolPort), user, passwd);
} else {
tt = new SSHThread(ipAddr, 22, user, passwd);
}
}
tt.initial();
this.isConnect = tt.isAuthorized();
}
public void release() {
if (tt != null) {
tt.close();
tt = null;
}
System.gc();
}
public Vector collHtable(HashMap params) {
logger.info("begin getSDFS : ");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
System.out.println(new Date() + " " + this.SDFSTABLE.replace("#SDFS_HOME#", sdfshome));
Vector sdfsTable = tt.getResultAsVector(this.SDFSTABLE.replace("#SDFS_HOME#", sdfshome));
System.out.println(new Date() + " GetHtableInfo execute end, " + sdfsTable.size());
if (!sdfsTable.isEmpty()) {
for (int i = 3; i < sdfsTable.size(); i++) {
String _sdfsTable = sdfsTable.get(i).toString();
if (null != _sdfsTable && !_sdfsTable.equals("") && _sdfsTable.length() > 0) {
String filesystem = split(_sdfsTable, 0);
String unitId = PRE_UNITID + "-11:" + neat_host_name + "-" + filesystem;
String total = split(_sdfsTable, 1);
String used = split(_sdfsTable, 2);
String free = split(_sdfsTable, 3);
String usedrate = split(_sdfsTable, 4);
System.out.println("SDFSFILENUM is: " + this.SDFSFILENUM.replace("#SDFS_HOME#", sdfshome).replace("#FILESYSTEM#", filesystem));
Vector sdfsFileNum = tt.getResultAsVector(this.SDFSFILENUM.replace("#SDFS_HOME#", sdfshome).replace("#FILESYSTEM#", filesystem));
if (!sdfsFileNum.isEmpty()) {
// System.out.print("sdfsFileNum is: " + sdfsFileNum.get(0).toString() + "【】");
collResult.addKPI(unitId, "PM-32-00-001-04", sdfsFileNum.get(0).toString());
}
// System.out.println(filesystem + " " + total + " " + used + " " + free + " " + usedrate);
collResult.addKPI(unitId, "CM-32-00-001-01", filesystem);
collResult.addKPI(unitId, "CM-32-00-001-02", total);
collResult.addKPI(unitId, "PM-32-00-001-01", Formater.convertKBMB2GB((used.substring(0, used.length() - 1)), used.substring(used.length() - 1)));
collResult.addKPI(unitId, "PM-32-00-001-02", Formater.convertKBMB2GB((free.substring(0, free.length() - 1)), free.substring(free.length() - 1)));
collResult.addKPI(unitId, "PM-32-00-001-03", usedrate);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
release();
}
logger.info("end getDemsg : " + params.get("DEMSG_KEY"));
return collResult.getKPISet();
}
}