HOME

com.ireasoning.protocol.snmp
Class MibUtil

java.lang.Object
  extended bycom.ireasoning.protocol.snmp.MibUtil

public final class MibUtil
extends Object

This class provides methods for parsing MIBs, translating numeric format oid to text format, translating numeric value to text value, lookup oid based on mib node name.

Examples:


Method Summary
static Set getModulesInfo()
           
static boolean isMibFileLoaded()
          Tests if there's at least one mib file already loaded
static boolean isMibFileLoaded(String fileName)
          Checks if the passed mib file has been loaded.
static void loadMib(Reader r, boolean isStrict)
          Loads MIB file from Reader object
static void loadMib(Reader r, boolean isStrict, String mibVersion)
          Loads MIB file from Reader object
static void loadMib(String fileName)
          Loads mib file, so translateOID, translateValue, and other methods can take effect.
static void loadMib(String fileName, boolean isStrict)
          Loads mib file, so translateOID, translateValue, and other methods can take effect.
static void loadMib(String fileName, boolean isStrict, String mibVersion)
          Loads mib file, so translateOID, translateValue, and other methods can take effect.
static void loadMib(String fileName, String mibVersion)
          Loads mib file, so translateOID, translateValue, and other methods can take effect.
static void loadMib2()
          Loads MIB-II (RFC1213) from the built-in resource file.
static void loadMibs(Reader[] readers, boolean isStrict)
          Loads multiple MIB files.
static void loadMibs(String[] fileNames)
          Loads multiple MIB files.
static void loadMibs(String[] fileNames, boolean isStrict)
          Loads multiple MIB files.
static void loadMibs(String[] fileNames, boolean isStrict, String mibVersion)
          Loads multiple MIB files.
static void loadMibs(String[] fileNames, String mibVersion)
          Loads multiple MIB files.
static SnmpOID lookupOID(String mibNodeName)
          Looks up OID for passed mibNodeName, case insensitive.
static SnmpOID lookupOID(String mibNodeName, boolean appendDotZero)
          Looks up OID for passed mibNodeName, case insensitive.
static SnmpOID lookupOID(String mibNodeName, String mibVersion)
          Looks up OID for passed mibNodeName, case insensitive.
static SnmpOID lookupOID(String mibNodeName, String mibVersion, boolean appendDotZero)
          Looks up OID for passed mibNodeName, case insensitive.
static MibTreeNode parseMib(Reader r, boolean isStrict)
          Returns the MIB tree representation in a MibTreeNode object, which is the root of the MIB tree.
static MibTreeNode parseMib(String fileName)
          Returns the MIB tree representation in a MibTreeNode object, which is the root of the MIB tree.
static MibTreeNode parseMib(String fileName, boolean isStrict)
          Returns the MIB tree representation in a MibTreeNode object, which is the root of the MIB tree.
static MibTreeNode parseMibs(Reader[] readers, boolean isStrict)
          Parses MIBs loaded from Reader array.
static MibTreeNode parseMibs(String[] fileNames)
          Parses passed MIB files strictly.
static MibTreeNode parseMibs(String[] fileNames, boolean isStrict)
          Parses passed MIB files.
static MibTreeNode[] parseMibsWithoutMerge(String[] fileNames)
          Parses passed MIB files strictly.
static MibTreeNode[] parseMibsWithoutMerge(String[] fileNames, boolean isStrict)
          Parses passed MIB files.
static void setResolveSyntax(boolean b)
          If true, MIB parser will resolve each MIB node's syntax to basic node syntax, such as OID, Octect String, Integer, etc.
static NameValue translate(SnmpOID oid, String value, boolean fullName)
          Translates oid and its value
static NameValue translate(String oid, String value, boolean fullName)
          Translates oid and its value
static String translateOID(SnmpOID oid, boolean fullName)
          Translates oid from numeric to string format.
static String translateOID(String oid, boolean fullName)
          Translates oid from numeric to string format.
