fluent-plugin-snmp 0.0.5 → 0.0.6

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.
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = "fluent-plugin-snmp"
6
- gem.version = "0.0.5"
6
+ gem.version = "0.0.6"
7
7
  gem.authors = ["hiro-su"]
8
8
  gem.email = ["h-sugimoto@iij.ad.jp"]
9
9
  gem.description = %q{Input plugin to walk snmp}
@@ -12,6 +12,10 @@ module Fluent
12
12
  config_param :nodes, :string, :default => nil
13
13
  config_param :polling_time, :string, :default => nil
14
14
  config_param :host_name, :string, :default => nil
15
+ config_param :retry, :integer, :default => 5
16
+ config_param :retry_interval, :time, :default => 1
17
+ config_param :method_type, :string, :default => "walk"
18
+ config_param :out_exec_filter, :string, :default => nil
15
19
 
16
20
  # SNMP Lib Params
17
21
  # require param: host, community
@@ -38,7 +42,7 @@ module Fluent
38
42
  config_param :write_community, :string, :default => nil
39
43
  config_param :version, :string, :default => nil # Use :SNMPv1 or :SNMPv2c
40
44
  config_param :timeout, :time, :default => nil
41
- config_param :retries, :integer, :default => nil
45
+ config_param :retries, :integer, :default => 0
42
46
  config_param :transport, :string, :default => nil
43
47
  config_param :max_recv_bytes, :string, :default => nil
44
48
  config_param :mib_dir, :string, :default => nil
@@ -83,7 +87,19 @@ module Fluent
83
87
  :use_IPv6 => @use_IPv6
84
88
  }
85
89
 
86
- @retry_conut = 0
90
+ unless @out_exec_filter.nil?
91
+ @out_exec = lambda do |manager|
92
+ require @out_exec_filter
93
+ opts = {
94
+ :tag => @tag,
95
+ :mib => @mib,
96
+ :mib_modules => @mib_modules,
97
+ :nodes => @nodes,
98
+ :conf => conf
99
+ }
100
+ Fluent::SnmpInput.new.out_exec(manager, opts)
101
+ end
102
+ end
87
103
  end
88
104
 
89
105
  def starter
@@ -102,35 +118,69 @@ module Fluent
102
118
 
103
119
  def run
104
120
  Polling::run(@polling_time) do
105
- snmpwalk(@manager, @mib, @nodes)
106
121
  break if @end_flag
122
+ exec_params = {
123
+ manager: @manager,
124
+ mib: @mib,
125
+ nodes: @nodes,
126
+ method_type: @method_type
127
+ }
128
+ exec_snmp(exec_params)
107
129
  end
108
130
  rescue TypeError => ex
109
131
  $log.error "run TypeError", :error=>ex.message
110
- exit
111
132
  rescue => ex
112
- $log.error "run failed", :error=>ex.message
113
- sleep(10)
114
- @retry_conut += 1
115
- retry if @retry_conut < 30
133
+ $log.fatal "run failed", :error=>ex.inspect
134
+ $log.fatal_backtrace ex.backtrace
135
+ exit
116
136
  end
117
137
 
118
- #Ctrl-cで処理を停止時に呼ばれる
119
138
  def shutdown
120
139
  @end_flag = true
121
140
  @thread.run
122
141
  @thread.join
123
142
  @starter.join
124
143
  @manager.close
144
+ super
145
+ end
146
+
147
+ def exec_snmp opts={}
148
+ @retry_count ||= 0
149
+ if @out_exec_filter.nil?
150
+ case opts[:method_type]
151
+ when /^walk$/
152
+ snmp_walk(opts[:manager], opts[:mib], opts[:nodes])
153
+ when /^get$/
154
+ snmp_get(opts[:manager], opts[:mib], opts[:nodes])
155
+ else
156
+ $log.error "unknow exec method"
157
+ raise
158
+ end
159
+ else
160
+ @out_exec.call opts[:manager]
161
+ end
162
+ @retry_count = 0
163
+ rescue SNMP::RequestTimeout => ex
164
+ $log.error "exec_snmp failed", :error=>ex.inspect
165
+ @retry_count += 1
166
+ if @retry_count <= @retry
167
+ sleep @retry_interval
168
+ $log.error "retry: #{@retry_count}"
169
+ retry
170
+ else
171
+ raise ex
172
+ end
173
+ rescue => ex
174
+ raise ex
125
175
  end
