CollHPWithSNMP.java
6.08 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
package com.sitech.ismp.coll.centercoll;
import java.util.Date;
import java.util.Vector;
import com.adventnet.snmp.beans.SnmpTarget;
import com.adventnet.snmp.snmp2.SnmpOID;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
public class CollHPWithSNMP {
String PRE_UNITID = "10-10-21"; // hp���ı�ʶ
private SnmpTarget target = null;
public CollHPWithSNMP(String ip) {
this.HOST_IP = ip;
target = new SnmpTarget();
HOST_NAME = this.getHostName();
NEAT_HOST_NAME = com.sitech.util.Formater.neatenunitid(HOST_NAME);
}
private String HOST_IP = "";
/*
* private int PORT = 161; private String VERSION = "v1"; private String
* COMMUNITY = "public"; private int RETRIES = 3; private int TIMEOUT = 4;
* private String MIBS = "5";
*/
private boolean DEBUG = false;
private String HOST_NAME = "";
private String NEAT_HOST_NAME = "";
public void init() {
// String authProtocol = new String("NO_AUTH");
// Use an SNMP target bean to perform SNMP operations
target = new SnmpTarget();
//target.setLoadFromCompiledMibs(true);
target.setDebug(DEBUG);
target.setSnmpVersion(SnmpTarget.VERSION1);
target.setTargetHost(this.HOST_IP); // set the agent hostname
// target.setCommunity( COMMUNITY);
// target.setTargetPort( PORT );
// target.setRetries( RETRIES );
// target.setTimeout( TIMEOUT );
if (target.getSnmpVersion() == SnmpTarget.VERSION3) {
target.create_v3_tables();
}
}// end init
public void release() {
target.releaseResources();
}// end release
public String getPhiMemory() {
String oid = ".1.3.6.1.4.1.11.2.3.1.1.8.0";
String phiMemory = this.getKPIInfo(oid);
return phiMemory;
}
public TblATO_KPIDETAIL getKPIPhiMemory() {
String strPhiMemory = this.getPhiMemory();
if (strPhiMemory == null) {
return null;
}
int phiMeomryK = Integer.parseInt(strPhiMemory);
int phiMeomryM = phiMeomryK / 1024;
String config_PRE_UNITID = PRE_UNITID + "-10"; // totalֵ
String UNIT_ID = config_PRE_UNITID + ":" + NEAT_HOST_NAME + "-total";
return this.createKPI(UNIT_ID, "CM-00-01-001-07", String
.valueOf(phiMeomryM));
}
private String getFreeMemory() {
String oid = ".1.3.6.1.4.1.11.2.3.1.1.7.0";
return this.getKPIInfo(oid);
}
public String getHostName() {
String oid = ".1.3.6.1.2.1.1.5.0";
return this.getKPIInfo(oid);
}
public TblATO_KPIDETAIL getKPIMemoryUseRate() {
String totalMemory = this.getPhiMemory();
String freeMemory = this.getFreeMemory();
if (totalMemory == null || freeMemory == null) {
return null;
}
int total = Integer.parseInt(totalMemory);
int free = Integer.parseInt(freeMemory);
int used = total - free;
double usedRate = used * 100.0 / total;
String strUsedRate = com.sitech.util.Formater
.formatDecimalKpivalue(String.valueOf(usedRate));
String memory_PRE_UNITID = PRE_UNITID + "-12";
String UNIT_ID = memory_PRE_UNITID + ":" + NEAT_HOST_NAME + "-memory";
return this.createKPI(UNIT_ID, "PM-00-01-002-01", strUsedRate);
}
public String getKPIInfo(String oid) {
String oids[] = new String[1];
init();
oids[0] = "1.0";
target.setObjectIDList(oids);
target.oidList[0] = new com.adventnet.snmp.snmp2.SnmpOID(oid);
String result[] = target.snmpGetList(); // do a get request
release();
if (result == null) {
return null;
} else {
return result[0];
}
}// end getKPIInfo
public boolean getSNMPState() {
String value = getKPIInfo(".1.3.6.1.2.1.1.5.0");
if (value == null) {
return false;
}
return true;
}// end getSNMPState
public Vector walkKPIInfo(String oid) {
// System.out.println("Begin snmpWalkList");
Vector tmp_result = new Vector();
Vector tmp_oid = new Vector();
target.setObjectID(oid);
target.oidList[0] = new com.adventnet.snmp.snmp2.SnmpOID(oid);// .1.3.6.1.4.1.11.2.3.1.2.2.1
// Each
// entry
// contains
// objects
// for a
// particular
// file
// system.(HP)
int maxtry = 0;
com.adventnet.snmp.snmp2.SnmpOID[] oidList = target.getSnmpOIDList();
if (oidList == null) {
return null;
} else {
SnmpOID rootoid = oidList[0];
SnmpOID curroid = null;
while (maxtry++ < 3000) { // limit the max getnexts to 5000
String result1[] = target.snmpGetNextList();
if (result1 == null) {
target.oidList[0] = curroid;
continue;
// break;
}
if (!SnmpTarget.isInSubTree(rootoid, target.getSnmpOID()))
break; // check first column
for (int i = 0; i < result1.length; i++) { // print the values
// System.out.println(target.getObjectID(i) + ": " +
// result1[i]);
tmp_result.add(result1[i]);
tmp_oid.add(target.getObjectID(i));
}
}
if (maxtry == 1) { // we did not get a valid row
System.err
.println("Request fed, timed out or no available data("
+ oid + "). \n " + target.getErrorString());
}
}
// System.out.println("End snmpWalkList");
return tmp_result;
}// end walkKPIInfo
private TblATO_KPIDETAIL createKPI(String KBP_ID, String KPI_ID,
String KPI_VALUE) {
TblATO_KPIDETAIL tblato_kpidetail = new TblATO_KPIDETAIL();
tblato_kpidetail.UNIT_ID = KBP_ID;
tblato_kpidetail.KPI_ID = KPI_ID;
tblato_kpidetail.KPI_VALUE = KPI_VALUE;
tblato_kpidetail.CLL_TIME = new Date();
return tblato_kpidetail;
}
public static void main(String[] args) {
System.out.println(DES3.encrypt("public"));
// String ip = args[0];
// String oid = args[1];
String ip = "172.16.9.98";
String oid = ".1.3.6.1.4.1.11.2.3.1.2.2.1.4";
CollHPWithSNMP c = new CollHPWithSNMP(ip);
// System.out.println(c.getKPIInfo(oid));
// System.out.println("over");
// CollHPWithSNMP c = new CollHPWithSNMP(ip);
// System.out.println(c.getKPIInfo(oid));
// System.out.println("over");
// c.release();
TblATO_KPIDETAIL t = c.getKPIMemoryUseRate();
System.out.print(t.UNIT_ID + " # ");
System.out.print(t.KPI_ID + " # ");
System.out.print(t.KPI_VALUE + " # ");
System.out.println(t.CLL_TIME + " ");
t = c.getKPIPhiMemory();
System.out.print(t.UNIT_ID + " # ");
System.out.print(t.KPI_ID + " # ");
System.out.print(t.KPI_VALUE + " # ");
System.out.println(t.CLL_TIME + " ");
System.exit(0);
}
}