StorageEntryWalker.java
4.98 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
package com.sitech.ismp.coll.centercoll.windows;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import com.sitech.ismp.snmp.CommSnmpGet;
public class StorageEntryWalker {
private Logger logger = Logger.getLogger("COLL");
public static final String ROOT_OID = "1.3.6.1.2.1.25.2.3.1.1";
public static final int ATTRIBCOUNT = 7;
private String IP;
Vector storageVector = new Vector();
private void addEntry(StorageEntry storage) {
storageVector.add(storage);
}
public Vector getEntry(){
return storageVector;
}
public void walker(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 + " 采集StoragerEntry失败]");
}
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);
// 填充Index
StorageEntry storage = new StorageEntry();
storage.setRowIndex(key);
storage.setStorageIndex(value);
this.addEntry(storage);
}
// try {
for (int i = 0; i < storageVector.size(); i++) {
StorageEntry storage = (StorageEntry) storageVector.elementAt(i);
String rowIndex = storage.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();
try{
String gb_str = new String(var.getBytes("GB2312"),"ISO8859_1");
String gb_str_iso = new String(var.getBytes("ISO8859_1"),"GB2312");
String gbk_str_utf = new String(var.getBytes("UTF-8"),"GBK");
String ut_str = new String(var.getBytes("GBK"),"UTF-8");
String gbk_str = new String(var.getBytes(),"GBK");
String gbk1_str = new String(var.getBytes(),"GB2312");
/*logger.info("gb_str="+gb_str);
logger.info("gb_strut_str="+ut_str);
logger.info("gbk_str="+gbk_str);
logger.info("gbk_str=gbk1_str"+gbk1_str);
logger.info("gbk_str=gbk1_strgb_str_iso"+gb_str_iso);
logger.info("gbk_str=gutf-8"+gbk_str_utf);
OctetString oct = (OctetString)(vb.getVariable());
logger.info("gbk_str=gutf-8----"+new String(oct.getValue(),"GBK"));*/
}catch(Exception e){
logger.info("gb_str="+e);
}
// System.out.println(oid + " = " + var);
if (oid.startsWith("1.3.6.1.2.1.25.2.3.1.2.")) {
storage.setStorageType(var);
} else if (oid.startsWith("1.3.6.1.2.1.25.2.3.1.3.")) {
String des="";
try{
OctetString oct = (OctetString)(vb.getVariable());
// logger.info("gbk_str=gutf-8----"+vb.getVariable().toString());
// logger.info("gbk_str=gutf-8----"+new String(oct.getValue(),"GBK"));
// logger.info("gbk_str=gutf-8----"+new String(oct.getValue(),"UTF-8"));
// logger.info("gbk_str=gutf-8----"+new String(oct.getValue(),"ISO8859_1"));
des = new String(oct.getValue(),"GB2312");
// logger.info("gbk_str=gutf-8-9b2312----"+des);
if(des!=null&&!des.equals("")){
var = des;
}
}catch(Exception e){
logger.info("gb_str="+e);
}
storage.setStorageDescr(var);
} else if (oid.startsWith("1.3.6.1.2.1.25.2.3.1.4.")) {
storage.setStorageAllocationUnits(var);
} else if (oid.startsWith("1.3.6.1.2.1.25.2.3.1.5.")) {
storage.setStorageSize(var);
} else if (oid.startsWith("1.3.6.1.2.1.25.2.3.1.6.")) {
storage.setStorageUsed(var);
} else if (oid.startsWith("1.3.6.1.2.1.25.2.3.1.7.")) {
storage.setStorageAllocationFailures(var);
}
}
}
}
snmpGet.release();
System.out.println("[IP: " + IP + " storageEntry采集完毕]");
}
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;
}
}