static String translateSnmpV1Trap(SnmpOID enterprise, int generic, int specific)
          Returns the SNMPv1 trap name defined in the MIB file.
static String translateSnmpV1Trap(SnmpV1Trap trap)
          Returns the SNMPv1 trap name defined in the MIB file.
static String translateValue(MibTreeNode node, String value)
           
static String translateValue(SnmpOID oid, String value)
          Translates value part of SnmpVarBind.
static String translateValue(String oid, String value)
          Translates value part of SnmpVarBind.
static void unloadAllMibs()
          Unloads all the loaded MIBs
static void unloadMib(String fileName)
          Unloads MIB file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setResolveSyntax

public static void setResolveSyntax(boolean b)
If true, MIB parser will resolve each MIB node's syntax to basic node syntax, such as OID, Octect String, Integer, etc. Then MibTreeNode.getRealSyntaxType() can return the basic syntax type

By default, MIB parser does not resolve syntax types.

This method needs to be invoked before loading a MIB for it to take effect.

See Also:
MibTreeNode.getRealSyntaxType()

loadMibs

public static void loadMibs(String[] fileNames)
                     throws IOException,
                            MibParseException
Loads multiple MIB files. The same as calling loadMibs(fileNames, true)

Parameters:
fileNames - MIB file names
Throws:
IOException
MibParseException
See Also:
loadMib(java.lang.String)

loadMibs

public static void loadMibs(String[] fileNames,
                            boolean isStrict)
                     throws IOException,
                            MibParseException
Loads multiple MIB files.

Parameters:
fileNames - MIB file names
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Throws:
IOException
MibParseException
See Also:
loadMib(java.lang.String)

loadMibs

public static void loadMibs(String[] fileNames,
                            String mibVersion)
                     throws IOException,
                            MibParseException
Loads multiple MIB files.

Parameters:
fileNames - MIB file names
mibVersion - If you have multiple versions of a MIB module, this parameter is used to distinguish different versions. It doesn't have to be the same as the actual version number, but it has to be unique among all the MIBs with the same module name. Use null if only no multiple versions of a mib.
Throws:
IOException
MibParseException
See Also:
loadMib(java.lang.String)

loadMibs

public static void loadMibs(String[] fileNames,
                            boolean isStrict,
                            String mibVersion)
                     throws IOException,
                            MibParseException
Loads multiple MIB files.

Parameters:
fileNames - MIB file names
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
mibVersion - If you have multiple versions of a MIB module, this parameter is used to distinguish different versions. It doesn't have to be the same as the actual version number, but it has to be unique among all the MIBs with the same module name. Use null if only no multiple versions of a mib.
Throws:
IOException
MibParseException
See Also:
loadMib(java.lang.String)

loadMib

public static void loadMib(String fileName)
                    throws IOException,
                           MibParseException
Loads mib file, so translateOID, translateValue, and other methods can take effect. This method is the same as loadMib(fileName, true)
Note: It's a static method, so a MIB only needs to be loaded once in a JVM.

Both loadMib and parseMib methods can load MIB. The difference between them is parseMib returns a MibTreeNode object which can be used to navigate thru the MIB tree, but parseMib doesn't build an internal data model to facilitate the translation between oid and name.

Parameters:
fileName - mib file name. If this file is already loaded, nothing happens
Throws:
IOException - raised if mib file not found
MibParseException - raised if mib file is corrupted
See Also:
parseMib(String fileName)

loadMib

public static void loadMib(String fileName,
                           String mibVersion)
                    throws IOException,
                           MibParseException
Loads mib file, so translateOID, translateValue, and other methods can take effect. This method is the same as loadMib(fileName, true)
Note: It's a static method, so a MIB only needs to be loaded once in a JVM.

