-
Notifications
You must be signed in to change notification settings - Fork 7
/
mib2yaml.rb
68 lines (61 loc) · 1.93 KB
/
mib2yaml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# MIB2YAML converts MIB files to YAML files for the Ruby SNMP library with support for imports
#
# The code is heavily based on http://snmplib.rubyforge.org/, the only addition is the "find imports" block
require 'rubygems'
require 'fileutils'
require 'yaml'
module MIB2YAML
def MIB2YAML.convert(module_file, mib_dir = '.')
raise "smidump tool must be installed" unless import_supported?
FileUtils.makedirs mib_dir
# find imports
imports = ''
File.read(module_file).to_a.each do |i|
import = i.slice(/FROM [\w-]+/)
if import != nil
imports += "-p #{import.gsub('FROM ','')}.mib "
end
end
mib_hash = `smidump -f python #{imports} #{module_file}`
mib = eval_mib_data(mib_hash)
if mib
module_name = mib["moduleName"]
raise "#{module_file}: invalid file format; no module name" unless module_name
if mib["nodes"]
oid_hash = {}
mib["nodes"].each { |key, value| oid_hash[key] = value["oid"] }
if mib["notifications"]
mib["notifications"].each { |key, value| oid_hash[key] = value["oid"] }
end
File.open(File.join(mib_dir, module_name + '.yaml'), 'w') do |file|
YAML.dump(oid_hash, file)
file.puts
end
puts "File converted"
else
puts "*** No nodes defined in: #{module_file} ***"
end
else
puts "*** Import failed for: #{module_file} ***"
end
end
def MIB2YAML.import_supported?
`smidump --version` =~ /^smidump 0.4/ && $? == 0
end
private
def MIB2YAML.eval_mib_data(mib_hash)
ruby_hash = mib_hash.
gsub(':', '=>'). # fix hash syntax
gsub('(', '[').gsub(')', ']'). # fix tuple syntax
sub('FILENAME =', 'filename ='). # get rid of constants
sub('MIB =', 'mib =')
mib = nil
eval(ruby_hash)
mib
end
end
if ARGV.length < 1
puts "Usage: mib2yaml <mibfile>"
else
MIB2YAML::convert(ARGV[0])
end