snmpdumper 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +18 -0
- data/README +28 -0
- data/bin/snmpdumper +4 -0
- data/lib/dumper/jalasoft_dumper.rb +100 -0
- data/lib/dumper/simple_text_dumper.rb +17 -0
- data/lib/options.rb +206 -0
- data/lib/runner.rb +40 -0
- data/lib/snmpwalk_reader.rb +85 -0
- data/lib/walker.rb +53 -0
- metadata +82 -0
data/LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2006 Sebastian de Castelberg
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
8
|
+
subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
15
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= snmpdumper - create snmpdumps for 3rd party tools
|
2
|
+
|
3
|
+
This tool dumps snmp walks into a set of output formats.
|
4
|
+
|
5
|
+
* Plain Text (same as running snmpwalk)
|
6
|
+
* Jalasoft Device Dump (http://www.jalasoft.com)
|
7
|
+
|
8
|
+
== Sample Workflows
|
9
|
+
|
10
|
+
Create an SNMP Device Dump for jalasoft Simulator of a Cisco SNMP Agent running on 192.168.1.10
|
11
|
+
and store it in ./cisco.xml.
|
12
|
+
|
13
|
+
snmpdump -D JalasoftDumper -C "Cisco Switches" -o ./cisco.xml 192.168.1.10
|
14
|
+
|
15
|
+
== Setup
|
16
|
+
|
17
|
+
gem install snmp
|
18
|
+
gem install snmpdumper
|
19
|
+
|
20
|
+
== Meta
|
21
|
+
|
22
|
+
Created and maintained by Sebastian de Castelberg
|
23
|
+
|
24
|
+
Patches contributed by: -
|
25
|
+
|
26
|
+
Released under the MIT license: http://www.opensource.org/licenses/mit-license.php
|
27
|
+
|
28
|
+
http://github.com/sdecastelberg/snmpdumper
|
data/bin/snmpdumper
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
%w(builder).each { |f| require f }
|
2
|
+
|
3
|
+
module SnmpDumper
|
4
|
+
GENERIC_VALUE_CALLBACK = lambda do |value_set, name, values|
|
5
|
+
value = values.first
|
6
|
+
value_set.JSVALUE("value" => value)
|
7
|
+
end
|
8
|
+
|
9
|
+
OCTETSTRING_VALUE_CALLBACK = lambda do |value_set, name, values|
|
10
|
+
value = values.first
|
11
|
+
if value =~ /[[:cntrl:]]/ then
|
12
|
+
value_set.JSVALUE("value" => value.unpack('H*').first.upcase.scan(/.{1,2}/).join(" "), "hexa" => 1)
|
13
|
+
else
|
14
|
+
value_set.JSVALUE("value" => "foo")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
TIMETICKS_VALUE_CALLBACK = lambda do |value_set, name, values|
|
19
|
+
value_set.JSVALUE("value" => values.first.to_i)
|
20
|
+
end
|
21
|
+
|
22
|
+
INTEGER_VALUE_CALLBACK = lambda do |value_set, name, values|
|
23
|
+
values = values.collect {|x| x.to_i}
|
24
|
+
values.uniq!
|
25
|
+
|
26
|
+
if values.size > 1 then
|
27
|
+
values.each do |value|
|
28
|
+
value_set.JSVALUE("value" => value.to_i, "weight" => 100 / values.size)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
value_set.JSVALUE("value" => values.first.to_i)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
DATA_TYPE_MAP = {
|
36
|
+
SNMP::Counter32 => {:syntax => "COUNTER", :callback => GENERIC_VALUE_CALLBACK},
|
37
|
+
SNMP::Counter64 => {:syntax => "COUNTER", :callback => GENERIC_VALUE_CALLBACK},
|
38
|
+
SNMP::Gauge32 => {:syntax => "GAUGE", :callback => GENERIC_VALUE_CALLBACK},
|
39
|
+
SNMP::Integer => {:syntax => "INTEGER", :callback => INTEGER_VALUE_CALLBACK},
|
40
|
+
SNMP::IpAddress => {:syntax => "IPADDRESS", :callback => GENERIC_VALUE_CALLBACK},
|
41
|
+
SNMP::ObjectId => {:syntax => "OBJECTIDENTIFIER", :callback => GENERIC_VALUE_CALLBACK},
|
42
|
+
SNMP::OctetString => {:syntax => "OCTETSTRING", :callback => OCTETSTRING_VALUE_CALLBACK},
|
43
|
+
SNMP::TimeTicks => {:syntax => "TIMETICKS", :callback => TIMETICKS_VALUE_CALLBACK},
|
44
|
+
String => {:syntax => "OCTETSTRING", :callback => GENERIC_VALUE_CALLBACK},
|
45
|
+
}
|
46
|
+
|
47
|
+
class SnmpVar
|
48
|
+
attr_accessor :name, :values
|
49
|
+
def initialize(args)
|
50
|
+
raise ArgumentError.new("Wrong paramters: ") unless args[:name]
|
51
|
+
self.name = args[:name]
|
52
|
+
self.values = []
|
53
|
+
end
|
54
|
+
|
55
|
+
def dump(jssnmpdevice)
|
56
|
+
jssnmpdevice.JSSNMPVAR do |jssnmpvar|
|
57
|
+
syntax_hash = DATA_TYPE_MAP[values.first.class] || {:syntax => "OCTETSTRING", :callback => OCTETSTRING_VALUE_CALLBACK}
|
58
|
+
jssnmpvar.JSOID("syntax" => syntax_hash[:syntax], "value" => name)
|
59
|
+
jssnmpvar.JSVALUESET { |value_set| DATA_TYPE_MAP[values.first.class][:callback].call(value_set, name, values) }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class JalasoftDumper
|
65
|
+
attr_accessor :category
|
66
|
+
attr_accessor :model
|
67
|
+
attr_accessor :snmp_vars
|
68
|
+
attr_accessor :model
|
69
|
+
attr_accessor :category
|
70
|
+
|
71
|
+
def initialize(options)
|
72
|
+
@model = options.model
|
73
|
+
@category = options.category
|
74
|
+
@snmp_vars = Hash.new
|
75
|
+
end
|
76
|
+
|
77
|
+
def dump
|
78
|
+
builder = Builder::XmlMarkup.new(:indent=>2)
|
79
|
+
|
80
|
+
builder.JSSNMPDEVICE("category" => @category, "model" => get_model) do |jssnmpdevice|
|
81
|
+
@snmp_vars.values.each do |snmp_var|
|
82
|
+
snmp_var.dump(jssnmpdevice)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def add_snmp_var(args)
|
88
|
+
raise ArgumentError.new("Invalid arguments") unless args[:name] && args[:value]
|
89
|
+
@snmp_vars[args[:name]] ||= SnmpVar.new(:name => args[:name])
|
90
|
+
@snmp_vars[args[:name]].values << args[:value]
|
91
|
+
end
|
92
|
+
|
93
|
+
def get_model
|
94
|
+
return @model if @model
|
95
|
+
m = @snmp_vars[".1.3.6.1.2.1.1.1.0"]
|
96
|
+
return "Unknown Model" unless m
|
97
|
+
m.values.first
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/lib/options.rb
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
module SnmpDumper
|
6
|
+
class Options
|
7
|
+
SNMP_VERSIONS = {"1" => :SNMPv1, "2c" => :SNMPv2c, "3" => :SNMPv3}
|
8
|
+
DEFAULT_PORT = 161
|
9
|
+
|
10
|
+
DEFAULT_INTERVAL = 10
|
11
|
+
DEFAULT_WALKS = 3
|
12
|
+
|
13
|
+
DEFAULT_COMMUNITY = "public"
|
14
|
+
DEFAULT_CATEGORY = "Unknown Category"
|
15
|
+
DEFAULT_DUMPER = "JalasoftDumper"
|
16
|
+
|
17
|
+
attr_reader :options
|
18
|
+
|
19
|
+
def initialize(argv)
|
20
|
+
parse(argv)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def parse(argv)
|
25
|
+
@options = OpenStruct.new
|
26
|
+
@options.port = DEFAULT_PORT
|
27
|
+
@options.interval = DEFAULT_INTERVAL
|
28
|
+
@options.walks = DEFAULT_WALKS
|
29
|
+
@options.oids = [
|
30
|
+
"1",
|
31
|
+
"1.3.6.1.2.1.10",
|
32
|
+
"1.3.6.1.2.1.1",
|
33
|
+
"1.3.6.1.2.1.11",
|
34
|
+
"1.3.6.1.2.1.16",
|
35
|
+
"1.3.6.1.2.1.17",
|
36
|
+
"1.3.6.1.2.1.2",
|
37
|
+
"1.3.6.1.2.1.4",
|
38
|
+
"1.3.6.1.2.1.47",
|
39
|
+
"1.3.6.1.4.1.9.2.1.40.0",
|
40
|
+
"1.3.6.1.4.1.9.2.1.41.0",
|
41
|
+
"1.3.6.1.4.1.9.2.1.42.0",
|
42
|
+
"1.3.6.1.4.1.9.2.1.43.0",
|
43
|
+
"1.3.6.1.4.1.9.2.1.44.0",
|
44
|
+
"1.3.6.1.4.1.9.2.1.45.0",
|
45
|
+
"1.3.6.1.4.1.9.2.1.46.0",
|
46
|
+
"1.3.6.1.4.1.9.2.1.47.0",
|
47
|
+
"1.3.6.1.4.1.9.2.1.48.0",
|
48
|
+
"1.3.6.1.4.1.9.2.1.49.0",
|
49
|
+
"1.3.6.1.4.1.9.2.4.1.1",
|
50
|
+
"1.3.6.1.4.1.9.9.10.1.1.3.1.1.2.1.1",
|
51
|
+
"1.3.6.1.4.1.9.9.10.1.1.3.1.1.3.1.1",
|
52
|
+
"1.3.6.1.4.1.9.9.10.1.1.3.1.1.4.1.1",
|
53
|
+
"1.3.6.1.4.1.9.9.10.1.1.3.1.1.5.1.1",
|
54
|
+
"1.3.6.1.4.1.9.9.10.1.1.3.1.1.6.1.1",
|
55
|
+
"1.3.6.1.4.1.9.9.10.1.1.3.1.1.7.1.1",
|
56
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.10.1.1",
|
57
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.11.1.1",
|
58
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.12.1.1",
|
59
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.2.1.1",
|
60
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.3.1.1",
|
61
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.4.1.1",
|
62
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.5.1.1",
|
63
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.6.1.1",
|
64
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.7.1.1",
|
65
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.8.1.1",
|
66
|
+
"1.3.6.1.4.1.9.9.10.1.1.4.1.1.9.1.1",
|
67
|
+
"1.3.6.1.4.1.9.9.106.1.1.1.0",
|
68
|
+
"1.3.6.1.4.1.9.2.1",
|
69
|
+
"1.3.6.1.4.1.9.2.2",
|
70
|
+
"1.3.6.1.4.1.9.3.6",
|
71
|
+
"1.3.6.1.4.1.9.2.9",
|
72
|
+
"1.3.6.1.4.1.9.2.10",
|
73
|
+
"1.3.6.1.4.1.9.9.10",
|
74
|
+
"1.3.6.1.4.1.9.9.23",
|
75
|
+
"1.3.6.1.4.1.9.9.46",
|
76
|
+
"1.3.6.1.4.1.9.9.48",
|
77
|
+
"1.3.6.1.4.1.9.9.68",
|
78
|
+
"1.3.6.1.4.1.9.9.87",
|
79
|
+
"1.3.6.1.4.1.9.9.109",
|
80
|
+
"1.3.6.1.4.1.9.1.324",
|
81
|
+
"1.3.6.1.4.1.9.9.134"
|
82
|
+
]
|
83
|
+
|
84
|
+
@options.version = :SNMPv2c
|
85
|
+
@options.community = DEFAULT_COMMUNITY
|
86
|
+
@options.category = DEFAULT_CATEGORY
|
87
|
+
@options.dumper = DEFAULT_DUMPER
|
88
|
+
|
89
|
+
opts = OptionParser.new do |opts|
|
90
|
+
opts.banner = "Usage: snmpdumper [options] [host] [oids]"
|
91
|
+
|
92
|
+
opts.separator ""
|
93
|
+
opts.separator "Specific options:"
|
94
|
+
|
95
|
+
opts.on("-p", "--port PORT", Integer, "SNMP agent port (Default: #{DEFAULT_PORT})") do |port|
|
96
|
+
@options.port = port
|
97
|
+
end
|
98
|
+
|
99
|
+
opts.on("-i", "--interval INTERVAL", Integer, "walk interval in seconds (Default: #{DEFAULT_INTERVAL})") do |interval|
|
100
|
+
@options.interval = interval
|
101
|
+
end
|
102
|
+
|
103
|
+
opts.on("-w walks", "--walks WALKS", Integer, "number of walks (Default: #{DEFAULT_WALKS})") do |walks|
|
104
|
+
@options.walks = walks
|
105
|
+
end
|
106
|
+
|
107
|
+
opts.on("-O", "--output FILENAME", "file to save SNMP dump") do |out_filename|
|
108
|
+
@options.out_filename = out_filename
|
109
|
+
end
|
110
|
+
|
111
|
+
opts.on("-I", "--input FILENAME", "file with output of snmpwalk") do |in_filename|
|
112
|
+
@options.in_filename = in_filename
|
113
|
+
end
|
114
|
+
|
115
|
+
opts.on("-m", "--model MODELNAME", "model name (Default: dynamically taken from sysDescr)") do |model|
|
116
|
+
@options.model = model
|
117
|
+
end
|
118
|
+
|
119
|
+
opts.on("-C", "--category CATEGORY", "category name (Default: #{DEFAULT_CATEGORY})") do |category|
|
120
|
+
@options.category = category
|
121
|
+
end
|
122
|
+
|
123
|
+
opts.on("-D", "--dumper DUMPER_CLASS", "which dumper to use (Default: #{DEFAULT_DUMPER})") do |dumper|
|
124
|
+
@options.dumper = dumper
|
125
|
+
end
|
126
|
+
|
127
|
+
opts.separator ""
|
128
|
+
opts.separator "snmpwalk options:"
|
129
|
+
|
130
|
+
opts.on("-r", "--retries RETRIES", Integer, "set the number of retries") do |retries|
|
131
|
+
@options.retries = retries
|
132
|
+
end
|
133
|
+
|
134
|
+
opts.on("-t", "--timeout TIMEOUT", Integer, "set the request timeout (in seconds)") do |timeout|
|
135
|
+
@options.timeout = timeout
|
136
|
+
end
|
137
|
+
|
138
|
+
opts.on("-v VERSION", SNMP_VERSIONS,
|
139
|
+
"specifies SNMP version to use #{SNMP_VERSIONS.keys.join(', ')} (Default: #{@options.version})") do |version|
|
140
|
+
options.version = version if version
|
141
|
+
end
|
142
|
+
|
143
|
+
opts.separator ""
|
144
|
+
opts.separator "SNMP Version 1 or 2c specific:"
|
145
|
+
|
146
|
+
opts.on("-c", "--community COMMUNITY", "set the community string") do |community|
|
147
|
+
@options.community = community
|
148
|
+
end
|
149
|
+
|
150
|
+
opts.separator ""
|
151
|
+
opts.separator "SNMP Version 3 specific:"
|
152
|
+
|
153
|
+
opts.on("-A PASSPHRASE", "set authentication protocol pass phrase") do |auth_passphrase|
|
154
|
+
@options.auth_passphrase = auth_passphrase
|
155
|
+
end
|
156
|
+
|
157
|
+
opts.on("-X PASSPHRASE", "set privacy protocol pass phrase") do |privacy_passphrase|
|
158
|
+
@options.privacy_passphrase = privacy_passphrase
|
159
|
+
end
|
160
|
+
|
161
|
+
opts.separator ""
|
162
|
+
opts.separator "Common options:"
|
163
|
+
|
164
|
+
opts.on("-d", "--[no-]debug", "print debug messages") do |d|
|
165
|
+
$DEBUG = d
|
166
|
+
end
|
167
|
+
|
168
|
+
opts.on("-f", "--force", "do not stop on errors") do |f|
|
169
|
+
@options.force = f
|
170
|
+
end
|
171
|
+
|
172
|
+
opts.on("-h", "--help", "display this help message") do
|
173
|
+
puts opts
|
174
|
+
exit
|
175
|
+
end
|
176
|
+
|
177
|
+
opts.on_tail("-V", "--version", "show version") do
|
178
|
+
puts OptionParser::Version.join('.')
|
179
|
+
exit
|
180
|
+
end
|
181
|
+
|
182
|
+
begin
|
183
|
+
opts.parse!(argv)
|
184
|
+
raise OptionParser::MissingArgument.new("Please provide AUTH and PRIVACY passphrase") if
|
185
|
+
@options.version == :SNMPv3 &&
|
186
|
+
(@options.auth_passphrase.nil? || @options.privacy_passphrase.nil?)
|
187
|
+
|
188
|
+
raise OptionParser::MissingArgument.new("No hostname provided") if $stdin.tty? && argv.empty? && !@options.in_filename
|
189
|
+
|
190
|
+
@options.host = argv.shift
|
191
|
+
@options.oids = argv unless argv.empty?
|
192
|
+
|
193
|
+
STDERR.puts @options if $DEBUG
|
194
|
+
|
195
|
+
rescue OptionParser::ParseError => e
|
196
|
+
STDERR.puts e.message, "\n", opts
|
197
|
+
exit(-1)
|
198
|
+
end
|
199
|
+
|
200
|
+
|
201
|
+
end #OptionParser.new
|
202
|
+
|
203
|
+
end #parse(argv)
|
204
|
+
|
205
|
+
end #class Options
|
206
|
+
end # module SnmpDumper
|
data/lib/runner.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'walker'
|
2
|
+
require 'snmpwalk_reader'
|
3
|
+
require 'options'
|
4
|
+
|
5
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'dumper/*.rb')).each {|f| require f }
|
6
|
+
|
7
|
+
module SnmpDumper
|
8
|
+
class Runner
|
9
|
+
def initialize(argv)
|
10
|
+
@options = Options.new(argv)
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
begin
|
15
|
+
## interactive shell?
|
16
|
+
if !$stdin.tty? || @options.options.inputfile then
|
17
|
+
walker = SnmpwalkReader.new(@options.options)
|
18
|
+
else
|
19
|
+
walker = Walker.new(@options.options)
|
20
|
+
end
|
21
|
+
|
22
|
+
dumper = SnmpDumper.const_get(@options.options.dumper)::new(@options.options)
|
23
|
+
|
24
|
+
walker.walk(dumper)
|
25
|
+
|
26
|
+
if @options.options.out_filename
|
27
|
+
File.open(@options.options.out_filename, 'w') { |f| f.write(dumper.dump) }
|
28
|
+
else
|
29
|
+
puts dumper.dump
|
30
|
+
end
|
31
|
+
|
32
|
+
rescue Exception => e
|
33
|
+
STDERR.puts e.message
|
34
|
+
STDERR.puts e.backtrace.join("\n") if $DEBUG
|
35
|
+
exit(-1)
|
36
|
+
end
|
37
|
+
exit(0)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module SnmpDumper
|
2
|
+
class SnmpwalkReader
|
3
|
+
CLASS_CALLBACK = {
|
4
|
+
"Gauge32" => lambda { |value| SNMP::Gauge32.new(Integer(value)) },
|
5
|
+
"Counter32" => lambda { |value| SNMP::Counter32.new(Integer(value)) },
|
6
|
+
"Counter64" => lambda { |value| SNMP::Counter64.new(Integer(value)) },
|
7
|
+
"Hex-STRING" => lambda { |value| SNMP::OctetString.new(value) },
|
8
|
+
"IpAddress" => lambda { |value| SNMP::IpAddress.new(value) },
|
9
|
+
"OID" => lambda { |value|
|
10
|
+
value.strip!
|
11
|
+
/^\.?((?:\d+\.)*\d+)$/ =~ value
|
12
|
+
raise ArgumentError, value if Regexp.last_match(0).nil?
|
13
|
+
SNMP::ObjectId.new(Regexp.last_match(1))
|
14
|
+
}, "Timeticks" => lambda { |value|
|
15
|
+
value.strip!
|
16
|
+
/^\((\d+)\).*$/ =~ value
|
17
|
+
raise value if Regexp.last_match(0).nil?
|
18
|
+
SNMP::TimeTicks.new(Integer(Regexp.last_match(1)))
|
19
|
+
}, "INTEGER" => lambda { |value|
|
20
|
+
/^(.*\()*(\d+)(\))*$/ =~ value
|
21
|
+
return nil if Regexp.last_match(0).nil?
|
22
|
+
SNMP::Integer.new(Integer(Regexp.last_match(2)))
|
23
|
+
},
|
24
|
+
}
|
25
|
+
|
26
|
+
|
27
|
+
def initialize(options)
|
28
|
+
STDERR.puts "SnmpwalkReader: initialize" if $DEBUG
|
29
|
+
@options = options
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse_snmpwalk_line(line)
|
33
|
+
result = {}
|
34
|
+
/^(\.?\d+(?:\.?\d)+)\s*=\s*([^:]+):\s*\"?([^"]*)\"?$/ =~ line
|
35
|
+
|
36
|
+
if Regexp.last_match(0).nil?
|
37
|
+
raise ArgumentError, "Invalid line: #{line}"
|
38
|
+
end
|
39
|
+
result = {:oid => Regexp.last_match(1), :class => Regexp.last_match(2) }
|
40
|
+
callback = CLASS_CALLBACK[result[:class]]
|
41
|
+
|
42
|
+
value = Regexp.last_match(3)
|
43
|
+
value = callback.call(value) if callback
|
44
|
+
result[:value] = value
|
45
|
+
|
46
|
+
STDERR.puts "SnmpwalkReader: oid = #{result[:oid]} / class = #{result[:class]} / value = #{result[:value]}" if $DEBUG
|
47
|
+
result
|
48
|
+
end
|
49
|
+
|
50
|
+
def walk(dumper)
|
51
|
+
STDERR.puts "SnmpwalkReader: walk start" if $DEBUG
|
52
|
+
|
53
|
+
if $stdin.tty? || @options.in_filename then
|
54
|
+
STDERR.puts "SnmpwalkReader: read from file #{@options.in_filename}" if $DEBUG
|
55
|
+
input = File.open(@options.in_filename, 'r')
|
56
|
+
else
|
57
|
+
STDERR.puts "SnmpwalkReader: read from stdin" if $DEBUG
|
58
|
+
input = $stdin
|
59
|
+
end
|
60
|
+
|
61
|
+
input.each_line do |line|
|
62
|
+
line.rstrip! # Remove trailing newline
|
63
|
+
STDERR.puts "SnmpwalkReader: processing line #{line}" if $DEBUG
|
64
|
+
|
65
|
+
begin
|
66
|
+
result = parse_snmpwalk_line line
|
67
|
+
dumper.add_snmp_var({:name => result[:oid], :value => result[:value]})
|
68
|
+
rescue Exception => e
|
69
|
+
raise ArgumentError, "Error encountered while parsing the line (-f ignore errors): \n
|
70
|
+
#{line}\n
|
71
|
+
Format for input:\n
|
72
|
+
<numerical oid> = <Type>: <value>\n
|
73
|
+
Example: .1.3.6.1.2.1.1.1.0 = STRING: Linux host.local 2.6.17.6 #1 SMP Thu Mar 8 15:32:13 CET 2007 i686\n
|
74
|
+
snmpwalk command example: snmpwalk -v 3 -u user -A PW -X PW -a MD5 -x DES -l authPriv -On -Oa 192.168.1.1" unless @options.force
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
input.close
|
80
|
+
|
81
|
+
STDERR.puts "SnmpwalkReader: walk start" if $DEBUG
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/walker.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
%w(snmp).each { |f| require f }
|
2
|
+
|
3
|
+
module SnmpDumper
|
4
|
+
class Walker
|
5
|
+
def initialize(options)
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def walk(dumper)
|
10
|
+
snmpconfig = { :Host => @options.host, :Port => @options.port, :Version => @options.version}
|
11
|
+
|
12
|
+
snmpconfig.merge!({:Timeout => @options.timeout }) if @options.timeout
|
13
|
+
snmpconfig.merge!({:Retries => @options.retries }) if @options.retries
|
14
|
+
|
15
|
+
if @options.version == :SNMPv3 then
|
16
|
+
snmpconfig.merge! Hash.new()
|
17
|
+
else
|
18
|
+
snmpconfig.merge! :Community => @options.community
|
19
|
+
end
|
20
|
+
|
21
|
+
STDERR.puts snmpconfig if $DEBUG
|
22
|
+
manager = SNMP::Manager.new(snmpconfig)
|
23
|
+
|
24
|
+
model = @options.model || manager.get_value('sysDescr.0')
|
25
|
+
|
26
|
+
dumper.model = model
|
27
|
+
dumper.category = @options.category
|
28
|
+
|
29
|
+
(1..@options.walks).each do |i|
|
30
|
+
STDERR.puts "Walk #{i}/#{@options.walks}: start" if $DEBUG
|
31
|
+
|
32
|
+
@options.oids.each do |oid|
|
33
|
+
begin
|
34
|
+
manager.walk(oid) do |var_bind|
|
35
|
+
dumper.add_snmp_var({:name => var_bind.name, :value => var_bind.value})
|
36
|
+
end
|
37
|
+
rescue SNMP::RequestTimeout => e
|
38
|
+
raise e if dumper.snmp_vars.empty?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
STDERR.puts "Walk #{i}/#{@options.walks}: end" if $DEBUG
|
43
|
+
if i != @options.walks then
|
44
|
+
STDERR.puts "Sleep for #{@options.interval} seconds" if $DEBUG
|
45
|
+
sleep @options.interval
|
46
|
+
end
|
47
|
+
end
|
48
|
+
manager.close
|
49
|
+
|
50
|
+
end # (1..@options.walks).each
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snmpdumper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sebastian de Castelberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-23 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: snmp
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: builder
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.2
|
34
|
+
version:
|
35
|
+
description: Dump snmp walks in various formats
|
36
|
+
email: snmpdumper@kpricorn.org
|
37
|
+
executables:
|
38
|
+
- snmpdumper
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- README
|
45
|
+
- LICENSE
|
46
|
+
- bin/snmpdumper
|
47
|
+
- lib/dumper/jalasoft_dumper.rb
|
48
|
+
- lib/dumper/simple_text_dumper.rb
|
49
|
+
- lib/options.rb
|
50
|
+
- lib/runner.rb
|
51
|
+
- lib/snmpwalk_reader.rb
|
52
|
+
- lib/walker.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/sdecastelberg/snmpdumper
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.5
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Dumps SNMP walk output in different format.
|
81
|
+
test_files: []
|
82
|
+
|