Parameters:
fileName - mib file name. If this file is already loaded, nothing happens
mibVersion - If you have multiple versions of a MIB module, this parameter is used to distinguish different versions. It doesn't have to be the same as the actual version number, but it has to be unique among all the MIBs with the same module name. Use null if only no multiple versions of a mib.
Throws:
IOException - raised if mib file not found
MibParseException - raised if mib file is corrupted

loadMib

public static void loadMib(String fileName,
                           boolean isStrict)
                    throws IOException,
                           MibParseException
Loads mib file, so translateOID, translateValue, and other methods can take effect.
Note: It's a static method, so a MIB only needs to be loaded once in a JVM.

Parameters:
fileName - mib file name. If this file is already loaded, nothing happens
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Throws:
IOException - raised if mib file not found
MibParseException - raised if mib file is corrupted

loadMib

public static void loadMib(String fileName,
                           boolean isStrict,
                           String mibVersion)
                    throws IOException,
                           MibParseException
Loads mib file, so translateOID, translateValue, and other methods can take effect.
Note: It's a static method, so a MIB only needs to be loaded once in a JVM.

Parameters:
fileName - mib file name. If this file is already loaded, nothing happens
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
mibVersion - If you have multiple versions of a MIB module, this parameter is used to distinguish different versions. It doesn't have to be the same as the actual version number, but it has to be unique among all the MIBs with the same module name. Use null if only no multiple versions of a mib.
Throws:
IOException - raised if mib file not found
MibParseException - raised if mib file is corrupted

loadMib

public static void loadMib(Reader r,
                           boolean isStrict)
                    throws IOException,
                           MibParseException
Loads MIB file from Reader object

Throws:
IOException
MibParseException

loadMib

public static void loadMib(Reader r,
                           boolean isStrict,
                           String mibVersion)
                    throws IOException,
                           MibParseException
Loads MIB file from Reader object

Throws:
IOException
MibParseException

loadMibs

public static void loadMibs(Reader[] readers,
                            boolean isStrict)
                     throws IOException,
                            MibParseException
Loads multiple MIB files.

Parameters:
readers - Reader objects for loading MIBs
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Throws:
IOException
MibParseException
See Also:
loadMib(java.lang.String)

loadMib2

public static void loadMib2()
Loads MIB-II (RFC1213) from the built-in resource file.
Note: It's a static method, so a MIB only needs to be loaded once in a JVM.


unloadMib

public static void unloadMib(String fileName)
Unloads MIB file

Parameters:
fileName - mib file to unload. If this file was not loaded, nothing happens

unloadAllMibs

public static void unloadAllMibs()
Unloads all the loaded MIBs


isMibFileLoaded

public static boolean isMibFileLoaded(String fileName)
Checks if the passed mib file has been loaded.

Parameters:
fileName - fully qualified file name of a MIB

isMibFileLoaded

public static boolean isMibFileLoaded()
Tests if there's at least one mib file already loaded

Returns:
true if at least one mib file is loaded

lookupOID

public static SnmpOID lookupOID(String mibNodeName)
Looks up OID for passed mibNodeName, case insensitive. For instance, if passed "sysUpTime" (or "sysuptime" "sysuptime.0" "system.sysUpTime" ".sysUpTime"), the returned OID will be ".1.3.6.1.2.1.1.3.0"; for "ifTable", it returns ".1.3.6.1.2.1.2.2" ; for "ifAdminStatus.2", it returns ".1.3.6.1.2.1.2.2.1.7.2". If passed mibNodeName is a numerical OID, it'll be returned in a SnmpOID object.
Note: For scalar variables such as sysUpTime, the returned OID already ends with ".0"

Parameters:
mibNodeName - mib node name
Returns:
oid corresponding the passed mibNodeName, or null if passed mibNodeName is not found in loaded MIBs match.

lookupOID

public static SnmpOID lookupOID(String mibNodeName,
                                boolean appendDotZero)
