snmpjr 0.2.2-java → 0.3.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +8 -8
  2. data/.semver +2 -2
  3. data/README.md +33 -10
  4. data/history.rdoc +2 -0
  5. data/lib/snmpjr/configuration.rb +13 -0
  6. data/lib/snmpjr/configuration_v2c.rb +22 -0
  7. data/lib/snmpjr/configuration_v3.rb +51 -0
  8. data/lib/snmpjr/getter.rb +18 -19
  9. data/lib/snmpjr/{pdu.rb → pdu_v2c.rb} +8 -5
  10. data/lib/snmpjr/pdu_v3.rb +14 -0
  11. data/lib/snmpjr/{session.rb → session_v2c.rb} +4 -2
  12. data/lib/snmpjr/session_v3.rb +63 -0
  13. data/lib/snmpjr/target_v2c.rb +18 -0
  14. data/lib/snmpjr/target_v3.rb +18 -0
  15. data/lib/snmpjr/version.rb +4 -1
  16. data/lib/snmpjr/walker.rb +3 -2
  17. data/lib/snmpjr/wrappers/mp.rb +9 -0
  18. data/lib/snmpjr/wrappers/security.rb +9 -0
  19. data/lib/snmpjr.rb +29 -31
  20. data/snmpjr.gemspec +1 -1
  21. data/spec/integration/{snmpjr_get_spec.rb → snmp_v2c/snmpjr_get_spec.rb} +18 -5
  22. data/spec/integration/{snmpjr_walk_spec.rb → snmp_v2c/snmpjr_walk_spec.rb} +20 -5
  23. data/spec/integration/snmp_v3/snmpjr_get_spec.rb +67 -0
  24. data/spec/integration/snmp_v3/snmpjr_walk_spec.rb +54 -0
  25. data/spec/snmpjr/configuration_v2c_spec.rb +30 -0
  26. data/spec/snmpjr/configuration_v3_spec.rb +90 -0
  27. data/spec/snmpjr/getter_spec.rb +24 -27
  28. data/spec/snmpjr/{pdu_spec.rb → pdu_v2c_spec.rb} +2 -2
  29. data/spec/snmpjr/pdu_v3_spec.rb +17 -0
  30. data/spec/snmpjr/{session_spec.rb → session_v2c_spec.rb} +1 -1
  31. data/spec/snmpjr/session_v3_spec.rb +22 -0
  32. data/spec/snmpjr/{target_spec.rb → target_v2c_spec.rb} +16 -7
  33. data/spec/snmpjr/target_v3_spec.rb +50 -0
  34. data/spec/snmpjr/walker_spec.rb +1 -2
  35. data/spec/snmpjr_spec.rb +68 -35
  36. metadata +37 -16
  37. data/lib/snmpjr/target.rb +0 -17
  38. data/lib/snmpjr/target_timeout_error.rb +0 -5
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmUwNWVjOGJkMDViODU2YjZiNGU1NWRjMTg0NmIwZDhjYzgyMmRiOQ==
4
+ MDY3NTgwYjc3ZTBlNDViODkyNzdiY2E4NzY5NWY3NjlhMjRhYzE0ZQ==
5
5
  data.tar.gz: !binary |-
6
- MzA2MDZlZmE0NjM1ZjIxMDRjYzMyZDQ0MmY1ZWMwYjhlMjQ3YjYxOA==
6
+ NDQzMjc5ZTcwYzY5ZmRjNTU2NzlkMzBiZGZiY2Q3ZGZkNjJiZGEyZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzFlMzlmMjNlY2I4MjlkNWYwN2QxMzZmNWU2NDk0OGUzZmJhZDczMTM3Zjg2
10
- NGMyMjY3NTJmMDlkNmRhNzM4ZTc3NTZiMTkyZmI0YjQ5OWUxZjg3OGQ1ODY2
11
- ZjIzNDhkZmZhNDEzYTZlZmIzNWMwMTBiNjI3NTE4NDI5Y2RmMzA=
9
+ MDc5YzIzMWJkZGRmODI1OTU4NjQwZTdmOWZmYzU5MzRjMTc2YWQxZjYwYzQ0
10
+ NWNlZDhlMDQ1MWU5ZDU0NTIyOWU5ZTA0ZGViYzVmNGIzYTU5YTVhMmFiNjli
11
+ NjU0MDdkNDRiMWM2NmE5YjQ1ODJiMmIzNjc5Mjc5MDg0NzhhYWQ=
12
12
  data.tar.gz: !binary |-
