snmp-open 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ae08ac661e97b5c47899d166a7bdcd6b71e0c55
4
- data.tar.gz: b10aae94bb31fc05b38ec3d3ab9ce1d450c3b104
3
+ metadata.gz: 118c73e3356d85cda27007ac7ada6ccb25e36af3
4
+ data.tar.gz: a6bbbbddbb1792aa8fef75c308c988c57d74e8a6
5
5
  SHA512:
6
- metadata.gz: 998d8abcd4e3e7b5967affb6441f590201c7a0c15b24ebe7f56911fa17a103086a2c6392586baa4fb9fe26083985e9de8cbc174eb2524a4303602a11757301be
7
- data.tar.gz: ecf925444abd22e218b7235985aa5d98357f9e6e34b759ac760d3bbc5c1e83c89c46c261132663ee479124932c5b8d07e3313c88edd812cccf18aed1a179ce31
6
+ metadata.gz: c6d5f53dfcc6db0e2278926bf2cd1c86904a7575c9199f3f4a2c6e405d61c245b6d84887c0f8ca8ca7af19c4ab1639b76cc164118f6703f7fd0596749fe1e2bd
7
+ data.tar.gz: 7986c9fdb242a009c384dd0be8824b73cd59f103b46502d7c142d6f863496c1983e6974f11dca2ffc2d8f3115951b624b092010b2b55196ea5a18e8765fd6858
@@ -12,6 +12,10 @@ module SNMP
12
12
  auth_protocol: '-a',
13
13
  community: '-c',
14
14
  context: '-n',
15
+ no_check_increasing: {
16
+ 'snmpbulkwalk' => '-Cc',
17
+ 'snmpwalk' => '-Cc'
18
+ },
15
19
  numeric: '-On', # needed by parser, should always be enabled
16
20
  priv_password: '-X', # not recommended, see snmp.conf(5)
17
21
  priv_protocol: '-x',
@@ -22,6 +26,12 @@ module SNMP
22
26
  host: nil
23
27
  }.freeze
24
28
 
29
+ OPTION_VALUES = {
30
+ no_check_increasing: {
31
+ true => ''
32
+ }.freeze
33
+ }.freeze
34
+
25
35
  # +options+ accepts options dealing with making connections to the host,
26
36
  # including all of the options listed in the +OPTIONS+ constant hash.
27
37
  # Other options can be given as strings (or any object with a suitable
@@ -33,26 +43,25 @@ module SNMP
33
43
  def initialize(options)
34
44
  host = options.delete(:host) ||
35
45
  (raise ArgumentError, 'Host expected but not given')
36
- @host_options = merge_options(options).merge('-On' => nil, host => nil)
46
+ opts = merge_options(options).merge('-On' => nil, host => nil)
47
+ @command_options, @host_options = partition_options(opts)
37
48
  end
38
49
 
39
50
  def capture(cmd, oid, options = {})
40
51
  out, err = Open3.capture3(cli(cmd, oid, options))
41
- raise err unless err.empty?
52
+ raise CommandError, err.chomp unless err.empty?
42
53
  out
43
54
  end
44
55
 
45
56
  # Generate a CLI command string
46
57
  def cli(command, id = nil, options = {})
47
- command = case command
48
- when Symbol then "snmp#{command}"
49
- else command.to_s
50
- end
58
+ command = normalize_command(command)
51
59
 
52
60
  [
53
61
  command,
54
62
  *options.map { |k, v| "#{k}#{v}" },
55
63
  *@host_options.map { |k, v| "#{k}#{v}" },
64
+ *@command_options.fetch(command, {}).map { |k, v| "#{k}#{v}" },
56
65
  *id
57
66
  ].join(' ')
58
67
  end
@@ -62,7 +71,8 @@ module SNMP
62
71
  def merge_options(options = {})
63
72
  options.each_pair.with_object({}) do |(key, value), opts|
64
73
  if OPTIONS.key?(key)
65
- opts[OPTIONS[key]] = value
74
+ opts[OPTIONS[key]] =
75
+ (OPTION_VALUES.fetch(key, {}).fetch(value, value) || next)
66
76
  elsif key.is_a?(String)
67
77
  opts[key] = value
68
78
  else
@@ -70,6 +80,42 @@ module SNMP
70
80
  end
71
81
  end
72
82
  end
83
+
84
+ def normalize_command(command)
85
+ case command
86
+ when Symbol then "snmp#{command}"
87
+ else command.to_s
88
+ end
89
+ end
90
+
91
+ def partition_options(options)
92
+ command_keys, host_keys = options
93
+ .keys
94
+ .partition { |k| k.is_a?(Hash) }
95
+
96
+ [
97
+ merge_command_options(options, command_keys),
98
+ merge_host_options(options, host_keys)
99
+ ]
100
+ end
101
+
102
+ def merge_command_options(options, keys)
103
+ keys.each.with_object({}) do |commands, hash|
104
+ command_value = options[commands]
105
+ commands.each_pair do |command, option|
106
+ hash[command] ||= {}
107
+ hash[command][option] = command_value
108
+ end
109
+ end
110
+ end
111
+
112
+ def merge_host_options(options, keys)
113
+ keys.each.with_object({}) do |key, hash|
114
+ hash[key] = options[key]
115
+ end
116
+ end
73
117
  end # class CommandReader
118
+
119
+ class CommandError < RuntimeError; end
74
120
  end # class Open
75
121
  end # module SNMP
@@ -1,5 +1,5 @@
1
1
  module Snmp
2
2
  module Open
3
- VERSION = '0.1.4'.freeze
3
+ VERSION = '0.1.5'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snmp-open
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Miller