snmpjr 0.1.0-java → 0.1.2-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/history.rdoc +4 -0
- data/lib/snmpjr/getter.rb +36 -0
- data/lib/snmpjr/session.rb +18 -9
- data/lib/snmpjr/version.rb +1 -1
- data/lib/snmpjr.rb +4 -13
- data/spec/integration/snmpjr_spec.rb +27 -6
- data/spec/snmpjr/getter_spec.rb +61 -0
- data/spec/snmpjr/session_spec.rb +52 -17
- data/spec/snmpjr_spec.rb +5 -17
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDg0ZjZmMzlhOTM4ZjA5Zjk3NzVhOTljN2U3MTI1M2RkZTBkYjNjMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzhkMWE4OTk2Nzk5MDg3YWJiOTk2ZDY2OTUzMTgwYzVlOWVhYjNjMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MmQ2ZGJmMzVlODRhNTJkZmIwN2VkMGIwYjA5MGE3Mzc0Nzc2OWRiOGVmOWYx
|
10
|
+
OTYwNjU4ZmMwMjE1YzJkYjQzNWRkMjU5ZTljNTc1ODNkYTkzOTA3YTBmOTA3
|
11
|
+
OTIyZDEzYTNkYTJmM2UwZTdlOTNmZWYxMTRhZDViZmE5NDUwZTc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Mjk1Y2VlZjg4NWZmNWFmZDM4MGNkZWY2NDNkZGQ0OWM4MjEwYjdjMzQ3YTY4
|
14
|
+
YjJlNjE4NzA5ZjQyYmJlNjRhNzQxMmFmYmMxY2YxMDFmMjA3MmI1NmE2NDAx
|
15
|
+
MzY4MTg0NGNjY2QwMDA1NzM1MWQyOTlkZTYwYjM0OTI3MDQ3MTg=
|
data/history.rdoc
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "snmpjr/pdu"
|
2
|
+
require "snmpjr/session"
|
3
|
+
|
4
|
+
class Snmpjr
|
5
|
+
class Getter
|
6
|
+
|
7
|
+
def initialize target
|
8
|
+
@target = target
|
9
|
+
@session = Snmpjr::Session.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_multiple oids
|
13
|
+
@session.start
|
14
|
+
result = oids.map {|oid|
|
15
|
+
send_oid oid
|
16
|
+
}
|
17
|
+
@session.close
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
def get oid
|
22
|
+
@session.start
|
23
|
+
result = send_oid oid
|
24
|
+
@session.close
|
25
|
+
result
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def send_oid oid
|
31
|
+
pdu = Snmpjr::Pdu.new.create oid
|
32
|
+
@session.send(pdu, @target)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/snmpjr/session.rb
CHANGED
@@ -3,20 +3,29 @@ require 'snmpjr/wrappers/transport'
|
|
3
3
|
class Snmpjr
|
4
4
|
class Session
|
5
5
|
|
6
|
+
def initialize
|
7
|
+
@snmp = Snmpjr::Wrappers::Snmp.new(Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping.new)
|
8
|
+
end
|
6
9
|
|
7
|
-
def
|
8
|
-
snmp
|
9
|
-
|
10
|
+
def start
|
11
|
+
@snmp.listen
|
12
|
+
end
|
10
13
|
|
14
|
+
def send pdu, target
|
11
15
|
begin
|
12
|
-
result = snmp.send(pdu, target)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
result = @snmp.send(pdu, target)
|
17
|
+
if result.response.nil?
|
18
|
+
"Request timed out"
|
19
|
+
else
|
20
|
+
result.response.variable_bindings.first.variable.to_s
|
21
|
+
end
|
22
|
+
rescue Exception => e
|
23
|
+
"Error: #{e.to_s}"
|
17
24
|
end
|
25
|
+
end
|
18
26
|
|
19
|
-
|
27
|
+
def close
|
28
|
+
@snmp.close
|
20
29
|
end
|
21
30
|
end
|
22
31
|
end
|
data/lib/snmpjr/version.rb
CHANGED
data/lib/snmpjr.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require "snmpjr/version"
|
2
|
-
require "snmpjr/
|
3
|
-
require "snmpjr/session"
|
2
|
+
require "snmpjr/getter"
|
4
3
|
require "snmpjr/target"
|
5
4
|
|
6
5
|
class Snmpjr
|
@@ -13,24 +12,16 @@ class Snmpjr
|
|
13
12
|
|
14
13
|
def get oids
|
15
14
|
target = Snmpjr::Target.new.create(:host => @host, :port => @port, :community => @community)
|
15
|
+
getter = Snmpjr::Getter.new(target)
|
16
16
|
|
17
17
|
case oids.class.to_s
|
18
18
|
when 'String'
|
19
|
-
|
19
|
+
getter.get oids
|
20
20
|
when 'Array'
|
21
|
-
oids
|
22
|
-
get_oid(oid, target)
|
23
|
-
}
|
21
|
+
getter.get_multiple oids
|
24
22
|
else
|
25
23
|
raise ArgumentError.new 'You can request a single Oid using a String, or multiple using an Array'
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
|
-
private
|
30
|
-
|
31
|
-
def get_oid oid, target
|
32
|
-
pdu = Snmpjr::Pdu.new.create oid
|
33
|
-
Snmpjr::Session.new.send(pdu, target)
|
34
|
-
end
|
35
|
-
|
36
27
|
end
|
@@ -2,15 +2,36 @@ require 'snmpjr'
|
|
2
2
|
|
3
3
|
describe "snmpjr" do
|
4
4
|
|
5
|
-
subject { Snmpjr.new(:host => 'demo.snmplabs.com', :port => 161, :community => 'public') }
|
6
5
|
describe 'GET' do
|
7
|
-
|
8
|
-
|
6
|
+
context 'when the host is reachable' do
|
7
|
+
subject { Snmpjr.new(:host => 'demo.snmplabs.com', :port => 161, :community => 'public') }
|
8
|
+
|
9
|
+
it "can perform a simple synchronous get request on an snmp agent" do
|
10
|
+
expect(subject.get '1.3.6.1.2.1.1.1.0').to eq 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:expected) { ['SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m', 'zeus.snmplabs.com'] }
|
14
|
+
it "can perform a series of gets if passed an array of oids" do
|
15
|
+
expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0']).to eq expected
|
16
|
+
end
|
17
|
+
|
18
|
+
context "when an invalid oid is requested" do
|
19
|
+
|
20
|
+
let(:expected) { ['Error: Invalid first sub-identifier (must be 0, 1, or 2)', 'zeus.snmplabs.com'] }
|
21
|
+
it "returns an error" do
|
22
|
+
expect(subject.get ['6.5.4.3.2.1', '1.3.6.1.2.1.1.5.0']).to eq expected
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
9
26
|
end
|
10
27
|
|
11
|
-
|
12
|
-
|
13
|
-
|
28
|
+
context 'when the host is unreachable' do
|
29
|
+
subject { Snmpjr.new(:host => 'example.com', :port => 161, :community => 'public') }
|
30
|
+
|
31
|
+
it "the request times out after 5 seconds" do
|
32
|
+
expect(subject.get '1.3.6.1.2.1.1.1.0').to eq 'Request timed out'
|
33
|
+
end
|
14
34
|
end
|
35
|
+
|
15
36
|
end
|
16
37
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require production_code
|
2
|
+
|
3
|
+
describe Snmpjr::Getter do
|
4
|
+
let(:target) { double :target }
|
5
|
+
let(:session) { double Snmpjr::Session }
|
6
|
+
let(:pdu) { double Snmpjr::Pdu }
|
7
|
+
let(:created_pdu_1) { double :created_pdu_1 }
|
8
|
+
|
9
|
+
subject { described_class.new(target) }
|
10
|
+
|
11
|
+
before do
|
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
|
14
|
+
allow(Snmpjr::Session).to receive(:new).and_return session
|
15
|
+
allow(session).to receive(:start)
|
16
|
+
allow(session).to receive(:send).with(created_pdu_1, target).and_return 'Foo'
|
17
|
+
allow(session).to receive(:close)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#get_multiple" do
|
21
|
+
let(:created_pdu_2) { double :created_pdu_2 }
|
22
|
+
|
23
|
+
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'
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'starts an snmp session' do
|
29
|
+
subject.get_multiple ['1.2.3.4.5.6', '6.5.4.3.2.1']
|
30
|
+
expect(session).to have_received(:start)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'performs multiple gets for each oid' do
|
34
|
+
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)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'closes the snmp session' do
|
40
|
+
subject.get_multiple ['1.2.3.4.5.6', '6.5.4.3.2.1']
|
41
|
+
expect(session).to have_received(:close)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#get" do
|
46
|
+
it 'starts an snmp session' do
|
47
|
+
subject.get '1.2.3.4.5.6'
|
48
|
+
expect(session).to have_received(:start)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'performs a synchronous get' do
|
52
|
+
expect(subject.get '1.2.3.4.5.6').to eq 'Foo'
|
53
|
+
expect(session).to have_received(:send).with(created_pdu_1, target)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'closes the snmp session' do
|
57
|
+
subject.get '1.2.3.4.5.6'
|
58
|
+
expect(session).to have_received(:close)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/snmpjr/session_spec.rb
CHANGED
@@ -1,26 +1,40 @@
|
|
1
1
|
require production_code
|
2
2
|
|
3
3
|
describe Snmpjr::Session do
|
4
|
-
describe "#send" do
|
5
|
-
let(:transport_mapping) { double Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping }
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
let(:transport_mapping) { double Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping }
|
6
|
+
let(:snmp_session) { double Snmpjr::Wrappers::Snmp }
|
7
|
+
|
8
|
+
before do
|
9
|
+
allow(Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping).to receive(:new).and_return transport_mapping
|
10
|
+
allow(Snmpjr::Wrappers::Snmp).to receive(:new).and_return snmp_session
|
11
|
+
allow(snmp_session).to receive(:listen)
|
12
|
+
allow(snmp_session).to receive(:send)
|
13
|
+
allow(snmp_session).to receive(:close)
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#start" do
|
17
|
+
|
18
|
+
it "opens a new SNMP4J session with Udp transport mapping" do
|
19
|
+
subject.start
|
20
|
+
expect(Snmpjr::Wrappers::Snmp).to have_received(:new).with(transport_mapping)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "calls the listen method on the Snmp session" do
|
24
|
+
subject.start
|
25
|
+
expect(snmp_session).to have_received(:listen)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
10
29
|
|
11
|
-
|
30
|
+
describe "#send" do
|
31
|
+
let(:result) { double :result }
|
32
|
+
let(:pdu) { double :pdu }
|
33
|
+
let(:target) { double :target }
|
12
34
|
|
13
35
|
before do
|
14
|
-
allow(Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping).to receive(:new).and_return transport_mapping
|
15
|
-
allow(Snmpjr::Wrappers::Snmp).to receive(:new).and_return snmp_session
|
16
36
|
allow(snmp_session).to receive(:send).and_return result
|
17
37
|
allow(result).to receive_message_chain('response.variable_bindings.first.variable.to_s')
|
18
|
-
allow(snmp_session).to receive(:listen)
|
19
|
-
allow(snmp_session).to receive(:close)
|
20
|
-
end
|
21
|
-
it "opens a new SNMP4J session with Udp transport mapping" do
|
22
|
-
expect(Snmpjr::Wrappers::Snmp).to receive(:new).with(transport_mapping)
|
23
|
-
subject.send(pdu, target)
|
24
38
|
end
|
25
39
|
|
26
40
|
it "sends the pdu to the target" do
|
@@ -28,10 +42,31 @@ describe Snmpjr::Session do
|
|
28
42
|
subject.send(pdu, target)
|
29
43
|
end
|
30
44
|
|
31
|
-
|
32
|
-
|
33
|
-
|
45
|
+
context 'the requests times out' do
|
46
|
+
before do
|
47
|
+
allow(result).to receive(:response).and_return nil
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'returns a request timeout' do
|
51
|
+
expect(subject.send(pdu, target)).to eq "Request timed out"
|
52
|
+
end
|
34
53
|
end
|
35
54
|
|
55
|
+
context 'an exception is raised' do
|
56
|
+
before do
|
57
|
+
allow(snmp_session).to receive(:send).and_raise(RuntimeError.new('Some error'))
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'returns the error without blowing up' do
|
61
|
+
expect(subject.send(pdu, target)).to eq "Error: Some error"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#close" do
|
67
|
+
it "closes the snmp session" do
|
68
|
+
subject.close
|
69
|
+
expect(snmp_session).to have_received(:close)
|
70
|
+
end
|
36
71
|
end
|
37
72
|
end
|
data/spec/snmpjr_spec.rb
CHANGED
@@ -6,17 +6,12 @@ describe Snmpjr do
|
|
6
6
|
|
7
7
|
let(:target) { double Snmpjr::Target }
|
8
8
|
let(:community_target) { double :community_target }
|
9
|
-
let(:
|
10
|
-
let(:pdu) { double Snmpjr::Pdu }
|
11
|
-
let(:created_pdu_1) { double :created_pdu_1 }
|
9
|
+
let(:getter) { double Snmpjr::Getter }
|
12
10
|
|
13
11
|
before do
|
14
|
-
allow(Snmpjr::Pdu).to receive(:new).and_return pdu
|
15
|
-
allow(pdu).to receive(:create).with('1.2.3.4.5.6').and_return created_pdu_1
|
16
|
-
allow(Snmpjr::Session).to receive(:new).and_return session
|
17
|
-
allow(session).to receive(:send)
|
18
12
|
allow(Snmpjr::Target).to receive(:new).and_return target
|
19
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
|
20
15
|
end
|
21
16
|
|
22
17
|
let(:agent_details) { { :host => '127.0.0.1', :port => 161, :community => 'some_community' } }
|
@@ -25,22 +20,15 @@ describe Snmpjr do
|
|
25
20
|
|
26
21
|
context 'when passed a single oid' do
|
27
22
|
it 'performs a synchronous get' do
|
23
|
+
expect(getter).to receive(:get).with('1.2.3.4.5.6')
|
28
24
|
subject.get '1.2.3.4.5.6'
|
29
|
-
expect(session).to have_received(:send).with(created_pdu_1, community_target)
|
30
25
|
end
|
31
26
|
end
|
32
27
|
|
33
28
|
context 'when passed multiple oids' do
|
34
|
-
|
35
|
-
|
36
|
-
before do
|
37
|
-
allow(pdu).to receive(:create).with('6.5.4.3.2.1').and_return created_pdu_2
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'performs multiple gets for each oid' do
|
29
|
+
it 'performs a get multiple using the getter' do
|
30
|
+
expect(getter).to receive(:get_multiple).with(['1.2.3.4.5.6', '6.5.4.3.2.1'])
|
41
31
|
subject.get ['1.2.3.4.5.6', '6.5.4.3.2.1']
|
42
|
-
expect(session).to have_received(:send).with(created_pdu_1, community_target)
|
43
|
-
expect(session).to have_received(:send).with(created_pdu_2, community_target)
|
44
32
|
end
|
45
33
|
end
|
46
34
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snmpjr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Zen Kyprianou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ! 'Snmpjr aims to provide a clean and simple interface to use SNMP in
|
14
14
|
your ruby code. It will wrap the popular SNMP4J library in Java.
|
@@ -30,6 +30,7 @@ files:
|
|
30
30
|
- Rakefile
|
31
31
|
- history.rdoc
|
32
32
|
- lib/snmpjr.rb
|
33
|
+
- lib/snmpjr/getter.rb
|
33
34
|
- lib/snmpjr/pdu.rb
|
34
35
|
- lib/snmpjr/session.rb
|
35
36
|
- lib/snmpjr/target.rb
|
@@ -40,6 +41,7 @@ files:
|
|
40
41
|
- lib/snmpjr/wrappers/transport.rb
|
41
42
|
- snmpjr.gemspec
|
42
43
|
- spec/integration/snmpjr_spec.rb
|
44
|
+
- spec/snmpjr/getter_spec.rb
|
43
45
|
- spec/snmpjr/pdu_spec.rb
|
44
46
|
- spec/snmpjr/session_spec.rb
|
45
47
|
- spec/snmpjr/target_spec.rb
|
@@ -71,6 +73,7 @@ specification_version: 4
|
|
71
73
|
summary: Simple SNMP interface for JRuby
|
72
74
|
test_files:
|
73
75
|
- spec/integration/snmpjr_spec.rb
|
76
|
+
- spec/snmpjr/getter_spec.rb
|
74
77
|
- spec/snmpjr/pdu_spec.rb
|
75
78
|
- spec/snmpjr/session_spec.rb
|
76
79
|
- spec/snmpjr/target_spec.rb
|