CollStorageWithSNMP.java
7.27 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
package com.sitech.ismp.coll.storage;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import com.sitech.util.Formater;
import com.sitech.util.LogUtil;
import com.sitech.util.SNMPClient;
import org.apache.log4j.Logger;
import org.apache.velocity.util.StringUtils;
import org.snmp4j.mp.SnmpConstants;
import java.util.HashMap;
import java.util.Vector;
/**
* Created by IntelliJ IDEA.
* User: liush_bj
* Date: 2014-10-16
*/
public class CollStorageWithSNMP extends CollBase{
private String kbpClass;
private String ipAddr;
private String deviceName;
private int snmpVersion;
private int snmpPort;
private String snmpCommunity;
private SNMPClient client;
private String sysObjectID;
private boolean isConn = false;
private boolean isSupportSnmp = false;
public Vector<TblATO_KPIDETAIL> getTotal(HashMap<String, String> params)
throws Exception {
init(params);
String unitId = kbpClass + "-10" + ":" + deviceName + "-total";
String status = "UP";
try {
connect();
// CM-01-04-001-01 设备名称
super.addKPI(unitId, "CM-01-04-001-01", params.get("HOST_NAME"));
// CM-01-04-001-02 IP地址
super.addKPI(unitId, "CM-01-04-001-02", ipAddr);
// CM-01-04-001-03 设备软件版本
super.addKPI(unitId, "CM-01-04-001-03", getSoftVersion());
//cluster索引
super.addKPI(unitId,"CM-01-04-001-04", client.get(".1.3.6.1.4.1.7564.18.1.3.1.1.0"));
//clusterID
super.addKPI(unitId,"CM-01-04-001-05", client.get(".1.3.6.1.4.1.7564.18.1.3.1.2.0"));
//cluster状态
super.addKPI(unitId,"FM-01-04-001-02", client.get(".1.3.6.1.4.1.7564.18.1.3.1.3.0"));
} catch (Exception e) {
status = e.getMessage();
LogUtil.error("Exception while getTotal", e);
} finally {
// FM-01-04-001-01 设备状态
super.addKPI(unitId, "FM-01-04-001-01", status);
destroy();
}
return super.KPISet;
}
public Vector<TblATO_KPIDETAIL> getHPXPInfo(HashMap<String, String> params) throws Exception {
init(params);
try {
connect();
String arrayUnitId = kbpClass + "-11" + ":" + deviceName;
super.addKPI(arrayUnitId,"PM-00-07-001-01", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.2"));
super.addKPI(arrayUnitId,"PM-00-07-001-02", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.3"));
super.addKPI(arrayUnitId,"PM-00-07-001-03", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.4"));
super.addKPI(arrayUnitId,"PM-00-07-001-04", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.5"));
super.addKPI(arrayUnitId,"PM-00-07-001-05", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.6"));
super.addKPI(arrayUnitId,"PM-00-07-001-06", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.7"));
super.addKPI(arrayUnitId,"PM-00-07-001-07", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.8"));
super.addKPI(arrayUnitId,"PM-00-07-001-08", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.6.1.9"));
super.addKPI(arrayUnitId,"PM-00-07-001-09", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.7.1.2"));
super.addKPI(arrayUnitId,"PM-00-07-001-10", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.7.1.3"));
super.addKPI(arrayUnitId,"PM-00-07-001-11", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.7.1.4"));
super.addKPI(arrayUnitId,"PM-00-07-001-12", client.get(".1.3.6.1.4.1.116.5.11.4.1.1.7.1.5"));
return super.getKPISet();
} catch (Exception e) {
LogUtil.error("Exception while getArraySPX", e);
} finally {
destroy();
}
return null;
}
public Vector<TblATO_KPIDETAIL> getHPEVAInfo(HashMap<String, String> params) throws Exception {
init(params);
try {
connect();
String arrayUnitId = kbpClass + "-11" + ":" + deviceName;
super.addKPI(arrayUnitId,"PM-01-04-001-01", client.get("1.3.6.1.4.1.21658"));
return super.getKPISet();
} catch (Exception e) {
LogUtil.error("Exception while getArraySPX", e);
} finally {
destroy();
}
return null;
}
private void init(HashMap<String, String> params) throws Exception {
kbpClass = params.get("KBP_CLASS");
ipAddr = params.get("IP");
deviceName = Formater.neatenunitid(params.get("HOST_NAME"));
try {
snmpCommunity = DES3.decrypt(params.get("COMMUNITY"));
}catch (Exception e) {
throw new Exception("Wrong SNMP COMMUNITY");
}
try {
snmpPort = Integer.parseInt(params.get("PORT"));
} catch (Exception e) {
throw new Exception("Wrong SNMP PORT:" + params.get("PORT"));
}
String versionStr = params.get("VERSION");
if (versionStr != null && versionStr.toLowerCase().trim().equals("v1")) {
snmpVersion = SnmpConstants.version1;
} else if (versionStr != null
&& versionStr.toLowerCase().trim().equals("v2c")) {
snmpVersion = SnmpConstants.version2c;
} else if (versionStr != null
&& versionStr.toLowerCase().trim().equals("v3")) {
throw new Exception("nonsupport snmp version: v3");
} else {
// 默认按照v2c版本采集
snmpVersion = SnmpConstants.version2c;
}
}
private void connect() throws Exception {
client = new SNMPClient(ipAddr, snmpPort, snmpCommunity, snmpVersion);
isSupportSnmp = client.isOpenSNMP();
isConn = client.connect();
if (!isConn || !isSupportSnmp) {
throw new Exception(
"Device not support SNMP or Collect params is wrong or Network is error: "
+ ipAddr + ":" + snmpPort);
}
sysObjectID = client.get("1.3.6.1.2.1.1.2.0");
logger.info("-- sysObjectID: " + sysObjectID);
}
public void destroy() {
if(client != null){
client.close();
}
}
/**
* 设备软件版本
*/
private String getSoftVersion() {
String oid = "1.3.6.1.2.1.1.1.0";
String version = null;
try {
version = client.get(oid).replace("\n", " ").replace("\r", "");
} catch (Exception e) {
LogUtil.error("Exception while getSoftVersion", e);
}
return version;
}
public static void main(String[] args) {
System.out.println("***************Load Balance Collect Test Begin*********************");
//拆分系统温度和cpu温度 sys:73,cpu1:46,cpu2:0
String[] tempList = StringUtils.split("sys:73,cpu1:46,cpu2:0",",");
for(String temp : tempList){
String[] tempList1 = StringUtils.split(temp,":");
if(tempList1.length>0){
if(tempList1[0].contains("sys")){
System.out.println(tempList1[0]+"sys-----"+tempList1[1]);
}else if(tempList1[0].contains("cpu")){
System.out.println(tempList1[0]+"cpu-----"+tempList1[1]);
}
}
}
}
}