Looks up OID for passed mibNodeName, case insensitive. For instance, if passed "sysUpTime" (or "sysuptime" "sysuptime.0" "system.sysUpTime" ".sysUpTime"), the returned OID will be ".1.3.6.1.2.1.1.3.0"; for "ifTable", it returns ".1.3.6.1.2.1.2.2" ; for "ifAdminStatus.2", it returns ".1.3.6.1.2.1.2.2.1.7.2". If passed mibNodeName is a numerical OID, it'll be returned in a SnmpOID object.

Parameters:
appendDotZero - if true, append ".0" to the returned scalar variable.
mibNodeName - mib node name
Returns:
oid corresponding the passed mibNodeName, or null if passed mibNodeName is not found in loaded MIBs match.

lookupOID

public static SnmpOID lookupOID(String mibNodeName,
                                String mibVersion)
Looks up OID for passed mibNodeName, case insensitive. For instance, if passed "sysUpTime" (or "sysuptime" "sysuptime.0" "system.sysUpTime" ".sysUpTime"), the returned OID will be ".1.3.6.1.2.1.1.3.0"; for "ifTable", it returns ".1.3.6.1.2.1.2.2" ; for "ifAdminStatus.2", it returns ".1.3.6.1.2.1.2.2.1.7.2". If passed mibNodeName is a numerical OID, it'll be returned in a SnmpOID object.
Note: For scalar variables such as sysUpTime, the returned OID already ends with ".0"

Parameters:
mibNodeName - mib node name
mibVersion - If you have multiple versions of a MIB module, this parameter is used to distinguish different versions. It doesn't have to be the same as the actual version number, but it has to be unique among all the MIBs with the same module name. Use null if only no multiple versions of a mib.
Returns:
oid corresponding the passed mibNodeName, or null if passed mibNodeName is not found in loaded MIBs match.

lookupOID

public static SnmpOID lookupOID(String mibNodeName,
                                String mibVersion,
                                boolean appendDotZero)
Looks up OID for passed mibNodeName, case insensitive. For instance, if passed "sysUpTime" (or "sysuptime" "sysuptime.0" "system.sysUpTime" ".sysUpTime"), the returned OID will be ".1.3.6.1.2.1.1.3.0"; for "ifTable", it returns ".1.3.6.1.2.1.2.2" ; for "ifAdminStatus.2", it returns ".1.3.6.1.2.1.2.2.1.7.2". If passed mibNodeName is a numerical OID, it'll be returned in a SnmpOID object.

Parameters:
mibNodeName - mib node name
appendDotZero - if true, append ".0" to the returned scalar variable.
mibVersion - If you have multiple versions of a MIB module, this parameter is used to distinguish different versions. It doesn't have to be the same as the actual version number, but it has to be unique among all the MIBs with the same module name. Use null if only no multiple versions of a mib.
Returns:
oid corresponding the passed mibNodeName, or null if passed mibNodeName is not found in loaded MIBs match.

translateOID

public static String translateOID(String oid,
                                  boolean fullName)
Translates oid from numeric to string format. For example, translateOID(".1.3.6.1.2.1.2.2.1.7", true) will return ".iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifAdminStatus"; translateOID(".1.3.6.1.2.1.2.2.1.7.2", true) will return ".iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifAdminStatus.2"; translateOID(".1.3.6.1.2.1.2.2.1.7.2", false) will return "ifAdminStatus.2"
Note: Assume the MIB file containing that oid has been loaded by calling loadMib(String fileName) method.

Parameters:
fullName - true to use fully qualified name. Otherwise only the last of second to last field is returned
oid - OID to be translated.
Returns:
MIB node name, or null if passed oid is not found in loaded MIBs

translateOID

public static String translateOID(SnmpOID oid,
                                  boolean fullName)
Translates oid from numeric to string format. For example, translateOID(".1.3.6.1.2.1.2.2.1.7", true) will return ".iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifAdminStatus"; translateOID(".1.3.6.1.2.1.2.2.1.7.2", true) will return ".iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifAdminStatus.2"; translateOID(".1.3.6.1.2.1.2.2.1.7.2", false) will return "ifAdminStatus.2"
Note: Assume the MIB file containing that oid has been loaded by calling loadMib(String fileName) method.

