最近工作上有功能需求解析MIB文件,在網上找了一圈之後發現都是不全的,很多論壇的提問及回答都是很多年前的,最後經過摸索找到了完整的解析方法,因此做個記錄,也希望給其他需要的人一個幫助。
以下是我的java代碼及一些小細節:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.xxx.bean.MmMib;
import net.percederberg.mibble.Mib;
import net.percederberg.mibble.MibLoader;
import net.percederberg.mibble.MibLoaderException;
import net.percederberg.mibble.MibValue;
import net.percederberg.mibble.MibValueSymbol;
import net.percederberg.mibble.snmp.SnmpObjectType;
public class MibFileParse {
@SuppressWarnings( { "rawtypes" })
public List<MmMib> getMib(String filePath) throws IOException, MibLoaderException {
File file = new File(filePath);
MibLoader ml = new MibLoader();
Mib mib = ml.load(file);
String mibName = mib.getName();
System.err.println("mibName===" + mibName);
System.err.println("-------------------------------------");
String syntax = "";
String access = "";
String status = "";
List<MmMib> list = new ArrayList<MmMib>();
Collection c = mib.getAllSymbols();
Iterator it = c.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof MibValueSymbol) {
MmMib mo = new MmMib();
MibValueSymbol mvs = (MibValueSymbol) obj;
SnmpObjectType sot = null;
if (mvs.getType() instanceof SnmpObjectType) {
sot = (SnmpObjectType) mvs.getType();
}
if (sot != null ) {
syntax = sot.getSyntax().getName();
access = sot.getAccess().toString();
status = sot.getStatus().toString();
}
//是否爲表的列
boolean isTableColumn = mvs.isTableColumn();
String name = mvs.getName();
MibValue value = mvs.getValue();
MibValueSymbol parent = mvs.getParent();
String parentValue = "";
System.err.println("name==" + name);
System.err.println("value==" + value);
System.err.println("isTableColumn==" + isTableColumn);
if (parent != null) {
parentValue = parent.getValue().toString();
if (parent.getParent()==null){
System.err.println("supperParentName======" + mibName);
System.err.println("supperParentValue=====" + parentValue);
}
System.err.println("parentName=" + parent.getName());
System.err.println("parentValue=" + parent.getValue());
} else {
}
System.err.println("syntax=" + syntax);
System.err.println("access=" + access);
System.err.println("status=" + status);
System.err.println("-------------------------------------");
mo.setName(name);
mo.setValue(value.toString());
mo.setParent(parentValue);
mo.setSyntax(syntax);
mo.setAccess(access);
mo.setStatus(status);
list.add(mo);
}
}
/*
MibValueSymbol mvs = mib.getSymbolByOid("1.3.6.1.2.1.10");
System.err.println("mvs.getName()=" + mvs.getName());
System.err.println("mvs.getValue()=" + mvs.getValue());
MibValueSymbol parent = mvs.getParent();
System.err.println("parent=" + parent); */
return list;
}
public static void main(String[] args) throws IOException, MibLoaderException {
MibFileParse t = new MibFileParse();
char a = File.separatorChar;
String abPath = System.getProperty("user.dir");
String filePath = abPath + a + "src" + a + "main" + a + "webapp" + a + "mib" + a + "ISM-HUAWEI-MIB.mib";
t.getMib(filePath);
}
}
這裏我使用了maven庫引用了兩個MIB包分別是Mibble及 Mibble MIBs 兩個包,注意一定要兩個都引入,如果只導入了第一個包,會導致MibLoader文件的時候異常失效,引入的方法直接在maven的官方庫下載就可以。
https://mvnrepository.com/artifact/net.percederberg.mibble
引入的時候如果下載不下來jar包,可以嘗試修改maven庫裏的setting.xml文件,將其中的<url></url>標籤裏的內容改爲如下:
還有需要注意一點是圖中引入的xxx.bean.MmMib是一個實體類,用來存儲採集到的信息,方便存入集合。
引入成功後,運行測試方法,可以看見採集成功,後面的處理大家都明白了吧。
轉載請標明出處