MiB is the basis of the SNMP collector. MiB are the carrier of network OID descriptions. They mainly include the values, descriptions, and parameters of device OID into MiB. It is the main basic data used for active polling and passive collection of trap information translation. However, MIB is a special protocol that stores data through a special tree structure.
Establish a set of good MIB control code, the foundation of the entire network management system. If you use a flexible and scalable MIB library, it is the vitality of the sustainable development of a network management system.
Mibble-parser-2.9.2.jar is an open-source mibble parser, the code of this program is built for the mibble-parser-2.9.2.jar. And thanks to the fengjiangzhongf@gmail.com for writing part of the code for this module.
Core Parsing
Package COM. shine. framework. mibbrowser; import Java. io. file; import Java. io. ioexception; import Java. util. arraylist; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import net. percederberg. mibble. miB; import net. percederberg. mibble. mibloader; import net. percederberg. mibble. mibloaderexception; import net. percederberg. mibble. mibsymbol; import net. percederberg. mibble. mibvalue; import n ET. percederberg. mibble. mibvaluesymbol; import net. percederberg. mibble. value. objectidentifiervalue; import COM. shine. framework. mibbrowser. model. mibmodel; /*** MIB file analysis class library ** @ author fjz 2011.10.08 **/public class mibbrowserutils {/*** get all MIB * @ Param mibfilepath * @ return */public static list <mibmodel> getalloid (string mibfilepath) {file = checkfile (mibfilepath); If (file = NULL) return NULL; mibmod El map; MIB; List <mibmodel> List = new arraylist <mibmodel> (); try {MIB = loadmib (File); iterator iter = MIB. getallsymbols (). iterator (); mibsymbol symbol; mibvalue value; while (ITER. hasnext () {symbol = (mibsymbol) ITER. next (); value = extractoid (Symbol); If (value! = NULL) {map = new mibmodel (); map. setname (symbol. getname (); map. setoid (value. tostring (); map. setdetail (symbol. tostring (); list. add (MAP) ;}} catch (mibloaderexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} return list ;} /*** check whether the oId belongs to MiB ** @ Param mibfilepath * @ Param OID * @ return */public static Boolean checkoid (string mibfilepath, string OID) {list <mibmodel> List = getalloid (mibfilepath); Return checkoid (list, OID );} /*** check whether the oId belongs to MiB * @ Param list * @ Param OID * @ return */public static Boolean checkoid (list <mibmodel> list, string OID) {If (list = NULL) return false; For (mibmodel: List) {If (mibmodel. getoid (). equals (OID) return true;} return false;}/*** obtain OID information ** @ Param mibfilepath * @ Param OID * @ return */public static mibmodel getoidinfo (string Mibfilepath, string OID) {list <mibmodel> List = getalloid (mibfilepath); Return getoidinfo (list, OID );} /*** obtain OID information ** @ Param mibfilepath * @ Param OID * @ return */public static mibmodel getoidinfo (list <mibmodel> list, string OID) {// system. out. println (list. size (); If (list = NULL) return NULL; For (mibmodel: List) {If (mibmodel. getoid (). equals (OID) return mibmodel;} return NULL;}/*** extracts MIB Based on the file * @ Param file address * @ return * @ throws mibloaderexception * @ throws ioexception */public static MIB loadmib (File file) throws mibloaderexception, ioexception {mibloader loader = new mibloader (); loader. adddir (file. getparentfile (); Return loader. load (File);}/*** extract the oId * @ Param symbol * @ return */public static objectidentifiervalue extractoid (mibsymbol symbol) {mibvalue value; If (symbol instance Of mibvaluesymbol) {value = (mibvaluesymbol) symbol ). getvalue (); If (value instanceof objectidentifiervalue) {return (objectidentifiervalue) value ;}} return NULL;}/*** checks whether the file is a file, * @ Param mibfilepath * @ return */public static file checkfile (string mibfilepath) {file = new file (mibfilepath); If (! File. exists () return NULL; If (! File. isfile () return NULL; return file ;}}
Get all MIB Information
Package COM. shine. framework. mibbrowser; import Java. util. list; import COM. shine. framework. mibbrowser. model. mibmodel; public class allmiblogsexample {/*** get all OID information of the specified document ** @ Param ARGs */public static void main (string [] ARGs) {list <mibmodel> List = mibbrowserutils. getalloid ("E:/workspace/nms4.6/webcontent/WEB-INF/config/MIB/CISCO-PRODUCTS-MIB.mib"); For (mibmodel model: List) {system. out. println ("name:" + model. getname (); system. err. println ("detail:" + model. getdetail ());}}}
Obtains information about a specified oid.
Package COM. shine. framework. mibbrowser; import COM. shine. framework. mibbrowser. model. mibmodel; public class gettargetoidinfoexample {/*** get the information of the specified OID ** @ Param ARGs */public static void main (string [] ARGs) {mibmodel model = mibbrowserutils. getoidinfo ("E:/workspace/nms4.6/webcontent/WEB-INF/config/MIB/CISCO-PRODUCTS-MIB.mib", "1.3.6.1.4.1.9.1.67"); system. out. println (model. getname ());}}
In specific use, we regard MIB as a specific data storage instead of the original tree structure. Is the list structure.