IfTable.java
9.73 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
package com.sitech.ismp.coll.net.table;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import org.snmp4j.smi.VariableBinding;
import com.sitech.ismp.snmp.CommSnmpGet;
public class IfTable {
public static final String ROOT_OID = "1.3.6.1.2.1.2.2.1.1";
public static final int ATTRIBCOUNT = 22;
private String IP;
Vector rowVector = new Vector();
private void addRow(IfTableRow row) {
rowVector.add(row);
}
public void FillTable(String ip, int port, String community, long sleep) {
this.IP = ip;
CommSnmpGet snmpGet = new CommSnmpGet(ip, port, community);
snmpGet.init();
snmpGet.setRootOid(ROOT_OID);
try {
snmpGet.workTable(ROOT_OID);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[IP: " + IP + " 采集IfTable失败]");
}
HashMap hm = snmpGet.getWalkHashMap();
Set set = hm.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value = (String) hm.get(key);
// 填充IfIndex
IfTableRow row = new IfTableRow();
row.setRowIndex(key);
row.setIfindex(value);
this.addRow(row);
}
// try {
for (int i = 0; i < rowVector.size(); i++) {
IfTableRow row = (IfTableRow) rowVector.elementAt(i);
String rowIndex = row.getRowIndex();
// System.out.println("rowIndex: " + rowIndex);
Vector oidsVector = getOtherOids(rowIndex, ATTRIBCOUNT);
Vector resultVector = null;
try {
Thread.sleep(sleep);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
resultVector = snmpGet.getPDU(oidsVector);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (resultVector != null) {
for (int j = 0; j < resultVector.size(); j++) {
VariableBinding vb = (VariableBinding) resultVector.get(j);
String oid = vb.getOid().toString();
String var = vb.getVariable().toString();
// System.out.println(oid + " = " + var);
if (oid.startsWith("1.3.6.1.2.1.2.2.1.2.")) {
row.setIfdesc(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.3.")) {
row.setIftype(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.4.")) {
row.setIfmtu(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.5.")) {
row.setIfspeed(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.6.")) {
row.setIfphyaddr(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.7.")) {
row.setIfadminstatus(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.8.")) {
row.setIfoperstatus(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.9.")) {
row.setIflastchange(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.10.")) {
row.setIfinoctets(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.11.")) {
row.setIfinucastpkts(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.12.")) {
row.setIfinnucastpkts(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.13.")) {
row.setIfindiscards(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.14.")) {
row.setIfinerrors(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.15.")) {
row.setIfinunknownprotos(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.16.")) {
row.setIfoutoctets(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.17.")) {
row.setIfoutucastpkts(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.18.")) {
row.setIfoutnucastpkts(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.19.")) {
row.setIfoutdiscards(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.20.")) {
row.setIfouterrors(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.21.")) {
row.setIfoutqlen(var);
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.22.")) {
row.setIfspecific(var);
}
}
}
}
// } catch (Exception e) {
// // TODO Auto-generated catch block
// System.out.println("[IP: " + IP + " 采集IfTable失败]");
// }
snmpGet.release();
System.out.println("[IP: " + IP + " IfTable采集完毕]");
}
public void FillTable(String ip, int port, String community) {
this.IP = ip;
CommSnmpGet snmpGet = new CommSnmpGet(ip, port, community);
snmpGet.init();
snmpGet.setRootOid(ROOT_OID);
try {
snmpGet.workTable(ROOT_OID);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("[IP: " + IP + " 采集IfTable失败]");
}
HashMap hm = snmpGet.getWalkHashMap();
Set set = hm.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value = (String) hm.get(key);
// 填充IfIndex
IfTableRow row = new IfTableRow();
row.setRowIndex(key);
row.setIfindex(value);
this.addRow(row);
}
// try {
for (int i = 0; i < rowVector.size(); i++) {
IfTableRow row = (IfTableRow) rowVector.elementAt(i);
String rowIndex = row.getRowIndex();
// System.out.println("rowIndex: " + rowIndex);
Vector oidsVector = getOtherOids(rowIndex, ATTRIBCOUNT);
Vector resultVector = null;
try {
resultVector = snmpGet.getPDU(oidsVector);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (resultVector != null) {
for (int j = 0; j < resultVector.size(); j++) {
VariableBinding vb = (VariableBinding) resultVector.get(j);
String oid = vb.getOid().toString();
String var = vb.getVariable().toString();
System.out.println(oid + " = " + var);
if (oid.startsWith("1.3.6.1.2.1.2.2.1.2.")) {
row.setIfdesc(var);// 端口描述
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.3.")) {
row.setIftype(var);// 端口类型
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.4.")) {
row.setIfmtu(var);// 端口可接收或发送的最大协议数据单元的尺寸
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.5.")) {
row.setIfspeed(var); // 接口当前数据速率的估计值
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.6.")) {
row.setIfphyaddr(var);// 端口物理地址,MAC地址
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.7.")) {
row.setIfadminstatus(var); //期望的接口状态 (up(1), down(2), testing(3))
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.8.")) {
row.setIfoperstatus(var); // 当前的操作接口状态 (up(1), down(2), testing(3))
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.9.")) {
row.setIflastchange(var); // 接口进入当前操作状态的时间
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.10.")) {
row.setIfinoctets(var); // 接口收到的8元组的总数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.11.")) {
row.setIfinucastpkts(var); // 递交到高层协议的子网单播的分组数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.12.")) {
row.setIfinnucastpkts(var); // 递交到高层协议的非单播的分组数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.13.")) {
row.setIfindiscards(var); // 被丢弃的进站分组数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.14.")) {
row.setIfinerrors(var); // 有错的进站分组数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.15.")) {
row.setIfinunknownprotos(var); // 由于协议未知而被丢弃的分组数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.16.")) {
row.setIfoutoctets(var); // 接口发送的8元组的总数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.17.")) {
row.setIfoutucastpkts(var); // 发送到子网单播地址的分组总数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.18.")) {
row.setIfoutnucastpkts(var); // 发送到非子网单播地址的分组总数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.19.")) {
row.setIfoutdiscards(var); // 被丢弃的出站分组数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.20.")) {
row.setIfouterrors(var); // 不能被发送的有错的分组数
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.21.")) {
row.setIfoutqlen(var); // 输出分组队列长度
} else if (oid.startsWith("1.3.6.1.2.1.2.2.1.22.")) {
row.setIfspecific(var); // 参考MIB对实现接口的媒体的定义
}
}
}
}
// } catch (Exception e) {
// // TODO Auto-generated catch block
// System.out.println("[IP: " + IP + " 采集IfTable失败]");
// }
snmpGet.release();
System.out.println("[IP: " + IP + " IfTable采集完毕]");
}
private Vector getOtherOids(String rowIndex, int attribCount) {
Vector vector = new Vector();
for (int i = 2; i <= attribCount; i++) {
String oid = releaseRootOidLast(ROOT_OID, rowIndex, i);
//System.out.println("rowIndex change oid: " + oid);
vector.add(oid);
}
return vector;
}
private String releaseRootOidLast(String rootOid, String rowIndex, int int_i) {
String result = "";
String rowIndexLast = rowIndex.substring(rootOid.length(), rowIndex
.length());
String rootOidStart = rootOid.substring(0, rootOid.length() - 1);
result = rootOidStart + int_i + rowIndexLast;
return result;
}
public Vector getRrows() {
return rowVector;
}
public static void main(String args[]) {
String ip = args[0];
// String ip = "130.30.6.250";
int port = 161;
String community = args[1];
IfTable ifTable = new IfTable();
ifTable.FillTable(ip, port, community);
Vector vector = ifTable.getRrows();
for (int i = 0; i < vector.size(); i++) {
IfTableRow row = (IfTableRow) vector.get(i);
System.out.println(row.getIfdesc() + " " + row.getIfindex() + " "
+ row.getIfindiscards());
}
// Vector vector = ifTable.getOtherOids("1.3.6.1.2.1.2.2.1.1.2",
// IfTable.ATTRIBCOUNT);
// for (int i = 0; i < vector.size(); i++) {
// System.out.println((String)vector.get(i));
// }
}
}