Parameters:
fullName - true to use fully qualified name. Otherwise only the last of second to last field is returned
oid - OID to be translated
Returns:
MIB node name, or null if passed oid is not found in loaded MIBs

translateSnmpV1Trap

public static String translateSnmpV1Trap(SnmpV1Trap trap)
Returns the SNMPv1 trap name defined in the MIB file.

Returns:
the name of SNMPv1 trap; null if trap couldn't be found in the loaded MIBs.

translateSnmpV1Trap

public static String translateSnmpV1Trap(SnmpOID enterprise,
                                         int generic,
                                         int specific)
Returns the SNMPv1 trap name defined in the MIB file.

Returns:
the name of SNMPv1 trap; null if trap couldn't be found in the loaded MIBs.

translateValue

public static String translateValue(String oid,
                                    String value)
Translates value part of SnmpVarBind. For example, in MIB-II's ifAdminStatus:
     ifAdminStatus OBJECT-TYPE
         SYNTAX  INTEGER {
                     up(1),       -- ready to pass packets
                     down(2),
                     testing(3)   -- in some test mode
                 }
 
MibUtil.translateValue(".1.3.6.1.2.1.2.2.1.7.1" // oid of ifAdminStatus.1
, "1") will return "up"; MibUtil.translateValue(".1.3.6.1.2.1.2.2.1.1.1" // oid of ifIndex.1
, "1") will return "1"; MibUtil.translateValue(".3.3.6.1.2.1.2.2.1.1.1" // unkonwn OID
, "1") will return "1"
Note: Assume the MIB file containing that oid has been loaded by calling loadMib(String fileName) method.

Returns:
the value corresponding the passed node, or the original passed-in "value" if node not found in the loaded MIBs

translateValue

public static String translateValue(MibTreeNode node,
                                    String value)

translateValue

public static String translateValue(SnmpOID oid,
                                    String value)
Translates value part of SnmpVarBind. For example, in MIB-II's ifAdminStatus:
     ifAdminStatus OBJECT-TYPE
         SYNTAX  INTEGER {
                     up(1),       -- ready to pass packets
                     down(2),
                     testing(3)   -- in some test mode
                 }
 
MibUtil.translateValue(".1.3.6.1.2.1.2.2.1.7.1" // oid of ifAdminStatus.1
, "1") will return "up"; MibUtil.translateValue(".1.3.6.1.2.1.2.2.1.1.1" // oid of ifIndex.1
, "1") will return "1"; MibUtil.translateValue(".3.3.6.1.2.1.2.2.1.1.1" // unkonwn OID
, "1") will return "1"
Note: Assume the MIB file containing that oid has been loaded by calling loadMib(String fileName) method.

Returns:
the value corresponding the passed node, or the original passed-in "value" if node not found in the loaded MIBs

translate

public static NameValue translate(String oid,
                                  String value,
                                  boolean fullName)
Translates oid and its value

Returns:
a NameValue object which contains the name corresponding to the passed oid, and value corresponding to the passed value. If passed OID is not found in the loaded mib, it returns null.
See Also:
translateValue(java.lang.String, java.lang.String), translateOID(java.lang.String, boolean)

translate

public static NameValue translate(SnmpOID oid,
                                  String value,
                                  boolean fullName)
Translates oid and its value

Returns:
a NameValue object which contains the name corresponding to the passed oid, and value corresponding to the passed value. If passed OID is not found in the loaded mib, it returns null.
See Also:
translateValue(java.lang.String, java.lang.String), translateOID(java.lang.String, boolean)

parseMib

public static MibTreeNode parseMib(String fileName)
                            throws IOException,
                                   MibParseException
Returns the MIB tree representation in a MibTreeNode object, which is the root of the MIB tree. The parser is capable of parsing both SMIv1 (SNMPv1) and SMIv2 (SNMPv2/v3) MIBs.

