Module: NETSNMP::MIB

Defined in:
lib/netsnmp/mib.rb,
lib/netsnmp/mib/parser.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

OIDREGEX =
/^[\d\.]*$/
MIBDIRS =
ENV.fetch("MIBDIRS", File.join("/usr", "share", "snmp", "mibs")).split(":")
PARSER =
Parser.new
TYPES =
["OBJECT IDENTIFIER", "OBJECT-TYPE", "MODULE-IDENTITY"].freeze
STATIC_MIB_TO_OID =
{
  "iso" => "1"
}.freeze

Class Method Summary collapse

Class Method Details

.do_load(mod) ⇒ Object

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/netsnmp/mib.rb', line 96

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) ⇒ Object



51
52
53
54
55
# File 'lib/netsnmp/mib.rb', line 51

def identifier(oid)
  @object_identifiers.select do |_, ids_oid|
    oid.start_with?(ids_oid)
  end.sort_by(&:size).first
end

.load(mod) ⇒ Object

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")


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/netsnmp/mib.rb', line 66

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_defaultsObject



156
157
158
159
160
# File 'lib/netsnmp/mib.rb', line 156

def load_defaults
  # loading the defaults MIBS
  load("SNMPv2-MIB")
  load("IF-MIB")
end

.load_imports(imports) ⇒ Object

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



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/netsnmp/mib.rb', line 142

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

.oid(identifier) ⇒ Object

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



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
# 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]
      return unless load(mod)
    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:) ⇒ Object



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
# File 'lib/netsnmp/mib.rb', line 111

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