13
- MGFkYmI2NTk4ZTBiNjg4YTdiZjdkZTczMjQ5MjBhMjFjZjFlY2U2YTFlZjEx
14
- YmYzNDNlM2ZlYjcyOGFmOTZmMTBhN2NhZWE0OTMyZjZhMDQ3NDYxMjY2MTg1
15
- ZWM5ZjlhNjU0MjI2MDY2MmZlMDQxNmM4NTcyNzZkZGZjYzhhMWE=
13
+ OTdkZDdhZjU3NzJiNjRmNjRhYWNkYjVkMzM1Mzk2MmE3YmFiZjM2ZmRiMDNi
14
+ MTQzYTI5ZmVmZTZlZjJmYjkwOWJiOGM1NjJkN2ExNzhhNWQ5OTIzNDlhYTg0
15
+ NGM5ODNhY2QxMDczZDM4ZWE3MzYwMDhiMzlmMWEyODA4OGU1MTA=
data/.semver CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 2
4
- :patch: 2
3
+ :minor: 3
4
+ :patch: '0'
5
5
  :special: ''
6
6
  :metadata: ''
data/README.md CHANGED
@@ -3,14 +3,12 @@ Snmpjr
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/snmpjr.svg)](http://badge.fury.io/rb/snmpjr) [![Build Status](https://travis-ci.org/zenonas/snmpjr.svg?branch=master)](https://travis-ci.org/zenonas/snmpjr) [![Coverage Status](https://img.shields.io/coveralls/zenonas/snmpjr.svg)](https://coveralls.io/r/zenonas/snmpjr?branch=master) [![Code Climate](https://codeclimate.com/github/zenonas/snmpjr/badges/gpa.svg)](https://codeclimate.com/github/zenonas/snmpjr) [![Dependency Status](https://gemnasium.com/zenonas/snmpjr.svg)](https://gemnasium.com/zenonas/snmpjr) [![Inline docs](http://inch-ci.org/github/zenonas/snmpjr.svg?branch=master)](http://inch-ci.org/github/zenonas/snmpjr)
5
5
 
6
- Snmpjr aims to provide a clean and simple interface to use SNMP in your ruby code. It will wrap the popular SNMP4J library in Java.
7
-
8
- Please note the gem is still in early develpment. Do not use as of yet!
6
+ Snmpjr aims to provide a clean and simple interface to use SNMP in your ruby code. It wraps the popular SNMP4J library in Java.
9
7
 
10
8
  ## Features
11
9
 
12
- * Simple Synchronous SNMP V2 Get requests
13
- * Synchronous SNMP V2 Walk
10
+ * Synchronous SNMP Get and Walk for SNMPv2c and SNMPv3
11
+ * Simple API
14
12
 
15
13
  ## Requirements
16
14
 
@@ -36,9 +34,34 @@ Or install it yourself as:
36
34
  ## Usage
37
35
 
38
36
  ```ruby
39
- # Initialize Snmpjr with host, port and a community
40
- # Optional Params. (port, retries, timeout)
41
- snmp = Snmpjr.new(host: '127.0.0.1', port: 161, community: 'public')
37
+ # Initialize Snmpjr with the version of SNMP you want to work with
38
+ snmp = Snmpjr.new(Snmpjr::Version::V2C).configure do |config|
39
+ config.host = 'example.com'
40
+ config.community = 'public'
41
+ # Optional Parameters
42
+ config.port = 161 #(default)
43
+ config.retries = 0 #(default)
44
+ config.timeout = 5000 #(default)
45
+ config.max_oids_per_request = 20 #(default)
46
+ end
47
+
48
+ snmp = Snmpjr.new(Snmpjr::Version::V3).configure do |config|
49
+ config.host = 'example.com'
50
+ config.user = 'some user'
51
+ # Agent dependent options
52
+ config.authentication 'authentication_protocol*', 'passphrase'
53
+ config.privacy 'privacy_protocol**', 'passphrase'
54
+ config.context = '' #(default)
55
+ # Optional Parameters
56
+ config.port = 161 #(default)
57
+ config.retries = 0 #(default)
58
+ config.timeout = 5000 #(default)
59
+ config.max_oids_per_request = 20 #(default)
60
+ end
61
+
62
+ # There is no need to specify options that you wish to default
63
+ # * Authentication Protocols (SHA|MD5)
64
+ # ** Privacy Protocols (DES|3DES|AES128|AES192|AES256)
42
65
 
43
66
  # Call get on any single Oid
44
67
  snmp.get '1.3.6.1.2.1.1.1.0'
@@ -48,7 +71,7 @@ snmp.get '1.3.6.1.2.1.1.1.0'
48
71
  snmp.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.3.0']
49
72
  => [Snmpjr::Response.new(value: 'First result'), Snmpjr::Response.new(value: 'Second result')]
50
73
 
51
- # Response objects respond to error?
74
+ # Response objects respond to response.error? that returns true if an SNMP error occured eg. noSuchInstance
52
75
 
53
76
  # Call walk on an Oid
54
77
  snmp.walk '1.3.6.1.2.1.1.1'
@@ -57,7 +80,7 @@ snmp.walk '1.3.6.1.2.1.1.1'
57
80
 
58
81
  Snmpjr will catch and raise any exceptions that happen in Java land and raise them as RuntimeErrors while preserving the message.
59
82
 
60
- When you request an Array of Oids these will be pulled sequentially
83
+ When you request an Array of Oids these will be packaged in a single PDU up to a maximum(configurable).
61
84
 
62
85
  ## Contributing
63
86
 
data/history.rdoc CHANGED
@@ -1,3 +1,5 @@
1
+ == v0.3.0 (01 December 2014)
2
+
1
3
  == v0.2.2 (03 November 2014)
2
4
 
3
5
  * Add the Oid as part of the response object
@@ -0,0 +1,13 @@
1
+ class Snmpjr
2
+ class Configuration
3
+ attr_accessor :host, :port, :retries, :timeout, :max_oids_per_request
4
+
5
+ def initialize
6
+ @port = 161
7
+ @retries = 0
8
+ @timeout = 5000
9
+ @max_oids_per_request = 20
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ require 'snmpjr/configuration'
2
+ require 'snmpjr/pdu_v2c'
3
+ require 'snmpjr/target_v2c'
4
+ require 'snmpjr/session_v2c'
5
+
6
+ class Snmpjr
7
+ class ConfigurationV2C < Configuration
8
+ attr_accessor :community
9
+
10
+ def create_pdu
11
+ Snmpjr::PduV2C.new
12
+ end
13
+
14
+ def create_target
15
+ Snmpjr::TargetV2C.new.create self
16
+ end
17
+
18
+ def create_session
19
+ Snmpjr::SessionV2C.new
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ require 'snmpjr/configuration'
2
+ require 'snmpjr/target_v3'
3
+ require 'snmpjr/session_v3'
4
+ require 'snmpjr/pdu_v3'
5
+
6
+ class Snmpjr
7
+ class ConfigurationV3 < Configuration
8
+ module SecurityLevels
9
+ NoAuthNoPriv = 1
10
+ AuthNoPriv = 2
11
+ AuthPriv = 3
12
+ end
13
+
14
+ attr_accessor :context, :user
15
+ attr_reader :authentication_protocol, :authentication_key, :privacy_protocol, :privacy_key
16
+
17
+ def initialize
18
+ @context = ''
19
+ super
20
+ end
21
+
22
+ def security_level
23
+ return SecurityLevels::AuthPriv if authentication_protocol && privacy_protocol
24
+ return SecurityLevels::AuthNoPriv if authentication_protocol
25
+ SecurityLevels::NoAuthNoPriv
26
+ end
27
+
28
+ def authentication protocol, key
29
+ @authentication_protocol = protocol
30
+ @authentication_key = key
31
+ end
32
+
33
+ def privacy protocol, key
34
+ @privacy_protocol = protocol
35
+ @privacy_key = key
36
+ end
37
+
38
+ def create_target
39
+ Snmpjr::TargetV3.new.create self
40
+ end
41
+
42
+ def create_session
43
+ Snmpjr::SessionV3.new self
44
+ end
45
+
46
+ def create_pdu
47
+ Snmpjr::PduV3.new context
48
+ end
49
+
50
+ end
51
+ end
data/lib/snmpjr/getter.rb CHANGED
@@ -1,37 +1,36 @@
1
- require "snmpjr/pdu"
2
- require "snmpjr/session"
3
-
4
1
  class Snmpjr
5
2
  class Getter
6
3
 
7
4
  def initialize args = {}
8
5
  @target = args.fetch(:target)
9
- @max_oids_per_request = args[:max_oids_per_request] || 30
10
- @session = Snmpjr::Session.new
11
- end
12
-
13
- def get_multiple oids
14
- @session.start
15
- result = oids.each_slice(@max_oids_per_request).map{|partial_oids|
16
- get_request partial_oids
17
- }.flatten
18
- @session.close
19
- result
6
+ @max_oids_per_request = args.fetch(:config).max_oids_per_request
7
+ @session = args.fetch(:session)
8
+ @pdu = args.fetch(:pdu)
20
9
  end
21
10
 
22
- def get oid
11
+ def get oids
23
12
  @session.start
24
- result = get_request [oid]
25
- @session.close
26
- result.first
13
+ begin
14
+ results = oids.each_slice(@max_oids_per_request).map{|partial_oids|
15
+ get_request partial_oids
16
+ }.flatten
17
+ ensure
18
+ @session.close
19
+ end
20
+ extract_possible_single_result_from results
27
21
  end
28
22
 
29
23
  private
30
24
 
31
25
  def get_request oids
32
- pdu = Snmpjr::Pdu.new.create oids
26
+ pdu = @pdu.create oids
33
27
  @session.send(pdu, @target)
34
28
  end
35
29
 
30
+ def extract_possible_single_result_from results
31
+ return results.first if results.size.eql?(1)
32
+ results
33
+ end
34
+
36
35
  end
37
36
  end
@@ -2,20 +2,23 @@ require 'snmpjr/wrappers/smi'
2
2
  require 'snmpjr/wrappers/snmp4j'
3
3
 
4
4
  class Snmpjr
5
- class Pdu
5
+ class PduV2C
6
6
  module Constants
7
7
  GET = -96
8
8
  end
9
9
 
10
+ def initialize
11
+ @pdu = Snmpjr::Wrappers::PDU.new
12
+ end
13
+
10
14
  def create oids
11
- pdu = Snmpjr::Wrappers::PDU.new
12
15
  oids.map {|oid|
13
16
  oid = Snmpjr::Wrappers::SMI::OID.new oid
14
17
  variable_binding = Snmpjr::Wrappers::SMI::VariableBinding.new oid
15
- pdu.add variable_binding
18
+ @pdu.add variable_binding
16
19
  }
17
- pdu.type = Snmpjr::Pdu::Constants::GET
18
- pdu
20
+ @pdu.type = Snmpjr::PduV2C::Constants::GET
21
+ @pdu
19
22
  end
20
23
 
21
24
  end
@@ -0,0 +1,14 @@
1
+ require 'snmpjr/pdu_v2c'
2
+ require 'snmpjr/wrappers/snmp4j'
3
+ require 'snmpjr/wrappers/smi'
4
+
5
+ class Snmpjr
6
+ class PduV3 < Snmpjr::PduV2C
7
+
8
+ def initialize context = ''
9
+ @pdu = Snmpjr::Wrappers::ScopedPDU.new
10
+ @pdu.context_name = Snmpjr::Wrappers::SMI::OctetString.new(context)
11
+ end
12
+
13
+ end
14
+ end
@@ -1,9 +1,8 @@
1
1
  require 'snmpjr/wrappers/transport'
2
2
  require 'snmpjr/response'
3
- require 'snmpjr/target_timeout_error'
4
3
 
5
4
  class Snmpjr
6
- class Session
5
+ class SessionV2C
7
6
  attr_reader :snmp
8
7
 
9
8
  def initialize
@@ -44,4 +43,7 @@ class Snmpjr
44
43
  end
45
44
 
46
45
  end
46
+
47
+ class TargetTimeoutError < StandardError
48
+ end
47
49
  end
@@ -0,0 +1,63 @@
1
+ require 'snmpjr/session_v2c'
2
+ require 'snmpjr/wrappers/security'
3
+ require 'snmpjr/wrappers/smi'
4
+ require 'snmpjr/wrappers/mp'
5
+
6
+ class Snmpjr
7
+ class SessionV3 < Snmpjr::SessionV2C
8
+ AUTHENTICATION_PROTOCOLS = {
9
+ 'MD5' => Snmpjr::Wrappers::Security::AuthMD5::ID,
10
+ 'SHA' => Snmpjr::Wrappers::Security::AuthSHA::ID
11
+ }
12
+
13
+ PRIVACY_PROTOCOLS = {
14
+ '3DES' => Snmpjr::Wrappers::Security::Priv3DES::ID,
15
+ 'DES' => Snmpjr::Wrappers::Security::PrivDES::ID,
16
+ 'AES128' => Snmpjr::Wrappers::Security::PrivAES128::ID,
17
+ 'AES192' => Snmpjr::Wrappers::Security::PrivAES192::ID,
18
+ 'AES256' => Snmpjr::Wrappers::Security::PrivAES256::ID
19
+ }
20
+
21
+ def initialize configuration
22
+ @snmp = Snmpjr::Wrappers::Snmp.new(Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping.new)
23
+ @config = configuration
24
+ add_v3_security
25
+ end
26
+
27
+ private
28
+
29
+ def add_v3_security
30
+ create_and_attach_usm
31
+ @snmp.usm.add_user create_usm_user
32
+ end
33
+
34
+ def create_and_attach_usm
35
+ usm = Snmpjr::Wrappers::Security::USM.new(
36
+ Snmpjr::Wrappers::Security::SecurityProtocols.instance,
37
+ Snmpjr::Wrappers::SMI::OctetString.new(Snmpjr::Wrappers::MP::MPv3.create_local_engine_id),
38
+ 0
39
+ )
40
+ Snmpjr::Wrappers::Security::SecurityModels.instance.add_security_model usm
41
+ end
42
+
43
+ def create_usm_user
44
+ Snmpjr::Wrappers::Security::UsmUser.new(
45
+ Snmpjr::Wrappers::SMI::OctetString.new(@config.user),
46
+ AUTHENTICATION_PROTOCOLS[@config.authentication_protocol],
47
+ authentication_key,
48
+ PRIVACY_PROTOCOLS[@config.privacy_protocol],
49
+ privacy_key
50
+ )
51
+ end
52
+
53
+ def authentication_key
54
+ return @config.authentication_key if @config.authentication_key.nil?
55
+ Snmpjr::Wrappers::SMI::OctetString.new(@config.authentication_key)
56
+ end
57
+
58
+ def privacy_key
59
+ return @config.privacy_key if @config.privacy_key.nil?
60
+ Snmpjr::Wrappers::SMI::OctetString.new(@config.privacy_key)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,18 @@
1
+ require 'snmpjr/wrappers/smi'
2
+ require 'snmpjr/version'
3
+
4
+ class Snmpjr
5
+ class TargetV2C
6
+
7
+ def create configuration
8
+ target = Snmpjr::Wrappers::CommunityTarget.new
9
+ target.community = Snmpjr::Wrappers::SMI::OctetString.new(configuration.community)
10
+ target.address = Snmpjr::Wrappers::SMI::GenericAddress.parse("udp:#{configuration.host}/#{configuration.port}")
11
+ target.version = Snmpjr::Version::V2C
12
+ target.retries = configuration.retries
13
+ target.timeout = configuration.timeout
14
+ target
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'snmpjr/wrappers/snmp4j'
2
+ require 'snmpjr/wrappers/smi'
3
+ require 'snmpjr/version'
4
+
5
+ class Snmpjr
6
+ class TargetV3
7
+ def create configuration
8
+ target = Snmpjr::Wrappers::UserTarget.new
9
+ target.address = Snmpjr::Wrappers::SMI::GenericAddress.parse("udp:#{configuration.host}/#{configuration.port}")
10
+ target.security_name = Snmpjr::Wrappers::SMI::OctetString.new(configuration.user)
11
+ target.version = Snmpjr::Version::V3
12
+ target.retries = configuration.retries
13
+ target.timeout = configuration.timeout
14
+ target.security_level = configuration.security_level
15
+ target
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,6 @@
1
1
  class Snmpjr
2
- VERSION = "0.2.2"
2
+ module Version
3
+ V2C = 1
4
+ V3 = 3
5
+ end
3
6
  end
data/lib/snmpjr/walker.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  require 'snmpjr/wrappers/util'
2
2
  require 'snmpjr/wrappers/smi'
3
- require 'snmpjr/session'
3
+ require 'snmpjr/response'
4
+ require 'snmpjr/session_v2c'
4
5
 
5
6
  class Snmpjr
6
7
  class Walker
7
8
  def initialize opts = {}
8
9
  @target = opts.fetch(:target)
9
- @session = Snmpjr::Session.new
10
+ @session = opts.fetch(:session)
10
11
  @tree_utils = Snmpjr::Wrappers::Util::TreeUtils.new(@session.snmp, pdu_factory)
11
12
  end
12
13
 
@@ -0,0 +1,9 @@
1
+ require 'snmpjr/wrappers/snmp4j'
2
+
3
+ class Snmpjr
4
+ module Wrappers
5
+ module MP
6
+ include_package 'org.snmp4j.mp'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'snmpjr/wrappers/snmp4j'
2
+
3
+ class Snmpjr
4
+ module Wrappers
5
+ module Security
6
+ include_package 'org.snmp4j.security'
7
+ end
8
+ end
9
+ end
data/lib/snmpjr.rb CHANGED
@@ -1,46 +1,44 @@
1
- require "snmpjr/version"
1
+ require 'snmpjr/configuration_v2c'
2
+ require 'snmpjr/configuration_v3'
2
3
  require 'snmpjr/wrappers/smi'
3
4
  require "snmpjr/getter"
4
- require "snmpjr/walker"
5
- require "snmpjr/target"
5
+ require 'snmpjr/walker'
6
+ require 'snmpjr/version'
6
7
 
7
8
  class Snmpjr
8
9
 
9
- def initialize options = {}
10
- @host = options.fetch(:host)
11
- @port = options.fetch(:port) { 161 }
12
- @community = options.fetch(:community)
13
- @timeout = options.fetch(:timeout) { 5000 }
14
- @retries = options.fetch(:retries) { 0 }
10
+ CONFIGURATION_VERSION = {
11
+ Snmpjr::Version::V2C => Snmpjr::ConfigurationV2C,
12
+ Snmpjr::Version::V3 => Snmpjr::ConfigurationV3
13
+ }
15
14
 
16
- @target = Snmpjr::Target.new.create(host: @host,
17
- port: @port,
18
- community: @community,
19
- timeout: @timeout,
20
- retries: @retries
21
- )
15
+ def initialize version
16
+ @version = version
17
+ end
22
18
 
23
- @max_oids_per_request = options.fetch(:max_oids_per_request) { 30 }
19
+ def configuration
20
+ @configuration ||= CONFIGURATION_VERSION.fetch(@version).new
24
21
  end
25
22
 
26
- def get oids
27
- getter = Snmpjr::Getter.new(target: @target, max_oids_per_request: @max_oids_per_request)
23
+ def configure
24
+ yield(configuration) if block_given?
25
+ self
26
+ end
28
27
 
29
- if oids.is_a?(String)
30
- getter.get oids
31
- elsif oids.is_a?(Array)
32
- getter.get_multiple oids
33
- else
34
- raise ArgumentError.new 'You can request a single Oid using a String, or multiple using an Array'
35
- end
28
+ def get oids
29
+ Snmpjr::Getter.new(
30
+ session: configuration.create_session,
31
+ target: configuration.create_target,
32
+ pdu: configuration.create_pdu,
33
+ config: configuration
34
+ ).get Array(oids)
36
35
  end
37
36
 
38
37
  def walk oid
39
- if oid.is_a?(String)
40
- Snmpjr::Walker.new(target: @target).walk Snmpjr::Wrappers::SMI::OID.new(oid)
41
- else
42
- raise ArgumentError.new 'The oid needs to be passed in as a String'
43
- end
38
+ Snmpjr::Walker.new(
39
+ session: configuration.create_session,
40
+ target: configuration.create_target,
41
+ pdu: configuration.create_pdu
42
+ ).walk Snmpjr::Wrappers::SMI::OID.new(oid.to_s)
44
43
  end
45
-
46
44
  end
data/snmpjr.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'snmpjr/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'snmpjr'
8
- spec.version = Snmpjr::VERSION
8
+ spec.version = '0.3.0'
9
9
  spec.authors = ['Zen Kyprianou']
10
10
  spec.email = ['zen@kyprianou.eu']
11
11
  spec.summary = 'Simple SNMP interface for JRuby'
@@ -1,12 +1,19 @@
1
1
  require 'snmpjr'
2
2
  require 'snmpjr/response'
3
- require 'snmpjr/target_timeout_error'
3
+ require 'snmpjr/session_v2c'
4
4
 
5
- describe "snmpjr" do
5
+ describe "snmpjr for snmp v2c" do
6
6
 
7
7
  describe 'GET' do
8
+ subject { Snmpjr.new(Snmpjr::Version::V2C) }
9
+
8
10
  context 'when the host is reachable' do
9
- subject { Snmpjr.new(host: 'demo.snmplabs.com', port: 161, community: 'public') }
11
+ before do
12
+ subject.configure do |config|
13
+ config.host = 'demo.snmplabs.com'
14
+ config.community = 'public'
15
+ end
16
+ end
10
17
 
11
18
  it 'can perform a simple synchronous get request on an snmp agent' do
12
19
  expect(subject.get '1.3.6.1.2.1.1.1.0').to eq Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.1.0', value: 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m')
@@ -18,7 +25,7 @@ describe "snmpjr" do
18
25
  expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0']).to eq expected
19
26
  end
20
27
 
21
- context "when an invalid oid is requested" do
28
+ context "when an invalid oid is requested" do
22
29
 
23
30
  let(:expected) { [Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5', error: 'noSuchInstance'),
24
31
  Snmpjr::Response.new(oid: '1.3.6.1.2.1.1.5.0', value: 'zeus.snmplabs.com')] }
@@ -30,7 +37,13 @@ describe "snmpjr" do
30
37
  end
31
38
 
32
39
  context 'when the host is unreachable' do
33
- subject { Snmpjr.new(host: 'example.com', port: 161, community: 'public', timeout: 50) }
40
+ before do
41
+ subject.configure do |config|
42
+ config.host = 'demo.snmplabs.com'
43
+ config.community = 'public'
44
+ config.timeout = 50
45
+ end
46
+ end
34
47
 
35
48
  it 'the request times out after 5 seconds' do
36
49
  expect{
@@ -1,18 +1,26 @@
1
1
  require 'snmpjr'
2
2
  require 'snmpjr/response'
3
- require 'snmpjr/target_timeout_error'
3
+ require 'snmpjr/session_v2c'
4
4
 
5
- describe "snmpjr" do
5
+ describe "snmpjr for snmp v2c" do
6
6
 
7
7
  describe 'WALK' do
8
+ subject { Snmpjr.new(Snmpjr::Version::V2C) }
9
+
8
10
  context 'when the host is reachable' do
9
- subject { Snmpjr.new(host: 'demo.snmplabs.com', port: 161, community: 'public') }
11
+ before do
12
+ subject.configure do |config|
13
+ config.host = 'demo.snmplabs.com'
14
+ config.port = 161
15
+ config.community = 'public'
16
+ end
17
+ end
10
18
 
11
19
  it 'can perform a simple synchronous walk request on an snmp agent' do
12
20
  response = subject.walk '1.3.6.1.2.1.1'
13
21
  expect(response.count).to eq 11
14
22
  expect(response.first.to_s).to eq 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'
15
- expect(response.last.to_s).to match /^\d+\:\d+:\d+\.\d+$/
23
+ expect(response.last.to_s).to match(/^\d+\:\d+:\d+\.\d+$/)
16
24
  end
17
25
 
18
26
  context "when a non existent subtree is walked" do
@@ -23,7 +31,14 @@ describe "snmpjr" do
23
31
  end
24
32
 
25
33
  context 'when the host is unreachable' do
26
- subject { Snmpjr.new(host: 'example.com', port: 161, community: 'public', timeout: 50) }
34
+ before do
35
+ subject.configure do |config|
36
+ config.host = 'demo.snmplabs.com'
37
+ config.port = 161
38
+ config.community = 'public'
39
+ config.timeout = 50
40
+ end
41
+ end
27
42
 
28
43
  it 'the request times out after 5 seconds' do
29
44
  expect{