Parameters:
fileName - file name of the MIB
Returns:
root node, whose name is the MIB module name. And its description is the MODULE-IDENTITY value for smiv2 MIBs. So root.getDescription() returns the MODULE-IDENTITY value.
Throws:
IOException
MibParseException
See Also:
loadMib(String fileName)

parseMib

public static MibTreeNode parseMib(Reader r,
                                   boolean isStrict)
                            throws IOException,
                                   MibParseException
Returns the MIB tree representation in a MibTreeNode object, which is the root of the MIB tree. The parser is capable of parsing both SMIv1 (SNMPv1) and SMIv2 (SNMPv2/v3) MIBs.

Parameters:
r - Reader object used to load MIB content
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Returns:
root node
Throws:
IOException
MibParseException

parseMibs

public static MibTreeNode parseMibs(Reader[] readers,
                                    boolean isStrict)
                             throws IOException,
                                    MibParseException
Parses MIBs loaded from Reader array. The trees will be merged into a single tree and its root is returned. The parser is capable of parsing both SMIv1 (SNMPv1) and SMIv2 (SNMPv2/v3) MIBs.

Parameters:
readers - Reader array
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Throws:
IOException
MibParseException

parseMib

public static MibTreeNode parseMib(String fileName,
                                   boolean isStrict)
                            throws IOException,
                                   MibParseException
Returns the MIB tree representation in a MibTreeNode object, which is the root of the MIB tree. The parser is capable of parsing both SMIv1 (SNMPv1) and SMIv2 (SNMPv2/v3) MIBs.

Parameters:
fileName - file name of the MIB
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Returns:
root node
Throws:
IOException
MibParseException

parseMibs

public static MibTreeNode parseMibs(String[] fileNames)
                             throws IOException,
                                    MibParseException
Parses passed MIB files strictly. The trees from passed MIBs will be merged into a single tree and its root is returned. The parser is capable of parsing both SMIv1 (SNMPv1) and SMIv2 (SNMPv2/v3) MIBs.

Parameters:
fileNames - file names of the MIBs
Throws:
IOException
MibParseException
See Also:
parseMibs(String[] fileNames, boolean isStrict)

parseMibs

public static MibTreeNode parseMibs(String[] fileNames,
                                    boolean isStrict)
                             throws IOException,
                                    MibParseException
Parses passed MIB files. The trees from passed MIBs will be merged into a single tree and its root is returned. The parser is capable of parsing both SMIv1 (SNMPv1) and SMIv2 (SNMPv2/v3) MIBs.

Parameters:
fileNames - file names of the MIBs
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Throws:
IOException
MibParseException

parseMibsWithoutMerge

public static MibTreeNode[] parseMibsWithoutMerge(String[] fileNames)
                                           throws IOException,
                                                  MibParseException
Parses passed MIB files strictly. and returns tree representation for each of the MIBs. No merge of trees is performed.

Parameters:
fileNames - file names of the MIBs
Returns:
MibTreeNode array corresponding to the passed fileNames
Throws:
IOException
MibParseException

parseMibsWithoutMerge

public static MibTreeNode[] parseMibsWithoutMerge(String[] fileNames,
                                                  boolean isStrict)
                                           throws IOException,
                                                  MibParseException
Parses passed MIB files. and returns tree representation for each of the MIBs. No merge of trees is performed.

Parameters:
fileNames - file names of the MIBs
isStrict - if true, MIB is parsed strictly, any error in MIB will raise exception. Otherwise only an error message is written to log file
Returns:
MibTreeNode array corresponding to the passed fileNames
Throws:
IOException
MibParseException

getModulesInfo

public static Set getModulesInfo()
Returns:
a Set <MibModuleData>, whose elements contain MIB module's data, such as dependencies, revision data, etc.

HOME

Copyright © 2002 iReasoning Inc. All Rights Reserved.