CollWeblogicWithAdmin.java
9.84 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
package com.sitech.ismp.coll.centercoll;
import java.util.Date;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import com.sitech.util.upload.RomoteController;
import com.sitech.util.upload.SSHThread;
import com.sitech.util.upload.TelnetThread;
public class CollWeblogicWithAdmin implements CollWeblogicMBean {
private Logger logger = Logger.getLogger("COLL");
String UNITID = "10-12-11"; // weblogic中间件标识
RomoteController tt = null;
String env_path = "";
String username = "";
String passwd = "";
String ip_addr = "";
String port = "";
String protocol = "";
String servicename = "";
String init_cmd = "";
private Vector result = new Vector();
public Vector coll(HashMap params) {
// TODO Auto-generated method stub
env_path = (String) params.get("ENV_PATH"); // 设置weblogic.admin环境变量的脚本路径
username = (String) params.get("USERNAME"); // weblogic用户
passwd = (String) params.get("PASSWORD"); // weblogic密码
ip_addr = (String) params.get("IP_ADDR"); // 监控weblogic所在主机
port = (String) params.get("PORT"); // weblogic管理端口
servicename = (String) params.get("SERVICENAME"); // weblogic服务名
protocol = (String) params.get("PROTOCOL"); // 登陆协议
initWeblogic(); // 实例化tt对象,设置weblogic.admin环境变量
collWeblogic();
return this.result;
}
public void initWeblogic() {
try {
init_cmd = "java weblogic.Admin -url " + ip_addr + ":" + port
+ " -username " + username + " -password "
+ DES3.decrypt(passwd);
if (protocol != null && protocol.equals("telnet")) {
tt = new TelnetThread(ip_addr, 23, username, passwd);
} else {
tt = new SSHThread(ip_addr, 22, username, passwd);
}
tt.initial();
tt.getResultAsStr(env_path); // 设置weblogic.admin环境变量
} catch (Exception e) {
e.printStackTrace();
}
}
public void collWeblogic() {
try {
// 连接weblogic server实例往返平均时长
collConnCost();
// 服务器的当前状态
// collRunState();
// 发送消息以验证 WebLogic Server 实例是否正在端口上监听且准备好接受 WebLogic 客户端请求
collPingCost();
// 采集软件版本信息
// collWeblogicVersion();
// 群集中服务器的编号和状态
collCluster();
// 采集服务器监听端口上的活动
collActiveProperty();
// 采集JDBC连接池
collJDBCDataSource();
// String unitID = "10-12-11-10-10:" + hostname + "-total";
// FM-00-04-003-03 主机状态 true 连接正常 false 连接失败可能宕机
// setResult("true", unitID, "FM-00-04-003-03");
} catch (Exception e) {
e.printStackTrace();
// 10-12-11-10-10------->10-12-11-11-10
} finally {
tt.close();
}
}
public void collConnCost() throws Exception {
String unit_id = UNITID + "-10:" + servicename + "-total";
// String path = tt.getResultAsStr("echo $PATH");
// String classpath = tt.getResultAsStr("echo $CLASSPATH");
String connCost = tt
.getResultAsStr(init_cmd
+ " CONNECT 10|tail -1|awk -F \"~\" '{print $3}'|awk '{print $1}'");
// 连接weblogic server实例往返平均时长(连接10次)
setResult(connCost, unit_id, "PM-00-04-003-03");
}
public void collPingCost() throws Exception {
String unit_id = UNITID + "-10:" + servicename + "-total";
String pingCost = tt
.getResultAsStr(init_cmd
+ " PING 10|tail -1|awk -F \"~\" '{print $3}'|awk '{print $1}'");
String state = "";
if (pingCost != null && pingCost.indexOf("Connection refused") > -1)
state = "DOWN";
else {// pingCost 格式:RTT = ~11 milliseconds, or ~1 milliseconds/packet
state = "ping时长:" + pingCost.trim() + " ms";
}
setResult(state, unit_id, "FM-00-04-001-01");// WebLogic Server
// 实例端口状态
}
public void collCluster() throws Exception {
String pre_unit_id = UNITID + "-11:" + servicename;
Vector cluster = tt.getResultAsVector(init_cmd + " CLUSTERSTATE");
String state = "";
String unit_id = "";
// 无集群配置
String cluster_info = (String) cluster.get(0);
if (cluster_info.indexOf("No clusters") > -1) {
state = "No clusters have been configured for this domain.";
unit_id = pre_unit_id + "-cluster";
setResult(state, unit_id, "FM-10-12-001-03"); // 集群状态
return;
}
if (cluster != null && cluster.size() > 0) {
for (int i = 0; i < cluster.size(); i++) {
cluster_info = (String) cluster.get(i);
// MedRecManagedServer2---RUNNING
if (cluster_info.indexOf("---") > -1) {
String cluster_name = cluster_info.split("---")[0];
String cluster_state = cluster_info.split("---")[1];
unit_id = pre_unit_id + "-" + cluster_name;
setResult(cluster_name, unit_id, "CM-10-12-001-03"); // 集群名称
setResult(cluster_state, unit_id, "FM-10-12-001-03"); // 集群状态
}
}
}
}
public void collActiveProperty() throws Exception {
String unit_id = UNITID + "-10:" + servicename + "-total";
Vector total = tt.getResultAsVector(init_cmd
+ " GET -pretty -type ServerRuntime "
+ "-property AdministrationURL " + "-property CachingDisabled "
+ "-property OpenSocketsCurrentCount "
+ "-property RestartsTotalCount " + "-property SSLListenPort "
+ "-property State " + "-property WeblogicVersion");
if (total != null && total.size() > 0) {
for (int i = 0; i < total.size(); i++) {
String property = (String) total.get(i);
if (property.indexOf("AdministrationURL") > -1)
setResult(property.substring(property.indexOf(":") + 1,
property.length()), unit_id, "CM-10-12-001-04");// 管理域地址
if (property.indexOf("CachingDisabled") > -1)
setResult(property.split(":")[1], unit_id,
"CM-10-12-001-07");// 缓存禁用配置
if (property.indexOf("OpenSocketsCurrentCount") > -1)
setResult(property.split(":")[1], unit_id,
"CM-10-12-001-08");// 当前打开的socket数量
if (property.indexOf("RestartsTotalCount") > -1)
setResult(property.split(":")[1], unit_id,
"CM-10-12-001-05");// 重启次数
if (property.indexOf("SSLListenPort") > -1)
setResult(property.split(":")[1], unit_id,
"CM-10-12-001-06");// SSL监听端口
if (property.indexOf("State") > -1)
setResult(property.split(":")[1], unit_id,
"FM-00-04-001-01");// 应用服务状态状态
if (property.indexOf("WeblogicVersion") > -1)
setResult(property.split(":")[1], unit_id,
"CM-00-02-001-03");// weblogic 版本
}
}
}
public void collJDBCDataSource() {
String pre_unit_id = UNITID + "-12:" + servicename;
String unit_id = "";
Vector jdbc_conn = tt.getResultAsVector(init_cmd
+ " GET -pretty -type JDBCConnectionPoolRuntime"
+ " -property Name"
+ " -property ActiveConnectionsCurrentCount"
+ " -property ConnectionDelayTime" + " -property CurrCapacity"
+ " -property State"
+ " -property WaitingForConnectionCurrentCount");
if (jdbc_conn != null && jdbc_conn.size() > 0) {
for (int i = 0; i < jdbc_conn.size(); i++) {
String property = (String) jdbc_conn.get(i);
if (property.indexOf("MBeanName") > -1) {
String conn_name = property.split("ApplicationRuntime=")[1]
.replaceAll("\"", "");
unit_id = pre_unit_id + "-"
+ conn_name.replaceAll(" ", "_");
} else if (property.indexOf(":") > -1) {
String name = property.split(":")[0].trim();
String value = property.split(":")[1].trim();
if (name.equals("Name"))
setResult(value, unit_id, "CM-10-12-001-09");// 连接池名称
if (name.equals("ActiveConnectionsCurrentCount"))
setResult(value, unit_id, "PM-10-12-001-10");// 当前活动连接计数
if (name.equals("ConnectionDelayTime"))
setResult(value, unit_id, "PM-10-12-001-11");// 当前活动连接计数
if (name.equals("CurrCapacity"))
setResult(value, unit_id, "PM-10-12-001-12");// 当前容量
if (name.equals("State"))
setResult(value, unit_id, "FM-10-12-001-12");// 状态
if (name.equals("WaitingForConnectionCurrentCount"))
setResult(value, unit_id, "PM-10-12-001-13");// 等待连接当前计数
} else
continue;
}
}
}
private void setResult(String value, String unitID, String kpiID) {
try {
TblATO_KPIDETAIL record;
record = new TblATO_KPIDETAIL();
record.setCLL_TIME(new java.util.Date()); // 采集所消耗的时间
record.setKPI_ID(kpiID); // kpi编号
record.setKPI_VALUE(value); // 具体采集得到的值信息
record.setUNIT_ID(unitID); // unit
record.setCLL_TIME(new Date()); // 日期
logger.info(unitID + "|" + kpiID + "|" + value + "|" + new Date());
result.add(record);
} catch (Exception e) {
e.printStackTrace();
}
}
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 displayKPI(Vector vKPISet) {
for (int i = 0; i < vKPISet.size(); i++) {
TblATO_KPIDETAIL tblato_kpidetail = (TblATO_KPIDETAIL) vKPISet
.elementAt(i);
if (tblato_kpidetail == null)
continue;
System.out.println(tblato_kpidetail.UNIT_ID + " | "
+ tblato_kpidetail.KPI_ID + " | "
+ tblato_kpidetail.KPI_VALUE + " | "
+ tblato_kpidetail.CLL_TIME);
}
}
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("IP_ADDR", "172.16.9.154");
map.put("PORT", "8099");
map.put("SERVICENAME", "adminserver");
map.put("ENV_PATH", "/weblogic/bea/weblogic92/server/bin/setWLSEnv.sh");
map.put("USERNAME", "weblogic");
map.put("PASSWORD", DES3.encrypt("weblogic"));
map.put("PROTOCOL", "ssh");
CollWeblogicWithAdmin coll = new CollWeblogicWithAdmin();
coll.coll(map);
coll.displayKPI(coll.result);
}
}