snmpjr 0.1.4-java → 0.1.5-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmUwZDc1YTk1N2M5MzY5ZjAxODgwNTU3ZjgzMjVlZTE5MWM5MWU5Mg==
4
+ ZDMzMmM5ZDlhMTIyMTMyODUxMjUxNTE5ZDk2YzA1YWJkMGRmMzFjZg==
5
5
  data.tar.gz: !binary |-
6
- ZTRlZmVhNTg0ODEyNDBmYTc4ODE3ZjUwZTNmNDMzY2VmYjY1MDk0ZQ==
6
+ NDYzMGI3YzRiMzQxYWQxMThkOWM2ZjhhYzIzNDU1ZDFiYmUyNTFiMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjY5MjZhNWEwZTE4MGJhNTgzZmFhODcwMDRiNTY0MjAzOGE5ODExMDg5Y2Rl
10
- ZjhlMGZiNjJkYzcxZTQ0OWE2Y2RjMWE5Y2Q1NjhiZDdiMmY1ZTE1YmQ1NjI1
11
- OTAzYzViMDQ4ZjExNmE5MjY3NDVmZjhlNzRhNGUzZGEwMjZiMjI=
9
+ ZWQ2ZGVlM2I5ZWQyN2Y4ZDg1YjNiZWI0MGMzZjM1YmNkNjU3MTY5NWY0ZjU4
10
+ N2M4NjM1Njg0MWFlZmZjODA2MTA0MDI0NTE2NWVhMzdlYzZmZTVjMDVhZTUz
11
+ ODcxZDM2M2RmMTMxM2QxNGJjYjNmNjU4YmEwN2Q1NjgzMzYxZWI=
12
12
  data.tar.gz: !binary |-
13
- MDNiMDE3MWM4NDUxMjIyZjZhZDBkZGQwZjViN2E5NWM4NGFjYWZkYWIyYWYy
14
- MzgxNjU4MTgyODI1ODQ1YWQ2NWIwZDYwNjZiNGU4N2IwYjg4OTM2NWJlYzIw
15
- M2EzNmU4MmI0MWZiZGU4OTIxY2E1YTc1MDQxMjA0MDQ2ZTU5NjU=
13
+ Njg0ZTlkYzExYjZkOWMzNjNhMTFhNmM0ODFmNGZjZWM5MWEzYmRmMGJjMDlj
14
+ ZGZjNzM1NDhkNGRkMTZmN2ZhNGE3MjRmYzY4NjcwNGRiYzFlNjZmNzA1NmZl
15
+ ZDNlOGRiMDMyMWQzZWQzNjRjYTU1NzU3MDlhMDQ5NThhZGMzZGM=
data/history.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.1.5 (14 October 2014)
2
+
3
+ * Allow timeout to be specified in the initializer
4
+ * Package Oids in a single PDU up to the default maximum of 30 which can be overidden using :max_oids_per_request
5
+
1
6
  == 0.1.4 (14 October 2014)
2
7
 
3
8
  * Returns a Response object rather than strings
data/lib/snmpjr/getter.rb CHANGED
@@ -4,31 +4,32 @@ require "snmpjr/session"
4
4
  class Snmpjr
5
5
  class Getter
6
6
 
7
- def initialize target
8
- @target = target
7
+ def initialize args = {}
8
+ @target = args.fetch(:target)
9
+ @max_oids_per_request = args[:max_oids_per_request] || 30
9
10
  @session = Snmpjr::Session.new
10
11
  end
11
12
 
12
13
  def get_multiple oids
13
14
  @session.start
14
- result = oids.map {|oid|
15
- send_oid oid
16
- }
15
+ result = oids.each_slice(@max_oids_per_request).map{|partial_oids|
16
+ get_request partial_oids
17
+ }.flatten
17
18
  @session.close
18
19
  result
19
20
  end
20
21
 
21
22
  def get oid
22
23
  @session.start
23
- result = send_oid oid
24
+ result = get_request [oid]
24
25
  @session.close
25
- result
26
+ result.first
26
27
  end
27
28
 
28
29
  private
29
30
 
30
- def send_oid oid
31
- pdu = Snmpjr::Pdu.new.create oid
31
+ def get_request oids
32
+ pdu = Snmpjr::Pdu.new.create oids
32
33
  @session.send(pdu, @target)
33
34
  end
34
35
 
