Class: GRI::SNMPCollector
Constant Summary
collapse
- SYSOIDS =
BER.enc_oid_list(['sysDescr', 'sysName', 'sysObjectID',
'sysUpTime', 'sysLocation'].map {|n|
oid = SNMP::OIDS[n] + '.0'
SNMP.add_oid n + '.0', oid
oid})
Constants inherited
from Collector
Collector::TYPES
Instance Attribute Summary
Attributes inherited from Collector
#attached_at, #host, #interval, #loop, #options, #timeout
Instance Method Summary
collapse
Methods inherited from Collector
#attach, create, #initialize, #on_error, #on_init, #on_retry
Constructor Details
This class inherits a constructor from GRI::Collector
Instance Method Details
#get(varbind, &cb) ⇒ Object
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/gri/collector.rb', line 259
def get varbind, &cb
@results.clear
@retry_count = 0
@poll_cb = cb
@snmp.connect unless @snmp.sock
@snmp.get_start(varbind) {|*res| @results.push res}
send_req varbind
rescue SocketError
@loop.detach self
@on_error.call if @on_error
Log.error "#{host}: error"
end
|
#key_encode(s) ⇒ Object
181
182
183
184
|
# File 'lib/gri/collector.rb', line 181
def key_encode s
s.to_s.gsub(/[:\/]/, '-').gsub(/[ =]/, '_').gsub(/[^-a-zA-Z0-9_.]/) { "%02X"%$&.unpack('C').first}
end
|
#on_attach ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/gri/collector.rb', line 73
def on_attach
now = Time.now
now_i = now.to_i
@attached_at = now
@results.clear
@tout = 5
@preq = nil
@snmp = SNMP.new(@cname || @host, options['port'])
if options['fake-snmp']
require 'gri/fake_snmp'
@snmp = FakeSNMP.new options['fake-snmp']
else
@snmp.version = options['ver'] if options['ver']
@snmp. = options['community'] if options['community']
end
varbind = SYSOIDS
get(varbind) {|results|
sysinfo = {'_host'=>(@hostname || host),
'_key'=>'SYS', '_time'=>now_i, '_mtime'=>now_i}
results.each {|enoid, tag, val|
(k = SNMP::ROIDS[enoid]) and sysinfo[k.sub(/\.0$/, '')] = val
}
sysinfo['sysDescr'] and sysinfo['sysDescr'].gsub!("\n", ' ')
vendor = Vendor.check sysinfo, options
if $debug
puts "snmp: #{host} (#{vendor.name}): #{sysinfo['sysDescr']}"
puts vendor.options.inspect
end
@snmp.version = vendor.options['ver'] if vendor.options['ver']
punits = vendor.get_punits
punits_d = punits.dup
@workhash = punits.inject({}) {|h, pu| h[pu.cat] = {}; h}
poll1(punits) {
if !options.has_key?('SYS') or options['SYS']
sysinfo.delete 'sysObjectID'
records = [sysinfo]
else
records = []
end
hfdh = @fake_descr_hash[host]
@workhash[:interfaces] = @workhash[''] if $debug and ($debug['workhash'] or $debug['workhash0'])
puts " workhash0"
show_workhash @workhash
end
for pu in punits_d
pu.options = vendor.options
join_cat, join_key = pu.defs[:join]
if join_cat
for cat, wh in @workhash[pu.cat]
if wh and @workhash[join_cat]
join_hash = @workhash[join_cat][wh[join_key]]
wh.merge! join_hash if join_hash
end
end
end
puts " fix_workhash #{pu.class} #{pu.name}" if $debug
pu.fix_workhash @workhash
end
@workhash.delete :interfaces if $debug and ($debug['workhash'] or $debug['workhash1'])
puts " workhash1"
show_workhash @workhash
end
for cat, wh in @workhash
if (specs = DEFS.get_specs cat)
(index_key = specs[:index_key] || specs[:named_index])
else
specs = {}
end
ign_proc = specs[:ignore?]
for ind, h in wh
h['_host'] = @hostname || host
h['_key'] = if index_key
next if h[index_key].to_s.empty?
"#{cat}_#{key_encode h[index_key]}"
elsif ind.kind_of? Integer
"#{cat}#{ind}"
elsif ind.kind_of? Array
"#{cat}_#{ind.join(',')}"
else
"#{cat}_#{ind}"
end
h['_interval'] = @interval
h['_time'] = now_i
if hfdh and (f_descr = hfdh[h['_key']]) and
(prop = specs[:prop]) and (descr_k = prop[:description])
h[descr_k] = f_descr
end
next if ign_proc and ign_proc.call(h)
records.push h
end
end
@cb.call records
@loop.detach self
}
}
end
|
#on_detach ⇒ Object
177
178
179
|
# File 'lib/gri/collector.rb', line 177
def on_detach
@snmp.close rescue nil
end
|
#on_readable(io = nil) ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/gri/collector.rb', line 310
def on_readable io=nil
msg = @snmp.sock.recv 2000, 0
arg = @snmp.recv_msg msg
if arg
@snmp.state = :SUCCESS if @snmp.state == :GET_REQ
end
if @snmp.state == :SUCCESS
@poll_cb.call @results
@preq = nil
elsif @snmp.state != :IDLE
send_req arg if arg
end
rescue SystemCallError
@snmp.state = :IDLE
Log.error "#{@host}: ERROR: #{$!}"
@loop.detach self
@on_error.call if @on_error
end
|
#on_timeout ⇒ Object
329
330
331
332
|
# File 'lib/gri/collector.rb', line 329
def on_timeout
Log.error "#{@host}: ERROR: timeout"
@on_error.call if @on_error
end
|
#on_writable(io = nil) ⇒ Object
302
303
304
305
306
307
308
|
# File 'lib/gri/collector.rb', line 302
def on_writable io=nil
unless @buffers.empty?
@snmp.sock.send @buffers.join(''), 0
@buffers.clear
loop.watch @snmp.sock, :r, @tout, self
end
end
|
#poll1(punits, &cb) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/gri/collector.rb', line 186
def poll1 punits, &cb
if punits.empty?
cb.call
else
pu = punits.shift
@get_oid_buf = []
oids = pu.oids.dup
puts " poll #{host} #{pu.name}" if $debug
walk1(pu, oids) {poll1 punits, &cb}
end
end
|
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/gri/collector.rb', line 285
def retry
if (@retry_count += 1) > 3
@loop.detach self
@on_error.call if @on_error
Log.error "#{host}: error"
elsif @preq
Log.warn "#{host}: retry #{@retry_count}"
@on_retry.call if @on_retry
@buffers.push @preq.last
loop.watch @snmp.sock, :w, @tout, self
else
@loop.detach self
@on_error.call if @on_error
Log.error "#{host}: cannot retry: #{@retry_count}"
end
end
|
#send_req(arg) ⇒ Object
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/gri/collector.rb', line 272
def send_req arg
@preq = [@snmp.state, arg]
if (s = @snmp.make_req(@snmp.state, arg))
if @retry_count > 0
Log.debug "retry send_req, #{@buffers.size}"
end
@buffers.push s
loop.watch @snmp.sock, :w, @tout, self
else
on_readable
end
end
|
#show_results(req_enoid, results) ⇒ Object
236
237
238
239
240
241
242
243
244
|
# File 'lib/gri/collector.rb', line 236
def show_results req_enoid, results
puts " req #{SNMP.enoid2name req_enoid}"
for enoid, tag, val in results
a = BER.dec_oid enoid
s = a[0..-2].join('.')
oid_ind = BER.enc_v_oid s
puts " res #{SNMP.enoid2name(enoid)} #{tag} #{val.inspect}"
end
end
|
#show_workhash(workhash) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/gri/collector.rb', line 224
def show_workhash workhash
for cat, wh in workhash
puts " cat: #{cat.inspect}"
for ind, h in wh
puts " index: #{ind}"
for k, v in h
puts " #{k}: #{v.inspect}"
end
end
end
end
|
#sync? ⇒ Boolean
71
|
# File 'lib/gri/collector.rb', line 71
def sync?; false; end
|
#walk(enoid, &cb) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/gri/collector.rb', line 246
def walk enoid, &cb
@results.clear
@retry_count = 0
@poll_cb = cb
@snmp.connect unless @snmp.sock
@snmp.walk_start(enoid) {|*res| @results.push res}
send_req enoid
rescue SocketError
@loop.detach self
@on_error.call if @on_error
Log.error "#{host}: error"
end
|
#walk1(pu, oids, &cb) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/gri/collector.rb', line 198
def walk1 pu, oids, &cb
wh = @workhash[pu.cat]
if oids.empty?
if @get_oid_buf.empty?
cb.call
else
varbind = BER.cat_enoid @get_oid_buf
get(varbind) {|results|
results.each {|enoid, tag, val| pu.feed wh, enoid, tag, val}
cb.call
}
end
else
if (req_enoid = oids.shift) and req_enoid.getbyte(-1) == 0
@get_oid_buf.push req_enoid
walk1 pu, oids, &cb
else
walk(req_enoid) {|results|
show_results req_enoid, results if $debug and $debug['walk']
results.each {|enoid, tag, val| pu.feed wh, enoid, tag, val}
walk1 pu, oids, &cb
}
end
end
end
|