Module: NETSNMP::MIB

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

Defined Under Namespace

Classes: Parser

Constant Summary collapse

OIDREGEX =
/^[\d.]*$/.freeze
MIBDIRS =
ENV.fetch("MIBDIRS", File.join("/usr", "share", "snmp", "mibs"))
.split(":")
.flat_map { |dir| [dir, *Dir.glob(File.join(dir, "**", "*")).select(&File.method(:directory?))] }.uniq
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.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/netsnmp/mib.rb', line 110

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



55
56
57
58
59
# File 'lib/netsnmp/mib.rb', line 55

def identifier(oid)
  @object_identifiers.select do |_, ids_oid|
    oid.start_with?(ids_oid)
  end.min_by(&:size)
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")


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/netsnmp/mib.rb', line 70

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



172
173
174
175
176
# File 'lib/netsnmp/mib.rb', line 172

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



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/netsnmp/mib.rb', line 158

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

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/netsnmp/mib.rb', line 93

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

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



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
50
51
52
53
# File 'lib/netsnmp/mib.rb', line 22

def oid(identifier)
  prefix, *suffix = case identifier
                    when Array
                      identifier.map(&:to_s)
                    else
                      identifier.split(".", 2).map(&:to_s)
                    end

  return unless prefix

  # 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 if mod && !module_loaded?(mod) && !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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/netsnmp/mib.rb', line 125

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, = if imports
                          imports.find do |_, identifiers|
                            identifiers.include?(cp)
                          end
                        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