snmp 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +15 -7
- data/Rakefile +2 -3
- data/examples/datetime.rb +22 -0
- data/examples/link_down.rb +49 -0
- data/lib/snmp/ber.rb +25 -13
- data/lib/snmp/varbind.rb +11 -5
- data/test/test_mib.rb +6 -0
- metadata +43 -33
data/README
CHANGED
@@ -9,7 +9,7 @@ that Ruby can run.
|
|
9
9
|
|
10
10
|
See snmplib.rubyforge.org[http://snmplib.rubyforge.org/] for more info.
|
11
11
|
|
12
|
-
Version 1.0.
|
12
|
+
Version 1.0.2 of this software supports the following:
|
13
13
|
|
14
14
|
* The GetRequest, GetNextRequest, GetBulkRequest, SetRequest, Response
|
15
15
|
SNMPv1_Trap, SNMPv2_Trap, and Inform PDUs
|
@@ -18,13 +18,20 @@ Version 1.0.0 of this software supports the following:
|
|
18
18
|
* Trap handling for informs and v1 and v2 traps
|
19
19
|
* Symbolic OID values (ie. "ifTable" instead of "1.3.6.1.2.1.2.2") as
|
20
20
|
parameters to the SNMP.Manager API
|
21
|
-
* Includes symbol data files for all current IETF MIBs
|
21
|
+
* Includes symbol data files for all current IETF MIBs
|
22
|
+
* Compatibility with both Ruby 1.8 and Ruby 1.9
|
22
23
|
|
23
24
|
See the SNMP.Manager, SNMP.TrapListener, and SNMP.MIB classes and the
|
24
25
|
examples below for more details.
|
25
26
|
|
26
27
|
== Changes
|
27
28
|
|
29
|
+
Changes for version 1.0.2:
|
30
|
+
|
31
|
+
* Internal code changes to make this library compatible with both Ruby 1.8
|
32
|
+
and Ruby 1.9. Note that an ord() method is now added to the Fixnum class
|
33
|
+
for Ruby 1.8. See the ber.rb file for details.
|
34
|
+
|
28
35
|
Changes for version 1.0.1:
|
29
36
|
|
30
37
|
* Made the host configurable for the TrapListener. Previously defaulted
|
@@ -114,25 +121,26 @@ formats.
|
|
114
121
|
From the .gem file you can install using
|
115
122
|
RubyGems[http://rubyforge.org/projects/rubygems].
|
116
123
|
|
117
|
-
gem install snmp-1.0.
|
124
|
+
gem install snmp-1.0.2.gem
|
118
125
|
|
119
126
|
From the .tgz or .zip file you can install using
|
120
127
|
setup.rb[http://i.loveruby.net/en/prog/setup.html]. Uncompress the archive
|
121
128
|
and then run setup.
|
122
129
|
|
123
|
-
cd snmp-1.0.
|
130
|
+
cd snmp-1.0.2
|
124
131
|
ruby setup.rb (may require root privilege)
|
125
132
|
|
126
133
|
== Testing
|
127
134
|
|
128
135
|
This library has received limited testing:
|
129
|
-
* The unit tests have been executed with Ruby 1.8.
|
136
|
+
* The unit tests have been executed with both Ruby 1.8.6 and Ruby 1.9.0 on
|
137
|
+
Mac OS X 10.5.
|
130
138
|
* Basic interoperability testing has been done with the
|
131
139
|
net-snmp[http://www.net-snmp.org/] tools.
|
132
140
|
|
133
141
|
I'm very interested in hearing about successes or failures on other platforms.
|
134
142
|
|
135
|
-
Send me an email at
|
143
|
+
Send me an email at hallidave at gmail.com.
|
136
144
|
|
137
145
|
== Examples
|
138
146
|
|
@@ -230,7 +238,7 @@ Log traps to STDOUT.
|
|
230
238
|
|
231
239
|
== License
|
232
240
|
|
233
|
-
This SNMP Library is Copyright (c) 2004
|
241
|
+
This SNMP Library is Copyright (c) 2004-2007 by David R. Halliday. It is free
|
234
242
|
software. Redistribution is permitted under the same terms and conditions as
|
235
243
|
the standard Ruby distribution. See the COPYING file in the Ruby distribution
|
236
244
|
for details.
|
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ Rake::TestTask.new do |test|
|
|
10
10
|
end
|
11
11
|
|
12
12
|
# package target
|
13
|
-
PKG_VERSION = '1.0.
|
13
|
+
PKG_VERSION = '1.0.2'
|
14
14
|
PKG_FILES = FileList[
|
15
15
|
'Rakefile',
|
16
16
|
'README',
|
@@ -30,7 +30,6 @@ spec = Gem::Specification.new do |s|
|
|
30
30
|
s.summary = "A Ruby implementation of SNMP (the Simple Network Management Protocol)."
|
31
31
|
s.name = 'snmp'
|
32
32
|
s.version = PKG_VERSION
|
33
|
-
s.autorequire = 'snmp'
|
34
33
|
s.files = PKG_FILES.to_a
|
35
34
|
s.has_rdoc = true
|
36
35
|
s.extra_rdoc_files = ['README']
|
@@ -38,7 +37,7 @@ spec = Gem::Specification.new do |s|
|
|
38
37
|
'--title' << 'SNMP Library for Ruby'
|
39
38
|
s.description = "A Ruby implementation of SNMP (the Simple Network Management Protocol)."
|
40
39
|
s.author = 'Dave Halliday'
|
41
|
-
s.email = '
|
40
|
+
s.email = 'hallidave@gmail.com'
|
42
41
|
s.rubyforge_project = 'snmplib'
|
43
42
|
s.homepage = 'http://snmplib.rubyforge.org'
|
44
43
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'snmp'
|
2
|
+
|
3
|
+
module SNMP
|
4
|
+
class OctetString
|
5
|
+
def to_date_time
|
6
|
+
raise ArgumentError, "not DateAndTime format" if size != 8 and size != 11
|
7
|
+
year = self[0].ord * 256 + self[1].ord
|
8
|
+
month = self[2].ord
|
9
|
+
day = self[3].ord
|
10
|
+
hour = self[4].ord
|
11
|
+
minutes = self[5].ord
|
12
|
+
seconds = self[6].ord
|
13
|
+
tenths = self[7].ord
|
14
|
+
sprintf("%d-%d-%d,%02d:%02d:%02d.%d", year, month, day, hour, minutes, seconds, tenths)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
SNMP::Manager.open(:Host => 'localhost', :MibModules => ["HOST-RESOURCES-MIB"]) do |snmp|
|
20
|
+
response = snmp.get_value("hrSystemDate.0")
|
21
|
+
puts response.to_date_time
|
22
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'snmp'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
log = Logger.new($stdout)
|
6
|
+
|
7
|
+
mib = SNMP::MIB.new
|
8
|
+
mib.load_module("IF-MIB")
|
9
|
+
linkDown_OID = mib.oid("linkDown")
|
10
|
+
|
11
|
+
# 1 is "up" and 2 is "down" in the MIB
|
12
|
+
ADMIN_UP = 1
|
13
|
+
ADMIN_DOWN = 2
|
14
|
+
|
15
|
+
listener = SNMP::TrapListener.new(:Port => 1062, :Community => 'public') do |listener|
|
16
|
+
log.info "Listening for linkDown traps"
|
17
|
+
listener.on_trap(linkDown_OID) do |trap|
|
18
|
+
vb_list = trap.vb_list
|
19
|
+
up_time = vb_list[0].value # standard varbind - always in v2c trap
|
20
|
+
trap_oid = vb_list[1].value # standard varbind - always in v2c trap
|
21
|
+
|
22
|
+
# extract trap info - order determined by IF-MIB
|
23
|
+
ifIndex = vb_list[2].value
|
24
|
+
adminStatus = vb_list[3].value
|
25
|
+
operStatus = vb_list[4].value
|
26
|
+
|
27
|
+
# log stuff based on interface status
|
28
|
+
if adminStatus == ADMIN_DOWN
|
29
|
+
log.info "Interface #{ifIndex} turned down"
|
30
|
+
else
|
31
|
+
log.error "Problem on interface #{ifIndex}! Link unintentionally down"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
listener.on_trap_v1 do |trap|
|
36
|
+
log.warn "Unexpected v1 trap: #{trap.generic_trap}"
|
37
|
+
end
|
38
|
+
|
39
|
+
listener.on_trap_v2c do |trap|
|
40
|
+
log.warn "Unexpected v2c trap: #{trap.trap_oid}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
trap("INT") do
|
45
|
+
puts "\nShutting down"
|
46
|
+
listener.kill
|
47
|
+
end
|
48
|
+
|
49
|
+
listener.join
|
data/lib/snmp/ber.rb
CHANGED
@@ -7,6 +7,17 @@
|
|
7
7
|
# COPYING file in the Ruby distribution for details.
|
8
8
|
#
|
9
9
|
|
10
|
+
#
|
11
|
+
# Add ord method to Fixnum for forward compatibility with Ruby 1.9
|
12
|
+
#
|
13
|
+
if "a"[0].kind_of? Fixnum
|
14
|
+
unless Fixnum.methods.include? :ord
|
15
|
+
class Fixnum
|
16
|
+
def ord; self; end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
10
21
|
#
|
11
22
|
# This module implements methods for encoding and decoding SNMP packets
|
12
23
|
# using the ASN.1 BER (Basic Encoding Rules).
|
@@ -84,9 +95,9 @@ module BER #:nodoc:all
|
|
84
95
|
# multi-byte).
|
85
96
|
#
|
86
97
|
def decode_tlv(data)
|
87
|
-
raise OutOfData if (data.length == 2 && data[1] != 0) || data.length < 2
|
88
|
-
tag = data[0]
|
89
|
-
length = data[1]
|
98
|
+
raise OutOfData if (data.length == 2 && data[1].ord != 0) || data.length < 2
|
99
|
+
tag = data[0].ord
|
100
|
+
length = data[1].ord
|
90
101
|
if length < 0x80
|
91
102
|
value = data[2, length]
|
92
103
|
remainder = data[length+2..-1]
|
@@ -122,7 +133,7 @@ module BER #:nodoc:all
|
|
122
133
|
|
123
134
|
def decode_integer_value(value)
|
124
135
|
result = build_integer(value, 0, value.length)
|
125
|
-
if value[0][7] == 1
|
136
|
+
if value[0].ord[7] == 1
|
126
137
|
result -= (1 << (8 * value.length))
|
127
138
|
end
|
128
139
|
result
|
@@ -139,7 +150,7 @@ module BER #:nodoc:all
|
|
139
150
|
|
140
151
|
def build_integer(data, start, num_octets)
|
141
152
|
number = 0
|
142
|
-
num_octets.times { |i| number = number<<8 | data[start+i] }
|
153
|
+
num_octets.times { |i| number = number<<8 | data[start+i].ord }
|
143
154
|
return number
|
144
155
|
end
|
145
156
|
|
@@ -197,18 +208,19 @@ module BER #:nodoc:all
|
|
197
208
|
if value.length == 0
|
198
209
|
object_id = []
|
199
210
|
else
|
200
|
-
|
211
|
+
value0 = value[0].ord
|
212
|
+
if value0 == 0x2b
|
201
213
|
object_id = [1,3]
|
202
214
|
else
|
203
|
-
second =
|
204
|
-
first = (
|
215
|
+
second = value0 % 40
|
216
|
+
first = (value0 - second) / 40
|
205
217
|
raise InvalidObjectId, value.to_s if first > 2
|
206
218
|
object_id = [first, second]
|
207
219
|
end
|
208
220
|
n = 0
|
209
221
|
for i in 1...value.length
|
210
|
-
n = (n<<7) + (value[i] & 0x7f)
|
211
|
-
if value[i] < 0x80
|
222
|
+
n = (n<<7) + (value[i].ord & 0x7f)
|
223
|
+
if value[i].ord < 0x80
|
212
224
|
object_id << n
|
213
225
|
n = 0
|
214
226
|
end
|
@@ -243,9 +255,9 @@ module BER #:nodoc:all
|
|
243
255
|
data = value.chr
|
244
256
|
else
|
245
257
|
data = integer_to_octets(value)
|
246
|
-
if value > 0 && data[0] > 0x7f
|
258
|
+
if value > 0 && data[0].ord > 0x7f
|
247
259
|
data = "\000" << data
|
248
|
-
elsif value < 0 && data[0] < 0x80
|
260
|
+
elsif value < 0 && data[0].ord < 0x80
|
249
261
|
data = "\377" << data
|
250
262
|
end
|
251
263
|
end
|
@@ -326,7 +338,7 @@ module BER #:nodoc:all
|
|
326
338
|
octets = (n & 0x7f | 0x80).chr << octets
|
327
339
|
n = n >> 7
|
328
340
|
end until n == 0
|
329
|
-
octets[-1]
|
341
|
+
octets[-1] = (octets[-1].ord & 0x7f).chr
|
330
342
|
data << octets
|
331
343
|
end
|
332
344
|
end
|
data/lib/snmp/varbind.rb
CHANGED
@@ -29,11 +29,17 @@ class VarBindList < Array
|
|
29
29
|
|
30
30
|
def initialize(varbind_list=[])
|
31
31
|
super()
|
32
|
-
varbind_list.
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
if varbind_list.respond_to? :to_str
|
33
|
+
self << ObjectId.new(varbind_list.to_str).to_varbind
|
34
|
+
elsif varbind_list.respond_to? :to_varbind
|
35
|
+
self << varbind_list.to_varbind
|
36
|
+
else
|
37
|
+
varbind_list.each do |item|
|
38
|
+
if item.respond_to? :to_str
|
39
|
+
self << ObjectId.new(item.to_str).to_varbind
|
40
|
+
else
|
41
|
+
self << item.to_varbind
|
42
|
+
end
|
37
43
|
end
|
38
44
|
end
|
39
45
|
end
|
data/test/test_mib.rb
CHANGED
@@ -33,7 +33,13 @@ class TestMib < Test::Unit::TestCase
|
|
33
33
|
|
34
34
|
assert_raise(ArgumentError) {
|
35
35
|
@mib.oid("IF-MIB::")
|
36
|
+
}
|
37
|
+
|
38
|
+
assert_raise(ArgumentError) {
|
39
|
+
MIB.new.oid("sysDescr.0")
|
36
40
|
}
|
41
|
+
|
42
|
+
assert_equal("1.2.3.4", MIB.new.oid("1.2.3.4").to_s)
|
37
43
|
end
|
38
44
|
|
39
45
|
def test_varbind_list
|
metadata
CHANGED
@@ -1,43 +1,37 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.11
|
3
|
-
specification_version: 1
|
4
2
|
name: snmp
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2006-05-04 00:00:00 -04:00
|
8
|
-
summary: A Ruby implementation of SNMP (the Simple Network Management Protocol).
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: snmp@halliday.ca
|
12
|
-
homepage: http://snmplib.rubyforge.org
|
13
|
-
rubyforge_project: snmplib
|
14
|
-
description: A Ruby implementation of SNMP (the Simple Network Management Protocol).
|
15
|
-
autorequire: snmp
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 1.0.2
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
6
|
authors:
|
29
7
|
- Dave Halliday
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2007-12-29 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A Ruby implementation of SNMP (the Simple Network Management Protocol).
|
17
|
+
email: hallidave@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
30
24
|
files:
|
31
25
|
- Rakefile
|
32
26
|
- README
|
33
27
|
- setup.rb
|
34
|
-
- lib/snmp.rb
|
35
28
|
- lib/snmp/agent.rb
|
36
29
|
- lib/snmp/ber.rb
|
37
30
|
- lib/snmp/manager.rb
|
38
31
|
- lib/snmp/mib.rb
|
39
32
|
- lib/snmp/pdu.rb
|
40
33
|
- lib/snmp/varbind.rb
|
34
|
+
- lib/snmp.rb
|
41
35
|
- test/test_ber.rb
|
42
36
|
- test/test_manager.rb
|
43
37
|
- test/test_mib.rb
|
@@ -48,9 +42,11 @@ files:
|
|
48
42
|
- test/test_walk.rb
|
49
43
|
- test/if_table1.yaml
|
50
44
|
- test/if_table6.yaml
|
45
|
+
- examples/datetime.rb
|
51
46
|
- examples/dump.rb
|
52
47
|
- examples/get.rb
|
53
48
|
- examples/iftable.rb
|
49
|
+
- examples/link_down.rb
|
54
50
|
- examples/log_traps.rb
|
55
51
|
- examples/set.rb
|
56
52
|
- examples/walk.rb
|
@@ -246,20 +242,34 @@ files:
|
|
246
242
|
- data/ruby/snmp/mibs/VDSL-LINE-MIB.yaml
|
247
243
|
- data/ruby/snmp/mibs/VRRP-MIB.yaml
|
248
244
|
- data/ruby/snmp/mibs/WWW-MIB.yaml
|
249
|
-
|
250
|
-
|
245
|
+
has_rdoc: true
|
246
|
+
homepage: http://snmplib.rubyforge.org
|
247
|
+
post_install_message:
|
251
248
|
rdoc_options:
|
252
249
|
- --main
|
253
250
|
- README
|
254
251
|
- --title
|
255
252
|
- SNMP Library for Ruby
|
256
|
-
|
257
|
-
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
253
|
+
require_paths:
|
254
|
+
- lib
|
255
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
256
|
+
requirements:
|
257
|
+
- - ">="
|
258
|
+
- !ruby/object:Gem::Version
|
259
|
+
version: "0"
|
260
|
+
version:
|
261
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
262
|
+
requirements:
|
263
|
+
- - ">="
|
264
|
+
- !ruby/object:Gem::Version
|
265
|
+
version: "0"
|
266
|
+
version:
|
262
267
|
requirements: []
|
263
268
|
|
264
|
-
|
269
|
+
rubyforge_project: snmplib
|
270
|
+
rubygems_version: 1.0.1
|
271
|
+
signing_key:
|
272
|
+
specification_version: 2
|
273
|
+
summary: A Ruby implementation of SNMP (the Simple Network Management Protocol).
|
274
|
+
test_files: []
|
265
275
|
|