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-TYPE", "OBJECT IDENTIFIER", "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
This is a helper function, do not rely on this functionality in future versions.
-
.load(mod) ⇒ Object
Loads a MIB.
- .load_defaults ⇒ Object
-
.load_imports(data) ⇒ Object
Reformats the import lists into an hash indexed by module name, to a list of imported names.
-
.oid(identifier) ⇒ Object
Translates na identifier, such as “sysDescr”, into an OID.
Class Method Details
.do_load(mod) ⇒ Object
Loads the MIB all the time, where mod
is the absolute path to the MIB.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/netsnmp/mib.rb', line 98 def do_load(mod) data = @parser_mutex.synchronize { PARSER.parse(File.read(mod)) } imports = load_imports(data) data[:declarations].each_with_object(@object_identifiers) do |dec, types| next unless TYPES.include?(dec[:type]) oid = String(dec[:value]).split(/ +/).flat_map do |cp| if cp.integer? cp else STATIC_MIB_TO_OID[cp] || @object_identifiers[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(".") types[String(dec[:name])] = oid end end |
.identifier(oid) ⇒ Object
This is a helper function, do not rely on this functionality in future versions
53 54 55 56 57 |
# File 'lib/netsnmp/mib.rb', line 53 def identifier(oid) @object_identifiers.select do |_, full_oid| full_oid.start_with?(oid) end 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
145 146 147 148 149 |
# File 'lib/netsnmp/mib.rb', line 145 def load_defaults # loading the defaults MIBS load("SNMPv2-MIB") load("IF-MIB") end |
.load_imports(data) ⇒ Object
Reformats the import lists into an hash indexed by module name, to a list of imported names
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/netsnmp/mib.rb', line 132 def load_imports(data) return unless data[:imports] data[: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 |