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
Instance Attribute Summary
Attributes inherited from Collector
#attached_at, #host, #interval, #loop, #options, #timeout
Instance Method Summary collapse
- #get(varbind, &cb) ⇒ Object
- #key_encode(s) ⇒ Object
- #on_attach ⇒ Object
- #on_detach ⇒ Object
- #on_readable(io = nil) ⇒ Object
- #on_timeout ⇒ Object
- #on_writable(io = nil) ⇒ Object
- #poll1(punits, &cb) ⇒ Object
- #retry ⇒ Object
- #send_req(arg) ⇒ Object
- #show_results(req_enoid, results) ⇒ Object
- #show_workhash(workhash) ⇒ Object
- #sync? ⇒ Boolean
- #walk(enoid, &cb) ⇒ Object
- #walk1(pu, oids, &cb) ⇒ Object
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
271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/gri/collector.rb', line 271 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 => e @loop.detach self @on_error.call if @on_error Log.error "#{host}: error: #{e.to_s}" end |
#key_encode(s) ⇒ Object
185 186 187 188 |
# File 'lib/gri/collector.rb', line 185 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 176 177 178 179 |
# 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, ['port']) if ['fake-snmp'] require 'gri/fake_snmp' @snmp = FakeSNMP.new ['fake-snmp'] else @snmp.version = ['ver'] if ['ver'] @snmp.community = ['community'] if ['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, if $debug puts "snmp: #{host} (#{vendor.name}): #{sysinfo['sysDescr']}" puts vendor..inspect printf " elapsed %f sec\n", Time.now - @attached_at end @snmp.version = vendor.['ver'] if vendor.['ver'] punits = vendor.get_punits punits_d = punits.dup @workhash = punits.inject({}) {|h, pu| h[pu.cat] = {}; h} poll1(punits) { printf " elapsed %f sec\n", Time.now - @attached_at if $debug if !.has_key?('SYS') or ['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. = vendor. 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 printf " elapsed %f sec\n", Time.now - @attached_at if $debug @cb.call records @loop.detach self printf " elapsed %f sec\n", Time.now - @attached_at if $debug } } end |
#on_detach ⇒ Object
181 182 183 |
# File 'lib/gri/collector.rb', line 181 def on_detach @snmp.close rescue nil end |
#on_readable(io = nil) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/gri/collector.rb', line 328 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 @preq = nil @poll_cb.call @results 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
347 348 349 350 |
# File 'lib/gri/collector.rb', line 347 def on_timeout Log.error "#{@host}: ERROR: timeout" @on_error.call if @on_error end |
#on_writable(io = nil) ⇒ Object
320 321 322 323 324 325 326 |
# File 'lib/gri/collector.rb', line 320 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
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/gri/collector.rb', line 190 def poll1 punits, &cb if punits.empty? cb.call else pu = punits.shift @get_oid_buf = [] oids = pu.oids.dup if $debug t = Time.now puts " poll #{host} #{pu.name}" if $debug['walk'] end walk1(pu, oids) { if $debug and !$debug['walk'] printf " poll #{host} #{pu.name} (%f sec)\n", Time.now - t end poll1 punits, &cb } end end |
#retry ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/gri/collector.rb', line 297 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 if (s = @snmp.make_req(@snmp.state, @preq.last)) @buffers.push s 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 else @loop.detach self @on_error.call if @on_error Log.error "#{host}: cannot retry: #{@retry_count}" end end |
#send_req(arg) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/gri/collector.rb', line 284 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
248 249 250 251 252 253 254 255 256 |
# File 'lib/gri/collector.rb', line 248 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
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/gri/collector.rb', line 236 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
258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/gri/collector.rb', line 258 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 => e @loop.detach self @on_error.call if @on_error Log.error "#{host}: error: #{e.to_s}" end |
#walk1(pu, oids, &cb) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/gri/collector.rb', line 210 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 |