data/lib/snmpjr/pdu.rb CHANGED
@@ -1,14 +1,19 @@
1
+ require 'snmpjr/wrappers/smi'
2
+ require 'snmpjr/wrappers/snmp4j'
3
+
1
4
  class Snmpjr
2
5
  class Pdu
3
6
  module Constants
4
7
  GET = -96
5
8
  end
6
9
 
7
- def create oid
10
+ def create oids
8
11
  pdu = Snmpjr::Wrappers::PDU.new
9
- oid = Snmpjr::Wrappers::SMI::OID.new oid
10
- variable_binding = Snmpjr::Wrappers::SMI::VariableBinding.new oid
11
- pdu.add variable_binding
12
+ oids.map {|oid|
13
+ oid = Snmpjr::Wrappers::SMI::OID.new oid
14
+ variable_binding = Snmpjr::Wrappers::SMI::VariableBinding.new oid
15
+ pdu.add variable_binding
16
+ }
12
17
  pdu.type = Snmpjr::Pdu::Constants::GET
13
18
  pdu
14
19
  end
@@ -1,5 +1,6 @@
1
1
  require 'snmpjr/wrappers/transport'
2
2
  require 'snmpjr/response'
3
+ require 'snmpjr/target_timeout_error'
3
4
 
4
5
  class Snmpjr
5
6
  class Session
@@ -15,18 +16,31 @@ class Snmpjr
15
16
  def send pdu, target
16
17
  begin
17
18
  result = @snmp.send(pdu, target)
18
- if result.response.nil?
19
- Snmpjr::Response.new(:error => 'Request timed out')
20
- else
21
- Snmpjr::Response.new(:value => result.response.variable_bindings.first.variable.to_s)
22
- end
23
19
  rescue Exception => error
24
- Snmpjr::Response.new(:error => error.to_s)
20
+ raise RuntimeError.new(error)
21
+ end
22
+ if result.response.nil?
23
+ raise Snmpjr::TargetTimeoutError.new('Request timed out')
24
+ else
25
+ result.response.variable_bindings.map{|vb|
26
+ construct_response(vb)
27
+ }
25
28
  end
26
29
  end
27
30
 
28
31
  def close
29
32
  @snmp.close
30
33
  end
34
+
35
+ private
36
+
37
+ def construct_response variable_binding
38
+ if variable_binding.is_exception
39
+ Snmpjr::Response.new(:error => variable_binding.variable.to_s)
40
+ else
41
+ Snmpjr::Response.new(:value => variable_binding.variable.to_s)
42
+ end
43
+ end
44
+
31
45
  end
32
46
  end
data/lib/snmpjr/target.rb CHANGED
@@ -8,7 +8,7 @@ class Snmpjr
8
8
  target.community = Snmpjr::Wrappers::SMI::OctetString.new(options.fetch(:community))
9
9
  target.address = Snmpjr::Wrappers::SMI::GenericAddress.parse("udp:#{options.fetch(:host)}/#{options.fetch(:port)}")
10
10
  target.version = 1
11
- target.timeout = 5000
11
+ target.timeout = options.fetch(:timeout)
12
12
  target
13
13
  end
14
14
  end
@@ -0,0 +1,5 @@
1
+ class Snmpjr
2
+ class TargetTimeoutError < StandardError
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  class Snmpjr
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/snmpjr.rb CHANGED
@@ -8,11 +8,13 @@ class Snmpjr
8
8
  @host = options.fetch(:host)
9
9
  @port = options.fetch(:port) || 161
10
10
  @community = options.fetch(:community)
11
+ @timeout = options[:timeout] || 5000
12
+ @max_oids_per_request = options[:max_oids_per_request] || 30
11
13
  end
12
14
 
13
15
  def get oids
14
- target = Snmpjr::Target.new.create(:host => @host, :port => @port, :community => @community)
15
- getter = Snmpjr::Getter.new(target)
16
+ target = Snmpjr::Target.new.create(:host => @host, :port => @port, :community => @community, :timeout => @timeout)
17
+ getter = Snmpjr::Getter.new(:target => target, :max_oids_per_request => @max_oids_per_request)
16
18
 
17
19
  case oids.class.to_s
18
20
  when 'String'
@@ -1,5 +1,6 @@
1
1
  require 'snmpjr'
2
2
  require 'snmpjr/response'
3
+ require 'snmpjr/target_timeout_error'
3
4
 