126
176
 
127
177
  private
128
178
 
129
- def snmpwalk(manager, mib, nodes, test=false)
179
+ def snmp_walk(manager, mib, nodes, test=false)
130
180
  manager.walk(mib) do |row|
131
181
  time = Engine.now
132
182
  time = time - time % 5
133
- record = Hash.new
183
+ record = {}
134
184
  row.each do |vb|
135
185
  if nodes.nil?
136
186
  record["value"] = vb
@@ -142,10 +192,27 @@ module Fluent
142
192
  end
143
193
  end
144
194
  rescue => ex
145
- $log.error "snmpwalk failed", :error=>ex.message
195
+ raise ex
196
+ end
197
+
198
+ def snmp_get(manager, mib, nodes, test=false)
199
+ manager.get(mib).each_varbind do |vb|
200
+ time = Engine.now
201
+ time = time - time % 5
202
+ record = {}
203
+ if nodes.nil?
204
+ record["value"] = vb
205
+ else
206
+ nodes.each{|param| record[param] = check_type(vb.__send__(param))}
207
+ end
208
+ Engine.emit(@tag, time, record)
209
+ return {:time => time, :record => record} if test
210
+ end
211
+ rescue => ex
212
+ raise ex
146
213
  end
147
214
 
148
- # SNMPで取得したデータの型チェック
215
+ # data check from snmp
149
216
  def check_type(value)
150
217
  if value =~ /^\d+(\.\d+)?$/
151
218
  return value.to_f
@@ -153,9 +220,8 @@ module Fluent
153
220
  return value.to_s
154
221
  end
155
222
  rescue => ex
156
- $Log.error "snmp failed to check_type", :error=>ex.message
223
+ $log.error "snmp failed to check_type", :error=>ex.message
157
224
  $log.warn_backtrace ex.backtrace
158
225
  end
159
-
160
226
  end
161
227
  end
@@ -0,0 +1,17 @@
1
+ module Fluent
2
+ class SnmpInput
3
+ def out_exec manager, opts={}
4
+ manager.walk(opts[:mib]) do |row|
5
+ time = Time.now.to_i
6
+ time = time - time % 5
7
+ record = {}
8
+ row.each do |vb|
9
+ record["name"] = vb.name.to_s
10
+ record["value"] = vb.value.to_s
11
+ Engine.emit opts[:tag], time, record
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,43 @@
1
+ <source>
2
+ type snmp
3
+ tag snmp.server1
4
+ nodes name, value
5
+ host localhost
6
+ community public
7
+ version 2c
8
+ mib hrStorageIndex, hrStorageDescr, hrStorageSize, hrStorageUsed
9
+ mib_modules HOST-RESOURCES-MIB
10
+ retries 0
11
+ timeout 3s
12
+ polling_time 0,10,20,30,40,50
13
+ </source>
14
+
15
+ #<source>
16
+ #type snmp
17
+ #tag snmp.server2
18
+ #host localhost
19
+ #community public
20
+ #version 2c
21
+ #mib hrStorageIndex, hrStorageDescr, hrStorageSize, hrStorageUsed
22
+ #mib_modules HOST-RESOURCES-MIB
23
+ #retries 0
24
+ #timeout 3s
25
+ #polling_time 5,15,25,35,45,55
26
+ #</source>
27
+ #
28
+ #<source>
29
+ #type snmp
30
+ #tag snmp.server3
31
+ #host localhost
32
+ #community public
33
+ #version 2c
34
+ #mib hrStorageIndex, hrStorageDescr, hrStorageSize, hrStorageUsed
35
+ #mib_modules HOST-RESOURCES-MIB
36
+ #retries 0
37
+ #timeout 3s
38
+ #polling_time 5m
39
+ #</source>
40
+
41
+ <match snmp.server*>
42
+ type stdout
43
+ </match>
@@ -15,10 +15,14 @@ class SnmpInputTest < Test::Unit::TestCase
15
15
  nodes name, value
