snmpjr 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDA5MDUxNDEyZmM2NjFiMzEyZDQzMjliMjY5YTlkOTlmNzBiZTAzZg==
5
+ data.tar.gz: !binary |-
6
+ NTQ4ZmE0YjZhYjllOTE4YmMzYjk1YjEwZGUwZTkxNzFjN2VjNjM5YQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MmQyYzA2NThlNWJkMGYyNDFkZTgzZWQ4ODJiZDI5MjZmYzFiODk0MmMxNDBi
10
+ ZWU1YWY2ZGU4NGUxZmMxMWM3MjEyNGE0ZGY2ODI1OTc5OWQ2NjJhZWJmYzEx
11
+ OGNhYWVmNTQ2MzFjY2IwNGFkNDRmNjk1ZThlNjYzMDNiMjM4M2M=
12
+ data.tar.gz: !binary |-
13
+ ZWNjYTE1N2Y1MGVhM2FmOWIwZmQ2ZmI5NzA3ZWIxNzZjNDU3ZjVkN2ZjMTFk
14
+ MTg4ZjliMmVlOWEwNjY3OTE4NDdjOWViYTAyMzQxNGMwMWVkMjEwODA2ODIy
15
+ NzA2ZWNiNDI5NWE1MzE3YTI2NTFkYWQ1ODAyNTcyZGU1NWI5NGE=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ log/*
2
+ *.swo
3
+ *.swp
4
+ /.bundle/
5
+ /.yardoc
6
+ /Gemfile.lock
7
+ /_yardoc/
8
+ /coverage/
9
+ /doc/
10
+ /pkg/
11
+ /spec/reports/
12
+ /tmp/
13
+ *.bundle
14
+ *.so
15
+ *.o
16
+ *.a
17
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create jruby-1.7.16@snmpjr
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - jruby-1.7.0
4
+ - jruby-1.7.16
5
+ deploy:
6
+ provider: rubygems
7
+ api_key:
8
+ secure: C9X1OQ07eVDPqOEcKQOOo+Li18J5VQ6uwsua6fNb7Mvzm5RVYKdaYMjan6gSJNkprxk5yDt/LQSZBBSSluO0yqxzyFFFBqzswPPKsYys5HVW45rFLFDs83e2YtgACEnXfNozuWoy0i3jgpN/P+eqrBlNFfuHpm4di7IBmeZimDY=
9
+ gem: snmpjr
10
+ on:
11
+ tags: true
12
+ repo: zenonas/snmpjr
13
+ all_branches: true
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rake_rack'
5
+ gem 'pry'
6
+ gem 'rspec'
7
+ gem 'rake'
8
+ gem 'simplecov'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Zen Kyprianou
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Snmpjr
2
+
3
+ [![Build Status](https://travis-ci.org/zenonas/snmpjr.svg?branch=master)](https://travis-ci.org/zenonas/snmpjr)
4
+ 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.
5
+
6
+ Please note the gem is still in early develpment. Do not use as of yet!
7
+
8
+ ## Features
9
+
10
+ * Simple Synchronous SNMP v2c Get requests
11
+
12
+ ## Requirements
13
+
14
+ * Java 1.6+
15
+ * JRuby 1.7+
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'snmpjr'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install snmpjr
32
+
33
+ ## Usage
34
+
35
+ ```ruby
36
+ # Initialize Snmpjr with host, port and a community
37
+ snmp = Snmpjr.new(:host => '127.0.0.1', :port => 161, :community => 'public')
38
+
39
+ # Call get on any single Oid
40
+ snmp.get '1.3.6.1.2.1.1.1.0'
41
+ => 'The result'
42
+
43
+ # Call get on an array of Oids'
44
+ snmp.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.3.0']
45
+ => ['First result', 'Second result']
46
+ ```
47
+
48
+ When you request an Array of Oids these will be pulled sequentially
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/zenonas/snmpjr/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake/clean'
2
+ require 'rake_rack'
3
+
4
+ @external_dependencies = %w[jruby java]
5
+
6
+ task :default => [
7
+ :clean,
8
+ :"rake_rack:check_external_dependencies",
9
+ :"rake_rack:code_quality:all",
10
+ :"rake_rack:rspec",
11
+ :"rake_rack:coverage:check_specs",
12
+ :"rake_rack:ok",
13
+ ]
data/history.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ == 0.1.1 (12 October 2014)
2
+
3
+ * Fix travis auto deployment
4
+
5
+ == 0.1.0 (12 October 2014)
6
+
7
+ * Simple Synchronous SNMP get requests for SNMP v2c
data/lib/snmpjr/pdu.rb ADDED
@@ -0,0 +1,17 @@
1
+ class Snmpjr
2
+ class Pdu
3
+ module Constants
4
+ GET = -96
5
+ end
6
+
7
+ def create oid
8
+ 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
+ pdu.type = Snmpjr::Pdu::Constants::GET
13
+ pdu
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require 'snmpjr/wrappers/transport'
2
+
3
+ class Snmpjr
4
+ class Session
5
+
6
+
7
+ def send pdu, target
8
+ snmp = Snmpjr::Wrappers::Snmp.new(Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping.new)
9
+ snmp.listen
10
+
11
+ begin
12
+ result = snmp.send(pdu, target)
13
+ rescue
14
+ raise "Failed to send SNMP package"
15
+ ensure
16
+ snmp.close
17
+ end
18
+
19
+ result.response.variable_bindings.first.variable.to_s
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'snmpjr/wrappers/smi'
2
+
3
+ class Snmpjr
4
+ class Target
5
+
6
+ def create options = {}
7
+ target = Snmpjr::Wrappers::CommunityTarget.new
8
+ target.community = Snmpjr::Wrappers::SMI::OctetString.new(options.fetch(:community))
9
+ target.address = Snmpjr::Wrappers::SMI::GenericAddress.parse("udp:#{options.fetch(:host)}/#{options.fetch(:port)}")
10
+ target.version = 1
11
+ target.timeout = 5000
12
+ target
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ class Snmpjr
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'snmpjr/wrappers/snmp4j'
2
+
3
+ class Snmpjr
4
+ module Wrappers
5
+ module SMI
6
+ include_package 'org.snmp4j.smi'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class Snmpjr
2
+ module Wrappers
3
+ begin
4
+ require 'java'
5
+ require 'snmpjr/wrappers/snmp4j-2.3.1.jar'
6
+ rescue
7
+ raise "Snmpjr requires JRuby"
8
+ end
9
+
10
+ include_package 'org.snmp4j'
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require 'snmpjr/wrappers/snmp4j'
2
+
3
+ class Snmpjr
4
+ module Wrappers
5
+ module Transport
6
+ include_package 'org.snmp4j.transport'
7
+ end
8
+ end
9
+ end
data/lib/snmpjr.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "snmpjr/version"
2
+ require "snmpjr/pdu"
3
+ require "snmpjr/session"
4
+ require "snmpjr/target"
5
+
6
+ class Snmpjr
7
+
8
+ def initialize options = {}
9
+ @host = options.fetch(:host)
10
+ @port = options.fetch(:port) || 161
11
+ @community = options.fetch(:community)
12
+ end
13
+
14
+ def get oids
15
+ target = Snmpjr::Target.new.create(:host => @host, :port => @port, :community => @community)
16
+
17
+ case oids.class.to_s
18
+ when 'String'
19
+ get_oid(oids, target)
20
+ when 'Array'
21
+ oids.map{|oid|
22
+ get_oid(oid, target)
23
+ }
24
+ else
25
+ raise ArgumentError.new 'You can request a single Oid using a String, or multiple using an Array'
26
+ end
27
+ end
28
+
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
+ end
data/snmpjr.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'snmpjr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "snmpjr"
8
+ spec.version = Snmpjr::VERSION
9
+ spec.authors = ["Zen Kyprianou"]
10
+ spec.email = ["zen@kyprianou.eu"]
11
+ spec.summary = %q{Simple SNMP interface for JRuby}
12
+ spec.description = %q{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.
13
+ }
14
+ spec.homepage = "https://github.com/zenonas/snmpjr"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.platform = "java"
23
+ end
@@ -0,0 +1,16 @@
1
+ require 'snmpjr'
2
+
3
+ describe "snmpjr" do
4
+
5
+ subject { Snmpjr.new(:host => 'demo.snmplabs.com', :port => 161, :community => 'public') }
6
+ describe 'GET' do
7
+ it "can perform a simple synchronous get request on an snmp agent" do
8
+ expect(subject.get '1.3.6.1.2.1.1.1.0').to eq 'SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'
9
+ end
10
+
11
+ let(:expected) { ['SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m', 'zeus.snmplabs.com'] }
12
+ it "can perform a series of gets if passed an array of oids" do
13
+ expect(subject.get ['1.3.6.1.2.1.1.1.0', '1.3.6.1.2.1.1.5.0']).to eq expected
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ require production_code
2
+
3
+ describe Snmpjr::Pdu do
4
+ describe "#create" do
5
+
6
+ let(:pdu) { double Snmpjr::Wrappers::PDU }
7
+ let(:oid) { double Snmpjr::Wrappers::SMI::OID }
8
+ let(:variable_binding) { double Snmpjr::Wrappers::SMI::VariableBinding }
9
+
10
+ before do
11
+ 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(pdu).to receive(:type=)
15
+ allow(pdu).to receive(:add)
16
+ end
17
+ it "creates a GET Pdu" do
18
+ expect(pdu).to receive(:type=).with(Snmpjr::Pdu::Constants::GET)
19
+ subject.create '1.2.3.4'
20
+ end
21
+
22
+ 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
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ require production_code
2
+
3
+ describe Snmpjr::Session do
4
+ describe "#send" do
5
+ let(:transport_mapping) { double Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping }
6
+
7
+ let(:target) { double :target }
8
+ let(:pdu) { double :pdu }
9
+ let(:result) { double :result }
10
+
11
+ let(:snmp_session) { double Snmpjr::Wrappers::Snmp }
12
+
13
+ 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
+ allow(snmp_session).to receive(:send).and_return result
17
+ 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
+ end
25
+
26
+ it "sends the pdu to the target" do
27
+ expect(snmp_session).to receive(:send).with(pdu, target)
28
+ subject.send(pdu, target)
29
+ end
30
+
31
+ it "closes the connection" do
32
+ expect(snmp_session).to receive(:close)
33
+ subject.send(pdu, target)
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require production_code
2
+
3
+ describe Snmpjr::Target do
4
+
5
+ describe "#create" do
6
+ let(:options) { { :host => '127.0.0.1', :port => 161, :community => 'some_community' } }
7
+
8
+ it "creates an octet string for the community string" do
9
+ expect(Snmpjr::Wrappers::SMI::OctetString).to receive(:new).with(options[:community])
10
+ subject.create(options)
11
+ end
12
+
13
+ it "creates an smi address based on the ip and port name" do
14
+ expect(Snmpjr::Wrappers::SMI::GenericAddress).to receive(:parse).with("udp:127.0.0.1/161")
15
+ subject.create(options)
16
+ end
17
+
18
+ let(:community_target) { double Snmpjr::Wrappers::CommunityTarget }
19
+
20
+ before do
21
+ allow(Snmpjr::Wrappers::CommunityTarget).to receive(:new).and_return community_target
22
+ allow(community_target).to receive(:version=)
23
+ allow(community_target).to receive(:timeout=)
24
+ allow(community_target).to receive(:community=)
25
+ allow(community_target).to receive(:address=)
26
+ end
27
+
28
+ it "sets the snmp version to v2c and the timeout to 5000ms" do
29
+ expect(community_target).to receive(:version=).with(1)
30
+ expect(community_target).to receive(:timeout=).with(5000)
31
+ subject.create(options)
32
+ end
33
+
34
+ it "returns the SNMP4J community target" do
35
+ expect(subject.create(options).class).to eq community_target.class
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,56 @@
1
+ require production_code
2
+
3
+ describe Snmpjr do
4
+ describe "#get" do
5
+ context "when the call is synchronous" do
6
+
7
+ let(:target) { double Snmpjr::Target }
8
+ let(:community_target) { double :community_target }
9
+ let(:session) { double Snmpjr::Session }
10
+ let(:pdu) { double Snmpjr::Pdu }
11
+ let(:created_pdu_1) { double :created_pdu_1 }
12
+
13
+ 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
+ allow(Snmpjr::Target).to receive(:new).and_return target
19
+ allow(target).to receive(:create).with(agent_details).and_return community_target
20
+ end
21
+
22
+ let(:agent_details) { { :host => '127.0.0.1', :port => 161, :community => 'some_community' } }
23
+
24
+ subject { described_class.new(agent_details) }
25
+
26
+ context 'when passed a single oid' do
27
+ it 'performs a synchronous get' do
28
+ subject.get '1.2.3.4.5.6'
29
+ expect(session).to have_received(:send).with(created_pdu_1, community_target)
30
+ end
31
+ end
32
+
33
+ context 'when passed multiple oids' do
34
+ let(:created_pdu_2) { double :created_pdu_2 }
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
41
+ 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
+ end
45
+ end
46
+
47
+ context 'when an invalid argument was passed in' do
48
+ it 'raises an ArgumentError' do
49
+ expect {
50
+ subject.get({'oid_value' => '1.3.4.5.6'})
51
+ }.to raise_error ArgumentError
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+ require 'pry'
3
+
4
+ SimpleCov.coverage_dir 'log/coverage/rspec'
5
+ SimpleCov.add_filter "spec"
6
+ SimpleCov.add_filter "vendor"
7
+ SimpleCov.start
8
+
9
+ def production_code
10
+ spec = caller[0][/spec.+\.rb/]
11
+ './' + spec.gsub('_spec','').gsub(/spec/, 'lib')
12
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snmpjr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: java
6
+ authors:
7
+ - Zen Kyprianou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ! 'Snmpjr aims to provide a clean and simple interface to use SNMP in
14
+ your ruby code. It will wrap the popular SNMP4J library in Java.
15
+
16
+ '
17
+ email:
18
+ - zen@kyprianou.eu
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - .rspec
25
+ - .rvmrc
26
+ - .travis.yml
27
+ - Gemfile
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - history.rdoc
32
+ - lib/snmpjr.rb
33
+ - lib/snmpjr/pdu.rb
34
+ - lib/snmpjr/session.rb
35
+ - lib/snmpjr/target.rb
36
+ - lib/snmpjr/version.rb
37
+ - lib/snmpjr/wrappers/smi.rb
38
+ - lib/snmpjr/wrappers/snmp4j-2.3.1.jar
39
+ - lib/snmpjr/wrappers/snmp4j.rb
40
+ - lib/snmpjr/wrappers/transport.rb
41
+ - snmpjr.gemspec
42
+ - spec/integration/snmpjr_spec.rb
43
+ - spec/snmpjr/pdu_spec.rb
44
+ - spec/snmpjr/session_spec.rb
45
+ - spec/snmpjr/target_spec.rb
46
+ - spec/snmpjr_spec.rb
47
+ - spec/spec_helper.rb
48
+ homepage: https://github.com/zenonas/snmpjr
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.4.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Simple SNMP interface for JRuby
72
+ test_files:
73
+ - spec/integration/snmpjr_spec.rb
74
+ - spec/snmpjr/pdu_spec.rb
75
+ - spec/snmpjr/session_spec.rb
76
+ - spec/snmpjr/target_spec.rb
77
+ - spec/snmpjr_spec.rb
78
+ - spec/spec_helper.rb