4
5
  describe "snmpjr" do
5
6
 
@@ -19,20 +20,22 @@ describe "snmpjr" do
19
20
 
20
21
  context "when an invalid oid is requested" do
21
22
 
22
- let(:expected) { [Snmpjr::Response.new(:error => 'Invalid first sub-identifier (must be 0, 1, or 2)'),
23
+ let(:expected) { [Snmpjr::Response.new(:error => 'noSuchInstance'),
23
24
  Snmpjr::Response.new(:value => 'zeus.snmplabs.com')] }
24
25
 
25
26
  it "returns an error" do
26
- expect(subject.get ['6.5.4.3.2.1', '1.3.6.1.2.1.1.5.0']).to eq expected
27
+ expect(subject.get ['1.3.6.1.2.1.1.5', '1.3.6.1.2.1.1.5.0']).to eq expected
27
28
  end
28
29
  end
29
30
  end
30
31
 
31
32
  context 'when the host is unreachable' do
32
- subject { Snmpjr.new(:host => 'example.com', :port => 161, :community => 'public') }
33
+ subject { Snmpjr.new(:host => 'example.com', :port => 161, :community => 'public', :timeout => 50) }
33
34
 
34
35
  it "the request times out after 5 seconds" do
35
- expect(subject.get '1.3.6.1.2.1.1.1.0').to eq Snmpjr::Response.new(:error => 'Request timed out')
36
+ expect{
37
+ subject.get '1.3.6.1.2.1.1.1.0'
38
+ }.to raise_error(Snmpjr::TargetTimeoutError)
36
39
  end
37
40
  end
38
41
  end
@@ -4,25 +4,28 @@ describe Snmpjr::Getter do
4
4
  let(:target) { double :target }
5
5
  let(:session) { double Snmpjr::Session }
6
6
  let(:pdu) { double Snmpjr::Pdu }
7
- let(:created_pdu_1) { double :created_pdu_1 }
7
+ let(:created_pdu_single) { double :created_pdu_single }
8
8
 
9
- subject { described_class.new(target) }
9
+ subject { described_class.new(:target => target, :max_oids_per_request => 1) }
10
10
 
11
11
  before do
12
12
  allow(Snmpjr::Pdu).to receive(:new).and_return pdu
13
- allow(pdu).to receive(:create).with('1.2.3.4.5.6').and_return created_pdu_1
13
+ allow(pdu).to receive(:create).with(['1.2.3.4.5.6']).and_return created_pdu_single
14
14
  allow(Snmpjr::Session).to receive(:new).and_return session
15
15
  allow(session).to receive(:start)
16
- allow(session).to receive(:send).with(created_pdu_1, target).and_return 'Foo'
16
+ allow(session).to receive(:send).with(created_pdu_single, target).and_return ['Foo']
17
17
  allow(session).to receive(:close)
18
18
  end
19
19
 
20
20
  describe "#get_multiple" do
21
- let(:created_pdu_2) { double :created_pdu_2 }
21
+ let(:created_pdu_multiple_1) { double :created_pdu_multiple_1 }
22
+ let(:created_pdu_multiple_2) { double :created_pdu_multiple_2 }
22
23
 
23
24
  before do
24
- allow(pdu).to receive(:create).with('6.5.4.3.2.1').and_return created_pdu_2
25
- allow(session).to receive(:send).with(created_pdu_2, target).and_return 'Bar'
25
+ allow(pdu).to receive(:create).with(['1.2.3.4.5.6']).and_return created_pdu_multiple_1
26
+ allow(pdu).to receive(:create).with(['6.5.4.3.2.1']).and_return created_pdu_multiple_2
27
+ allow(session).to receive(:send).with(created_pdu_multiple_1, target).and_return ['Foo']
28
+ allow(session).to receive(:send).with(created_pdu_multiple_2, target).and_return ['Bar']
26
29
  end
27
30
 
28
31
  it 'starts an snmp session' do
@@ -32,8 +35,8 @@ describe Snmpjr::Getter do
32
35
 
33
36
  it 'performs multiple gets for each oid' do
34
37
  expect(subject.get_multiple ['1.2.3.4.5.6', '6.5.4.3.2.1']).to eq(['Foo', 'Bar'])
35
- expect(session).to have_received(:send).with(created_pdu_1, target)
36
- expect(session).to have_received(:send).with(created_pdu_2, target)
38
+ expect(session).to have_received(:send).with(created_pdu_multiple_1, target)
39
+ expect(session).to have_received(:send).with(created_pdu_multiple_2, target)
37
40
  end
38
41
 
39
42
  it 'closes the snmp session' do
@@ -50,7 +53,7 @@ describe Snmpjr::Getter do
50
53
 
51
54
  it 'performs a synchronous get' do
52
55
  expect(subject.get '1.2.3.4.5.6').to eq 'Foo'
53
- expect(session).to have_received(:send).with(created_pdu_1, target)
56
+ expect(session).to have_received(:send).with(created_pdu_single, target)
54
57
  end
55
58
 
56
59
  it 'closes the snmp session' do
@@ -4,26 +4,29 @@ describe Snmpjr::Pdu do
4
4
  describe "#create" do
5
5
 
6
6
  let(:pdu) { double Snmpjr::Wrappers::PDU }
7
- let(:oid) { double Snmpjr::Wrappers::SMI::OID }
8
- let(:variable_binding) { double Snmpjr::Wrappers::SMI::VariableBinding }
7
+ let(:oid_1) { double Snmpjr::Wrappers::SMI::OID }
8
+ let(:oid_2) { double Snmpjr::Wrappers::SMI::OID }
9
+ let(:variable_binding_1) { double Snmpjr::Wrappers::SMI::VariableBinding }
10
+ let(:variable_binding_2) { double Snmpjr::Wrappers::SMI::VariableBinding }
9
11
 
10
12
  before do
11
13
  allow(Snmpjr::Wrappers::PDU).to receive(:new).and_return pdu
12
- allow(Snmpjr::Wrappers::SMI::OID).to receive(:new).and_return oid
13
- allow(Snmpjr::Wrappers::SMI::VariableBinding).to receive(:new).and_return variable_binding
14
+ allow(Snmpjr::Wrappers::SMI::OID).to receive(:new).with('1.2.3.4').and_return oid_1
15
+ allow(Snmpjr::Wrappers::SMI::OID).to receive(:new).with('5.6.7.8').and_return oid_2
16
+ allow(Snmpjr::Wrappers::SMI::VariableBinding).to receive(:new).with(oid_1).and_return variable_binding_1
17
+ allow(Snmpjr::Wrappers::SMI::VariableBinding).to receive(:new).with(oid_2).and_return variable_binding_2
14
18
  allow(pdu).to receive(:type=)
15
19
  allow(pdu).to receive(:add)
16
20
  end
17
21
  it "creates a GET Pdu" do
18
22
  expect(pdu).to receive(:type=).with(Snmpjr::Pdu::Constants::GET)
19
- subject.create '1.2.3.4'
23
+ subject.create ['1.2.3.4']
20
24
  end
21
25
 
22
26
  it "adds an SMI variable binding containing an oid to the pdu" do
23
- expect(Snmpjr::Wrappers::SMI::OID).to receive(:new).with('1.2.3.4')
24
- expect(Snmpjr::Wrappers::SMI::VariableBinding).to receive(:new).with(oid)
25
- expect(pdu).to receive(:add).with variable_binding
26
- subject.create '1.2.3.4'
27
+ expect(pdu).to receive(:add).with variable_binding_1
28
+ expect(pdu).to receive(:add).with variable_binding_2
29
+ subject.create ['1.2.3.4', '5.6.7.8']
27
30
  end
28
31
  end
29
32
  end
@@ -28,13 +28,20 @@ describe Snmpjr::Session do
28
28
  end
29
29
 
30
30
  describe "#send" do
31
- let(:result) { double :result }
31
+ let(:vb1) { double :vb1 }
32
+ let(:vb2) { double :vb2 }
33
+ let(:results) { [vb1, vb2] }
34
+ let(:response) { double :response }
32
35
  let(:pdu) { double :pdu }
33
36
  let(:target) { double :target }
34
37
 
35
38
  before do
36
- allow(snmp_session).to receive(:send).and_return result
37
- allow(result).to receive_message_chain('response.variable_bindings.first.variable.to_s')
39
+ allow(snmp_session).to receive(:send).and_return response
40
+ allow(vb1).to receive_message_chain('variable.to_s')
41
+ allow(vb1).to receive(:is_exception)
42
+ allow(vb2).to receive_message_chain('variable.to_s')
43
+ allow(vb2).to receive(:is_exception)
44
+ allow(response).to receive_message_chain('response.variable_bindings').and_return(results)
38
45
  end
39
46
 
40
47
  it "sends the pdu to the target" do
@@ -44,23 +51,25 @@ describe Snmpjr::Session do
44
51
 
45
52
  context 'the requests times out' do
46
53
  before do
47
- allow(result).to receive(:response).and_return nil
54
+ allow(response).to receive(:response).and_return nil
48
55
  end
49
56
 
50
- let(:expected_response) { Snmpjr::Response.new(:error => 'Request timed out') }
51
- it 'returns a request timeout' do
52
- expect(subject.send(pdu, target)).to eq expected_response
57
+ it 'raises a timeout error' do
58
+ expect {
59
+ subject.send(pdu, target)
60
+ }.to raise_error(Snmpjr::TargetTimeoutError)
53
61
  end
54
62
  end
55
63
 
56
64
  context 'an exception is raised' do
57
65
  before do
58
- allow(snmp_session).to receive(:send).and_raise(RuntimeError.new('Some error'))
66
+ allow(snmp_session).to receive(:send).and_raise(Exception)
59
67
  end
60
68
 
61
- let(:expected_response) { Snmpjr::Response.new(:error => 'Some error') }
62
- it 'returns the error without blowing up' do
63
- expect(subject.send(pdu, target)).to eq expected_response
69
+ it 'catches it and raises a ruby runtime error' do
70
+ expect{
71
+ subject.send(pdu, target)
72
+ }.to raise_error(RuntimeError)
64
73
  end
65
74
  end
66
75
  end
@@ -3,7 +3,7 @@ require production_code
3
3
  describe Snmpjr::Target do
4
4
 
5
5
  describe "#create" do
6
- let(:options) { { :host => '127.0.0.1', :port => 161, :community => 'some_community' } }
6
+ let(:options) { { :host => '127.0.0.1', :port => 161, :community => 'some_community', :timeout => 50 } }
7
7
 
8
8
  it "creates an octet string for the community string" do
9
9
  expect(Snmpjr::Wrappers::SMI::OctetString).to receive(:new).with(options[:community])
@@ -25,9 +25,9 @@ describe Snmpjr::Target do
25
25
  allow(community_target).to receive(:address=)
26
26
  end
27
27
 
28
- it "sets the snmp version to v2c and the timeout to 5000ms" do
28
+ it "sets the snmp version to v2c and the timeout to 50ms" do
29
29
  expect(community_target).to receive(:version=).with(1)
30
- expect(community_target).to receive(:timeout=).with(5000)
30
+ expect(community_target).to receive(:timeout=).with(50)
31
31
  subject.create(options)
32
32
  end
33
33
 
data/spec/snmpjr_spec.rb CHANGED
@@ -11,12 +11,12 @@ describe Snmpjr do
11
11
  before do
12
12
  allow(Snmpjr::Target).to receive(:new).and_return target
13
13
  allow(target).to receive(:create).with(agent_details).and_return community_target
14
- allow(Snmpjr::Getter).to receive(:new).with(community_target).and_return getter
14
+ allow(Snmpjr::Getter).to receive(:new).with(:target => community_target, :max_oids_per_request => 20).and_return getter
15
15
  end
16
16
 
17
- let(:agent_details) { { :host => '127.0.0.1', :port => 161, :community => 'some_community' } }
17
+ let(:agent_details) { { :host => '127.0.0.1', :port => 161, :community => 'some_community', :timeout => 50 } }
18
18
 
19
- subject { described_class.new(agent_details) }
19
+ subject { described_class.new(agent_details.merge({:max_oids_per_request => 20})) }
20
20
 
21
21
  context 'when passed a single oid' do
22
22
  it 'performs a synchronous get' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snmpjr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: java
6
6
  authors:
7
7
  - Zen Kyprianou
@@ -35,6 +35,7 @@ files:
35
35
  - lib/snmpjr/response.rb
36
36
  - lib/snmpjr/session.rb
37
37
  - lib/snmpjr/target.rb
38
+ - lib/snmpjr/target_timeout_error.rb
38
39
  - lib/snmpjr/version.rb
39
40
  - lib/snmpjr/wrappers/smi.rb
40
41
  - lib/snmpjr/wrappers/snmp4j-2.3.1.jar