16
16
  polling_time 0,10,20,30,40,50
17
17
  host localhost
18
- community private
18
+ host_name test_host
19
+ community public
19
20
  mib_modules HOST-RESOURCES-MIB, IF-MIB
20
- retries 0
21
+ retry 0
22
+ retry_interval 2
21
23
  timeout 3s
24
+ method_type walk
25
+ out_exec_filter sample/out_exec.rb
22
26
  ]
23
27
 
24
28
  def create_driver(conf=CONFIG)
@@ -33,16 +37,20 @@ class SnmpInputTest < Test::Unit::TestCase
33
37
  assert_equal ["hrStorageIndex","hrStorageDescr","hrStorageSize","hrStorageUsed"], d.instance.mib
34
38
  assert_equal ["name","value"], d.instance.nodes
35
39
  assert_equal ["0","10","20","30","40","50"], d.instance.polling_time
40
+ assert_equal "walk", d.instance.method_type
41
+ assert_equal 2, d.instance.retry_interval
42
+ assert_equal "sample/out_exec.rb", d.instance.out_exec_filter
36
43
 
37
44
  # SNMP Lib Params
38
45
  assert_equal "localhost", d.instance.host
46
+ assert_equal "test_host", d.instance.host_name
39
47
  assert_nil d.instance.port
40
48
  assert_nil d.instance.trap_port
41
- assert_equal "private", d.instance.community
49
+ assert_equal "public", d.instance.community
42
50
  assert_nil d.instance.write_community
43
51
  assert_equal :SNMPv2c, d.instance.version
44
52
  assert_equal 3, d.instance.timeout
45
- assert_equal 0, d.instance.retries
53
+ assert_equal 0, d.instance.retry
46
54
  assert_nil d.instance.transport
47
55
  assert_nil d.instance.max_recv_bytes
48
56
  assert_nil d.instance.mib_dir
@@ -57,9 +65,15 @@ class SnmpInputTest < Test::Unit::TestCase
57
65
  assert_equal "sensorValue_%RH", @obj.__send__(:check_type,"sensorValue_%RH")
58
66
  assert_equal 12.00, @obj.__send__(:check_type,"12")
59
67
  assert_equal 12.34, @obj.__send__(:check_type,"12.34")
68
+ assert_equal String, @obj.__send__(:check_type,"test").class
69
+ assert_equal String, @obj.__send__(:check_type,"utrh0").class
70
+ assert_equal String, @obj.__send__(:check_type,"sensorValue_degC").class
71
+ assert_equal String, @obj.__send__(:check_type,"sensorValue_%RH").class
72
+ assert_equal Float, @obj.__send__(:check_type,"12").class
73
+ assert_equal Float, @obj.__send__(:check_type,"12.34").class
60
74
  end
61
75
 
62
- def test_snmpwalk
76
+ def test_snmp_walk
63
77
  d = create_driver
64
78
  nodes = d.instance.nodes
65
79
  mib = d.instance.mib
@@ -77,12 +91,71 @@ class SnmpInputTest < Test::Unit::TestCase
77
91
  Time.stubs(:now).returns(Time.parse "2012/12/31 23:59:50")
78
92
  manager = SNMP::Manager.new(snmp_init_params)
79
93
 
80
- data = @obj.__send__(:snmpwalk, manager, mib, nodes, true)
94
+ data = @obj.__send__(:snmp_walk, manager, mib, nodes, true)
81
95
  record = data[:record]
82
96
 
83
97
  assert_equal 1356965990, data[:time]
84
- assert_equal "HOST-RESOURCES-MIB::hrStorageIndex.1", record["name"]
85
- assert_equal "1", record["value"]
98
+ assert_equal "HOST-RESOURCES-MIB::hrStorageIndex.31", record["name"]
99
+ assert_equal "31", record["value"]
86
100
  end
87
101
 
