module NETSNMP::MIB

Constants

MIBDIRS
OIDREGEX
PARSER
STATIC_MIB_TO_OID
TYPES

Public Instance Methods

do_load(mod) click to toggle source

Loads the MIB all the time, where mod is the absolute path to the MIB.

# File lib/netsnmp/mib.rb, line 106
def do_load(mod)
  data = @parser_mutex.synchronize { PARSER.parse(File.read(mod)) }

  imports = load_imports(data[:imports])

  declarations = Hash[
    data[:declarations].reject { |dec| !dec.key?(:name) || !TYPES.include?(dec[:type]) }
                       .map { |dec| [String(dec[:name]), String(dec[:value]).split(/ +/)] }
  ]

  declarations.each do |nme, value|
    store_oid_in_identifiers(nme, value, imports: imports, declarations: declarations)
  end
end
identifier(oid) click to toggle source
# File lib/netsnmp/mib.rb, line 53
def identifier(oid)
  @object_identifiers.select do |_, ids_oid|
    oid.start_with?(ids_oid)
  end.sort_by(&:size).first
end
load(mod) click to toggle source

Loads a MIB. Can be called multiple times, as it'll load it once.

Accepts the MIB name in several ways:

MIB.load("SNMPv2-MIB")
MIB.load("SNMPv2-MIB.txt")
MIB.load("/path/to/SNMPv2-MIB.txt")
# File lib/netsnmp/mib.rb, line 68
def load(mod)
  unless File.file?(mod)
    moddir = nil
    MIBDIRS.each do |mibdir|
      if File.exist?(File.join(mibdir, mod))
        moddir = File.join(mibdir, mod)
        break
      elsif File.extname(mod).empty? && File.exist?(File.join(mibdir, "#{mod}.txt"))
        moddir = File.join(mibdir, "#{mod}.txt")
        break
      end
    end
    return false unless moddir
    mod = moddir
  end
  return true if @modules_loaded.include?(mod)
  do_load(mod)
  @modules_loaded << mod
  true
end
load_defaults() click to toggle source
# File lib/netsnmp/mib.rb, line 166
def load_defaults
  # loading the defaults MIBS
  load("SNMPv2-MIB")
  load("IF-MIB")
end
load_imports(imports) click to toggle source

Reformats the import lists into an hash indexed by module name, to a list of imported names

# File lib/netsnmp/mib.rb, line 152
def load_imports(imports)
  return unless imports

  imports = [imports] unless imports.respond_to?(:to_ary)
  imports.each_with_object({}) do |import, imp|
    imp[String(import[:name])] = case import[:ids]
                                 when Hash
                                   [String(import[:ids][:name])]
                                 else
                                   import[:ids].map { |id| String(id[:name]) }
                                 end
  end
end
module_loaded?(mod) click to toggle source
# File lib/netsnmp/mib.rb, line 89
def module_loaded?(mod)
  if File.file?(mod)
    @modules_loaded.include?(mod)
  else
    @modules_loaded.map { |path| File.basename(path, ".*") }.include?(mod)
  end
end
oid(identifier) click to toggle source

Translates na identifier, such as “sysDescr”, into an OID

# File lib/netsnmp/mib.rb, line 20
def oid(identifier)
  prefix, *suffix = case identifier
                    when Array
                      identifier
                    else
                      identifier.split(".", 2)
                    end

  # early exit if it's an OID already
  unless prefix.integer?
    load_defaults
    # load module if need be
    idx = prefix.index("::")
    if idx
      mod = prefix[0..(idx - 1)]
      type = prefix[(idx + 2)..-1]
      unless module_loaded?(mod)
        return unless load(mod)
      end
    else
      type = prefix
    end

    return if type.nil? || type.empty?

    prefix = @object_identifiers[type] ||
             raise(Error, "can't convert #{type} to OID")

  end

  [prefix, *suffix].join(".")
end
store_oid_in_identifiers(nme, value, imports:, declarations:) click to toggle source
# File lib/netsnmp/mib.rb, line 121
def store_oid_in_identifiers(nme, value, imports:, declarations:)
  oid = value.flat_map do |cp|
    if cp.integer?
      cp
    elsif @object_identifiers.key?(cp)
      @object_identifiers[cp]
    elsif declarations.key?(cp)
      store_oid_in_identifiers(cp, declarations[cp], imports: imports, declarations: declarations)
      @object_identifiers[cp]
    else
      STATIC_MIB_TO_OID[cp] || begin
        imported_mod, = imports.find do |_, identifiers|
          identifiers.include?(cp)
        end

        raise Error, "didn't find a module to import \"#{cp}\" from" unless imported_mod

        load(imported_mod)

        @object_identifiers[cp]
      end
    end
  end.join(".")

  @object_identifiers[nme] = oid
end