snmputils 0.0.1
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.
- data/.gitignore +20 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +58 -0
- data/Rakefile +35 -0
- data/lib/snmputils/arrayutils.rb +53 -0
- data/lib/snmputils/stringutils.rb +18 -0
- data/lib/snmputils/version.rb +5 -0
- data/lib/snmputils.rb +128 -0
- data/snmputils.gemspec +28 -0
- data/spec/snmputils/arrayutils_spec.rb +73 -0
- data/spec/snmputils/stringutils_spec.rb +43 -0
- data/spec/snmputils_spec.rb +54 -0
- data/spec/spec_helper.rb +13 -0
- metadata +181 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
snmp-utils
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ted Cook
|
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.rdoc
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= snmputils
|
2
|
+
|
3
|
+
{<img src="https://travis-ci.org/teddyphreak/ruby-snmputils.png">}[https://travis-ci.org/teddyphreak/ruby-snmputils]
|
4
|
+
{<img src="https://codeclimate.com/github/teddyphreak/ruby-snmputils.png">}[https://codeclimate.com/github/teddyphreak/ruby-snmputils]
|
5
|
+
{<img src="https://gemnasium.com/teddyphreak/ruby-snmputils.png">}[https://gemnasium.com/teddyphreak/ruby-snmputils]
|
6
|
+
|
7
|
+
Functional wrappers for Dave Halliday's ruby-snmp[https://github.com/hallidave/ruby-snmp]
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'snmputils'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
gem install snmputils
|
22
|
+
|
23
|
+
== Usage
|
24
|
+
|
25
|
+
require snmputils
|
26
|
+
|
27
|
+
SNMPUtils.get({:host => 'localhost'}, 'SNMPv2-MIB::sysName.0')
|
28
|
+
# => {"SNMPv2-MIB::sysName.0"=>"localhost", "1.3.6.1.2.1.1.5.0"=>"localhost"}
|
29
|
+
|
30
|
+
SNMPUtils.walk({:host => 'localhost'}, ['SNMPv2-MIB::sysORDescr', 'SNMPv2-MIB::sysORID'])
|
31
|
+
# => [
|
32
|
+
# {
|
33
|
+
# "SNMPv2-MIB::sysORDescr"=>"The SNMP Management Architecture MIB.",
|
34
|
+
# "1.3.6.1.2.1.1.9.1.3"=>"The SNMP Management Architecture MIB.",
|
35
|
+
# "SNMPv2-MIB::sysORID"=>"SNMPv2-SMI::snmpModules.10.3.1.1",
|
36
|
+
# "1.3.6.1.2.1.1.9.1.2"=>"SNMPv2-SMI::snmpModules.10.3.1.1"
|
37
|
+
# },
|
38
|
+
# ...,
|
39
|
+
# {
|
40
|
+
# "SNMPv2-MIB::sysORDescr"=>"View-based Access Control Model for SNMP.",
|
41
|
+
# "1.3.6.1.2.1.1.9.1.3"=>"View-based Access Control Model for SNMP.",
|
42
|
+
# "SNMPv2-MIB::sysORID"=>"SNMPv2-SMI::snmpModules.16.2.2.1",
|
43
|
+
# "1.3.6.1.2.1.1.9.1.2"=>"SNMPv2-SMI::snmpModules.16.2.2.1"
|
44
|
+
# }
|
45
|
+
# ]
|
46
|
+
|
47
|
+
== Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
54
|
+
|
55
|
+
== License
|
56
|
+
|
57
|
+
{MIT License}[http://opensource.org/licenses/MIT]
|
58
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
###
|
9
|
+
# RDoc (http://rdoc.rubyforge.org/) tasks
|
10
|
+
require 'rdoc/task'
|
11
|
+
Rake::RDocTask.new(:rdoc_generate) do |rdoc|
|
12
|
+
rdoc.main = "README.rdoc"
|
13
|
+
rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb", "bin/**/*")
|
14
|
+
rdoc.title = "snmputils - A functional wrapper to snmp"
|
15
|
+
end
|
16
|
+
|
17
|
+
task :rdoc => :rdoc_generate
|
18
|
+
|
19
|
+
###
|
20
|
+
# Yardstick (https://github.com/dkubb/yardstick) tasks
|
21
|
+
require 'yardstick/rake/measurement'
|
22
|
+
require 'yardstick/rake/verify'
|
23
|
+
|
24
|
+
# Yardstick (https://github.com/dkubb/yardstick) measure coverage
|
25
|
+
Yardstick::Rake::Measurement.new(:yardstick_measure) do |measurement|
|
26
|
+
measurement.output = 'measurement/report.txt'
|
27
|
+
end
|
28
|
+
|
29
|
+
# Yardstick (https://github.com/dkubb/yardstick) verify coverage
|
30
|
+
Yardstick::Rake::Verify.new(:yardstick_verify) do |verify|
|
31
|
+
verify.threshold = 80
|
32
|
+
end
|
33
|
+
|
34
|
+
task :coverage_measure => :yardstick_measure
|
35
|
+
task :coverage_verify => :yardstick_verify
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module ArrayUtils
|
2
|
+
|
3
|
+
class ::Array
|
4
|
+
|
5
|
+
public
|
6
|
+
# Convenience method to return the second element in the list
|
7
|
+
#
|
8
|
+
# @api public
|
9
|
+
# @return [Object] The second object in the array
|
10
|
+
# @example ['a', 'b', 'c'].second # => 'b'
|
11
|
+
def second
|
12
|
+
return at(1)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Calculate the longest common prefix for all elements in the array.
|
16
|
+
#
|
17
|
+
# Requires array elements to support size and take operations
|
18
|
+
#
|
19
|
+
# @api public
|
20
|
+
# @return [Object] The largest common prefix for all elements in the array
|
21
|
+
# @example [['a', 'b'], ['a', 'b', 'c', 'd'], ['a', 'b', 'e', 'f']].prefix # => ['a', 'b']
|
22
|
+
def prefix
|
23
|
+
pairwise_prefix = lambda do |short, long|
|
24
|
+
if short.size == 0 or long .size == 0
|
25
|
+
short.class.new
|
26
|
+
else
|
27
|
+
if short.size > long.size
|
28
|
+
pairwise_prefix.call(long, short)
|
29
|
+
elsif short == long.take(short.size)
|
30
|
+
short
|
31
|
+
else
|
32
|
+
pairwise_prefix.call(short.take(short.size - 1), long)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
reduce &pairwise_prefix
|
37
|
+
end
|
38
|
+
|
39
|
+
# Calculate the longest common suffix for all elements in the array.
|
40
|
+
#
|
41
|
+
# Requires array elements to support size and take operations
|
42
|
+
#
|
43
|
+
# @api public
|
44
|
+
# @return [Object] The largest common suffix for all elements in the array
|
45
|
+
# @example [['y', 'z'], ['w', 'x', 'y', 'z'], ['u', 'v', 'y', 'z']].suffix # => ['y', 'z']
|
46
|
+
def suffix
|
47
|
+
prefix = (map { |x| x != nil && x.reverse }).prefix
|
48
|
+
prefix.reverse if prefix
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module StringUtils
|
2
|
+
|
3
|
+
class ::String
|
4
|
+
|
5
|
+
public
|
6
|
+
# Return the first N characters in the string
|
7
|
+
#
|
8
|
+
# @api public
|
9
|
+
# @param n [Integer] the number of characters to retrieve from the string
|
10
|
+
# @return [String] the first N characters in the string
|
11
|
+
# @example "example".take(2) # => "ex"
|
12
|
+
def take(n=1)
|
13
|
+
chars.to_a.take(n).join('')
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/lib/snmputils.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'snmputils/version'
|
2
|
+
require 'snmp'
|
3
|
+
require 'snmp/mib'
|
4
|
+
require 'snmputils/stringutils'
|
5
|
+
require 'snmputils/arrayutils'
|
6
|
+
|
7
|
+
# @author Ted Cok
|
8
|
+
module SNMPUtils
|
9
|
+
|
10
|
+
private
|
11
|
+
# Return the dot notation string representation of a SNMP::VarBind's OID
|
12
|
+
#
|
13
|
+
# @api private
|
14
|
+
# @param x [SNMP::VarBind] the input VarBind value
|
15
|
+
# @return [String] the dot notation string representation of the VarBind's OID
|
16
|
+
def self.oid(x) x.oid.to_s end
|
17
|
+
|
18
|
+
# Return the symbolic string representation of a SNMP::VarBind's OID
|
19
|
+
#
|
20
|
+
# @api private
|
21
|
+
# @param x [SNMP::VarBind] the input VarBind value
|
22
|
+
# @return [String] the symbolic string representation of the VarBind's OID
|
23
|
+
def self.name(x) x.oid.to_str end
|
24
|
+
|
25
|
+
# Return the string representation of a SNMP::VarBind's value
|
26
|
+
#
|
27
|
+
# @api private
|
28
|
+
# @param x [SNMP::VarBind] the input VarBind value
|
29
|
+
# @return [String] the string representation of the VarBind's value
|
30
|
+
def self.value(x) x.value.to_s unless ['noSuchInstance', 'noSuchObject'].include? x.value.to_s end
|
31
|
+
|
32
|
+
# Return a Hash representation of a SNMP::VarBind's value indexed by dotted OID
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
# @param x [SNMP::VarBind] the input VarBind value
|
36
|
+
# @return [String] the Hash representation of the VarBind as !{ oid(x) => value(x) }
|
37
|
+
# @see #oid
|
38
|
+
# @see #value
|
39
|
+
def self.oid_keyval(x)
|
40
|
+
value(x) && { oid(x) => value(x) } or {}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Return a Hash representation of a SNMP::VarBind's value indexed by symbolic OID
|
44
|
+
#
|
45
|
+
# @api private
|
46
|
+
# @param x [SNMP::VarBind] the input VarBind value
|
47
|
+
# @return [String] the Hash representation of the VarBind as !{ name(x) => value(x) }
|
48
|
+
# @see #name
|
49
|
+
# @see #value
|
50
|
+
def self.name_keyval(x)
|
51
|
+
value(x) && ( name(x) != oid(x) ) && { name(x) => value(x) } or {}
|
52
|
+
end
|
53
|
+
|
54
|
+
# SNMP get. Return an Enumerator of the SNMP::VarBind at each input oids
|
55
|
+
#
|
56
|
+
# @api private
|
57
|
+
# @param opts [Hash] the snmp connection options. Check {http://snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values
|
58
|
+
# @param oids [Array] a list of oids containing the interesting values to retrieve
|
59
|
+
# @return [Enumerator] an {http://www.ruby-doc.org/core-2.0/Enumerator.html Enumerator} of {http://snmplib.rubyforge.org/doc/SNMP/VarBind.html SNMP::VarBind} values
|
60
|
+
def self.snmpget(opts, oids)
|
61
|
+
return Enumerator.new do |yielder|
|
62
|
+
SNMP::Manager.open(opts) do |manager|
|
63
|
+
begin
|
64
|
+
response = manager.get([oids].flatten)
|
65
|
+
response.each_varbind do |v|
|
66
|
+
yielder.yield v
|
67
|
+
end
|
68
|
+
rescue ArgumentError
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# SNMP walk. Return an Enumerator of each SNMP::VarBindList at the input oids
|
75
|
+
#
|
76
|
+
# @api private
|
77
|
+
# @param opts [Hash] the snmp connection options. Check {http://snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values
|
78
|
+
# @param oids [Array] a list of oids containing the interesting columns to scan/retrieve from a snmp table
|
79
|
+
# @return [Enumerator] an {http://www.ruby-doc.org/core-2.0/Enumerator.html Enumerator} of {http://snmplib.rubyforge.org/doc/SNMP/VarBindList.html SNMP::VarBindList} values
|
80
|
+
def self.snmpwalk(opts, oids)
|
81
|
+
return Enumerator.new do |yielder|
|
82
|
+
SNMP::Manager.open(opts) do |manager|
|
83
|
+
begin
|
84
|
+
manager.walk([oids].flatten) do |row|
|
85
|
+
yielder.yield row
|
86
|
+
end
|
87
|
+
rescue ArgumentError
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
public
|
94
|
+
|
95
|
+
# SNMP get. Return a Hash representation of the values at the input oids
|
96
|
+
#
|
97
|
+
# @api public
|
98
|
+
# @param opts [Hash] the snmp connection options. Check {http://snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values
|
99
|
+
# @param oids [Array] a list of oids containing the interesting values to retrieve
|
100
|
+
# @return [Hash] a Hash representation of the values at the requested oids as !{ name(oids[0]) => value(oids[0]), oid(oids[0]) => value(oids[0]) } for each value in the input array oids
|
101
|
+
# @example snmpvalue = SNMPUtils.get({:host => 'localhost'}, 'SNMPv2-MIB::sysName.0') # => {"SNMPv2-MIB::sysName.0"=>"medusa", "1.3.6.1.2.1.1.5.0"=>"medusa"}
|
102
|
+
def self.get(opts, oids)
|
103
|
+
return snmpget(opts,oids).inject({}) do |row, value|
|
104
|
+
row.merge(oid_keyval(value)).merge(name_keyval(value))
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# SNMP walk. Return a List[Hash] representation of the values at the input oids
|
109
|
+
#
|
110
|
+
# @api public
|
111
|
+
# @param opts [Hash] the snmp connection options. Check {http://snmplib.rubyforge.org/doc/SNMP/Manager.html snmplib} for default values
|
112
|
+
# @param oids [Array] a list of oids containing the interesting snmp columns to retrieve
|
113
|
+
# @return [List] a List of Hashes describing each row in the table as
|
114
|
+
# @example snmpvalues = SNMPUtils.walk({:host => 'localhost'}, ['SNMPv2-MIB::sysORDescr', 'SNMPv2-MIB::sysORID']) # =>
|
115
|
+
def self.walk(opts, oids)
|
116
|
+
return snmpwalk(opts, oids).inject([]) do |table, row|
|
117
|
+
rawoids = row.inject({}) do |row_n, value|
|
118
|
+
row_n.merge(oid_keyval(value)).merge(name_keyval(value))
|
119
|
+
end
|
120
|
+
suffix = rawoids.keys.suffix
|
121
|
+
table << rawoids.inject({}) do |row_n, kvpair|
|
122
|
+
key, value = kvpair
|
123
|
+
row_n.merge(key.sub(/#{suffix}$/, '') => value)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
data/snmputils.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'snmputils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "snmputils"
|
8
|
+
spec.version = Snmp::Utils::VERSION
|
9
|
+
spec.authors = ["Ted Cook"]
|
10
|
+
spec.email = ["teodoro.cook@gmail.com"]
|
11
|
+
spec.description = "SNMP low level routines"
|
12
|
+
spec.summary = "SNMP low level routines"
|
13
|
+
spec.homepage = "https://github.com/teddyphreak/ruby-snmputils.git"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.1"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.13"
|
24
|
+
spec.add_development_dependency "yard", "~> 0.8"
|
25
|
+
spec.add_development_dependency "yardstick", "~> 0.9"
|
26
|
+
spec.add_development_dependency "travis-lint", "~> 1.7"
|
27
|
+
spec.add_dependency "snmp", "~> 1.1"
|
28
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
|
5
|
+
describe '#suffix' do
|
6
|
+
|
7
|
+
it "should return the input for scalar arrays" do
|
8
|
+
['single'].suffix.should eql 'single'
|
9
|
+
[''].suffix.should eql ''
|
10
|
+
[].suffix.should eql nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return the input for list input with a single repeated value" do
|
14
|
+
(['two'] * 2).suffix.should eql 'two'
|
15
|
+
(['three'] * 3).suffix.should eql 'three'
|
16
|
+
(['twenty'] * 20).suffix.should eql 'twenty'
|
17
|
+
(['two'.chars.to_a] * 2).suffix.should eql 'two'.chars.to_a
|
18
|
+
(['three'.chars.to_a] * 3).suffix.should eql 'three'.chars.to_a
|
19
|
+
(['twenty'.chars.to_a] * 20).suffix.should eql 'twenty'.chars.to_a
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return the largest suffix for a list of length 2" do
|
23
|
+
['two thousand one', 'four million'].suffix.should eql ''
|
24
|
+
['two thousand one.1', 'two, million.1'].suffix.should eql '.1'
|
25
|
+
['three thousand', 'threethousand'].suffix.should eql 'thousand'
|
26
|
+
(['two thousand one.1', 'two, million.1'].map { |x| x.chars.to_a }).suffix.should eql '.1'.chars.to_a
|
27
|
+
(['three thousand', 'threethousand'].map { |x| x.chars.to_a }).suffix.should eql 'thousand'.chars.to_a
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return the largest suffix for a list of length N" do
|
31
|
+
(['', 'prove', 'provoke'].map { |x| x.reverse } * 2).suffix.should eql ''.reverse
|
32
|
+
(['proud', 'prove', 'provoke'].map { |x| x.reverse } * 6).suffix.should eql 'pro'.reverse
|
33
|
+
(['', 'prank', 'primary', 'probability'].map { |x| x.reverse } * 12).suffix.should eql ''.reverse
|
34
|
+
(['prune', 'prank', 'primary', 'probability'].map { |x| x.reverse } * 24).suffix.should eql 'pr'.reverse
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#prefix' do
|
40
|
+
|
41
|
+
it "should return the input for scalar arrays" do
|
42
|
+
['single'].prefix.should eql 'single'
|
43
|
+
[''].prefix.should eql ''
|
44
|
+
[].prefix.should eql nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return the input for list input with a single repeated value" do
|
48
|
+
(['two'] * 2).prefix.should eql 'two'
|
49
|
+
(['three'] * 3).prefix.should eql 'three'
|
50
|
+
(['twenty'] * 20).prefix.should eql 'twenty'
|
51
|
+
(['two'.chars.to_a] * 2).prefix.should eql 'two'.chars.to_a
|
52
|
+
(['three'.chars.to_a] * 3).prefix.should eql 'three'.chars.to_a
|
53
|
+
(['twenty'.chars.to_a] * 20).prefix.should eql 'twenty'.chars.to_a
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return the largest prefix for a list of length 2" do
|
57
|
+
['two thousand', 'four million'].prefix.should eql ''
|
58
|
+
['two thousand', 'two, million'].prefix.should eql 'two'
|
59
|
+
['three thousand', 'threepence'].prefix.should eql 'three'
|
60
|
+
(['three thousand', 'threepence'].map { |x| x.chars.to_a }).prefix.should eql 'three'.chars.to_a
|
61
|
+
(['two thousand', 'two, million'].map { |x| x.chars.to_a }).prefix.should eql 'two'.chars.to_a
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return the largest prefix for a list of length N" do
|
65
|
+
(['', 'prove', 'provoke'] * 2).prefix.should eql ''
|
66
|
+
(['proud', 'prove', 'provoke'] * 6).prefix.should eql 'pro'
|
67
|
+
(['', 'prank', 'primary', 'probability'] * 12).prefix.should eql ''
|
68
|
+
(['prune', 'prank', 'primary', 'probability'] * 24).prefix.should eql 'pr'
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
|
5
|
+
describe '#take' do
|
6
|
+
|
7
|
+
it "should return nil for an empty string" do
|
8
|
+
''.take.should eql ''
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return the first character of the string with no input" do
|
12
|
+
'victor'.take.should eql 'v'
|
13
|
+
'tango uniform'.take.should eql 't'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return the first N characters of the string with input N" do
|
17
|
+
'alpha'.take(1).should eql 'a'
|
18
|
+
'hotel'.take(2).should eql 'ho'
|
19
|
+
'india juliet kilo mike'.take('india '.size). should eql 'india '
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#to_a' do
|
25
|
+
|
26
|
+
it "should return an empty array for an empty string" do
|
27
|
+
''.take.should be_empty
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return an array of the first character of the string with no input" do
|
31
|
+
'foxtrot'.take.should eql 'f'
|
32
|
+
'quebec romeo'.take.should eql 'q'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return an array of the first N characters of the string with input N" do
|
36
|
+
'bravo'.take(1).should eql 'b'
|
37
|
+
'delta'.take(2).should eql 'de'
|
38
|
+
'november oscar papa quebec romeo sierra'.take('november oscar'.size). should eql 'november oscar'
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SNMPUtils do
|
4
|
+
|
5
|
+
describe '#get' do
|
6
|
+
|
7
|
+
it "should return an empty SNMP response for an invalid scalar request" do
|
8
|
+
SNMPUtils.get({:host => 'localhost'}, 'invalid').should be_empty
|
9
|
+
SNMPUtils.get({:host => 'localhost'}, []).should be_empty
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return an empty SNMP response for a mixed valid/invalid list request" do
|
13
|
+
SNMPUtils.get({:host => 'localhost'}, ['SNMPv2-MIB::sysName.0', 'invalid']).should be_empty
|
14
|
+
SNMPUtils.get({:host => 'localhost'}, ['invalid', 'SNMPv2-MIB::sysContact.0']).should be_empty
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return a hash SNMP response for a valid scalar request" do
|
18
|
+
SNMPUtils.get({:host => 'localhost'}, 'SNMPv2-MIB::sysName.0').should_not be_empty
|
19
|
+
SNMPUtils.get({:host => 'localhost'}, 'SNMPv2-MIB::sysName.0').should include('SNMPv2-MIB::sysName.0')
|
20
|
+
SNMPUtils.get({:host => 'localhost'}, 'SNMPv2-MIB::sysName.0')['SNMPv2-MIB::sysName.0'].should eql Socket.gethostname
|
21
|
+
SNMPUtils.get({:host => 'localhost'}, '1.3.6.1.2.1.1.5.0').should_not be_empty
|
22
|
+
SNMPUtils.get({:host => 'localhost'}, '1.3.6.1.2.1.1.5.0').should include('1.3.6.1.2.1.1.5.0')
|
23
|
+
SNMPUtils.get({:host => 'localhost'}, '1.3.6.1.2.1.1.5.0')['1.3.6.1.2.1.1.5.0'].should eql Socket.gethostname
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return a hash SNMP response for a valid list request" do
|
27
|
+
SNMPUtils.get({:host => 'localhost'}, ['SNMPv2-MIB::sysName.0', 'SNMPv2-MIB::sysContact.0']).should include('SNMPv2-MIB::sysName.0')
|
28
|
+
SNMPUtils.get({:host => 'localhost'}, ['SNMPv2-MIB::sysName.0', 'SNMPv2-MIB::sysContact.0']).should include('SNMPv2-MIB::sysContact.0')
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#walk' do
|
34
|
+
it "should return an empty SNMP response for an invalid scalar request" do
|
35
|
+
SNMPUtils.walk({:host => 'localhost'}, 'invalid').should be_empty
|
36
|
+
SNMPUtils.walk({:host => 'localhost'}, []).should be_empty
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return an empty SNMP response for a mixed valid/invalid list request" do
|
40
|
+
SNMPUtils.walk({:host => 'localhost'}, ['SNMPv2-MIB::sysName.0', 'invalid']).should be_empty
|
41
|
+
SNMPUtils.walk({:host => 'localhost'}, ['invalid', 'SNMPv2-MIB::sysContact.0']).should be_empty
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return a hash list SNMP response for a valid requests" do
|
45
|
+
SNMPUtils.walk({:host => 'localhost'}, ['SNMPv2-MIB::sysORDescr', 'SNMPv2-MIB::sysORID']).should_not be_empty
|
46
|
+
SNMPUtils.walk({:host => 'localhost'}, ['SNMPv2-MIB::sysORDescr', 'SNMPv2-MIB::sysORID']).each do |x|
|
47
|
+
x.should include('SNMPv2-MIB::sysORDescr')
|
48
|
+
x.should include('SNMPv2-MIB::sysORID')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
unless Kernel.respond_to?(:require_relative)
|
2
|
+
module Kernel
|
3
|
+
def require_relative(path)
|
4
|
+
require File.join(File.dirname(caller[0]), path.to_str)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
require_relative'../lib/snmputils'
|
10
|
+
require_relative'../lib/snmputils/arrayutils'
|
11
|
+
require_relative'../lib/snmputils/stringutils'
|
12
|
+
|
13
|
+
require 'yaml'
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snmputils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ted Cook
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '10.1'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '10.1'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.13'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.13'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: yard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.8'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.8'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: yardstick
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0.9'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0.9'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: travis-lint
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '1.7'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '1.7'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: snmp
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.1'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '1.1'
|
126
|
+
description: SNMP low level routines
|
127
|
+
email:
|
128
|
+
- teodoro.cook@gmail.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .rspec
|
135
|
+
- .ruby-gemset
|
136
|
+
- .ruby-version
|
137
|
+
- .travis.yml
|
138
|
+
- Gemfile
|
139
|
+
- LICENSE.txt
|
140
|
+
- README.rdoc
|
141
|
+
- Rakefile
|
142
|
+
- lib/snmputils.rb
|
143
|
+
- lib/snmputils/arrayutils.rb
|
144
|
+
- lib/snmputils/stringutils.rb
|
145
|
+
- lib/snmputils/version.rb
|
146
|
+
- snmputils.gemspec
|
147
|
+
- spec/snmputils/arrayutils_spec.rb
|
148
|
+
- spec/snmputils/stringutils_spec.rb
|
149
|
+
- spec/snmputils_spec.rb
|
150
|
+
- spec/spec_helper.rb
|
151
|
+
homepage: https://github.com/teddyphreak/ruby-snmputils.git
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
none: false
|
160
|
+
requirements:
|
161
|
+
- - ! '>='
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
none: false
|
166
|
+
requirements:
|
167
|
+
- - ! '>='
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
requirements: []
|
171
|
+
rubyforge_project:
|
172
|
+
rubygems_version: 1.8.25
|
173
|
+
signing_key:
|
174
|
+
specification_version: 3
|
175
|
+
summary: SNMP low level routines
|
176
|
+
test_files:
|
177
|
+
- spec/snmputils/arrayutils_spec.rb
|
178
|
+
- spec/snmputils/stringutils_spec.rb
|
179
|
+
- spec/snmputils_spec.rb
|
180
|
+
- spec/spec_helper.rb
|
181
|
+
has_rdoc:
|