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
-
.do_load(mod) ⇒ Object
Loads the MIB all the time, where
mod
is the absolute path to the MIB. - .identifier(oid) ⇒ Object
-
.load(mod) ⇒ Object
Loads a MIB.
- .load_defaults ⇒ Object
-
.load_imports(imports) ⇒ Object
Reformats the import lists into an hash indexed by module name, to a list of imported names.
- .module_loaded?(mod) ⇒ Boolean
-
.oid(identifier) ⇒ Object
Translates na identifier, such as “sysDescr”, into an OID.
- .store_oid_in_identifiers(nme, value, imports:, declarations:) ⇒ Object
Class Method Details
.do_load(mod) ⇒ Object
Loads the MIB all the time, where mod
is the absolute path to the MIB.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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) ⇒ Object
53 54 55 56 57 |
# 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) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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 ⇒ Object
166 167 168 169 170 |
# File 'lib/netsnmp/mib.rb', line 166 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
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# 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) ⇒ Boolean
89 90 91 92 93 94 95 |
# 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) ⇒ 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 50 51 |
# 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:) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# 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 |