102
+
103
+ def test_snmp_get
104
+ d = create_driver %[
105
+ tag snmp.server1
106
+ mib hrStorageIndex.31
107
+ nodes name, value
108
+ polling_time 0,10,20,30,40,50
109
+ host localhost
110
+ community public
111
+ mib_modules HOST-RESOURCES-MIB
112
+ ]
113
+ nodes = d.instance.nodes
114
+ mib = d.instance.mib
115
+
116
+ snmp_init_params = {
117
+ :host => d.instance.host,
118
+ :community => d.instance.community,
119
+ :timeout => d.instance.timeout,
120
+ :retries => d.instance.retries,
121
+ :mib_dir => d.instance.mib_dir,
122
+ :mib_modules => d.instance.mib_modules,
123
+ }
124
+
125
+ # unixtime 1356965990
126
+ Time.stubs(:now).returns(Time.parse "2012/12/31 23:59:50")
127
+ manager = SNMP::Manager.new(snmp_init_params)
128
+
129
+ data = @obj.__send__(:snmp_get, manager, mib, nodes, true)
130
+ record = data[:record]
131
+
132
+ assert_equal 1356965990, data[:time]
133
+ assert_equal "HOST-RESOURCES-MIB::hrStorageIndex.31", record["name"]
134
+ assert_equal "31", record["value"]
135
+ end
136
+
137
+ def test_exec_snmp
138
+ d = create_driver
139
+ snmp_init_params = {
140
+ :host => d.instance.host,
141
+ :community => d.instance.community,
142
+ :timeout => d.instance.timeout,
143
+ :retries => d.instance.retries,
144
+ :mib_dir => d.instance.mib_dir,
145
+ :mib_modules => d.instance.mib_modules,
146
+ }
147
+
148
+ manager = SNMP::Manager.new(snmp_init_params)
149
+
150
+ opts = {
151
+ :manager => manager,
152
+ :method_type => d.instance.method_type,
153
+ :mib => d.instance.mib,
154
+ :nodes => d.instance.nodes,
155
+ :test => true
156
+ }
157
+
158
+ exec = @obj.__send__(:exec_snmp, opts)
159
+ assert_equal 0, exec
160
+ end
88
161
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-snmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-21 00:00:00.000000000 Z
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
@@ -121,7 +121,8 @@ files:
121
121
  - Rakefile
122
122
  - fluent-plugin-snmp.gemspec
123
123
  - lib/fluent/plugin/in_snmp.rb
124
- - snmp.conf.sample
124
+ - sample/out_exec.rb.sample
125
+ - sample/snmp.conf.sample
125
126
  - test/helper.rb
126
127
  - test/plugin/test_in_snmp.rb
127
128
  homepage:
data/snmp.conf.sample DELETED
@@ -1,43 +0,0 @@
1
- <source>
2
- type snmp
3
- tag snmp.server1
4
- nodes name, value
5
- host localhost
6
- community private
7
- version 2c
8
- mib hrStorageIndex, hrStorageDescr, hrStorageSize, hrStorageUsed
9
- mib_modules HOST-RESOURCES-MIB
10
- retries 0
11
- timeout 3s
12
- polling_time 0,10,20,30,40,50
13
- </source>
14
-
15
- <source>
16
- type snmp
17
- tag snmp.server2
18
- host localhost
19
- community private
20
- version 2c
21
- mib hrStorageIndex, hrStorageDescr, hrStorageSize, hrStorageUsed
22
- mib_modules HOST-RESOURCES-MIB
23
- retries 0
24
- timeout 3s
25
- polling_time 5,15,25,35,45,55
26
- </source>
27
-
28
- <source>
29
- type snmp
30
- tag snmp.server3
31
- host localhost
32
- community private
33
- version 2c
34
- mib hrStorageIndex, hrStorageDescr, hrStorageSize, hrStorageUsed
35
- mib_modules HOST-RESOURCES-MIB
36
- retries 0
37
- timeout 3s
38
- polling_time 5m
39
- </source>
40
-
41
- <match snmp.server*>
42
- type stdout
43
- </match>