Class: Corona::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/corona/core.rb

Instance Method Summary collapse

Instance Method Details

#mkrecord(opt) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/corona/core.rb', line 74

def mkrecord opt
  {
    :ip              => opt[:ip].to_s,
    :ptr             => ip2name(opt[:ip].to_s),
    :model           => Model.map(opt[:oids][:sysDescr], opt[:oids][:sysObjectID]),
    :oid_ifDescr     => opt[:int],
    :oid_sysName     => opt[:oids][:sysName],
    :oid_sysLocation => opt[:oids][:sysLocation],
    :oid_sysDescr    => opt[:oids][:sysDescr],
    :oid_sysObjectID => opt[:oids][:sysObjectID].join('.'),
  }
end

#poll(ip) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/corona/core.rb', line 54

def poll ip
  result = nil
  snmp = SNMP.new ip.to_s, @community
  oids = snmp.dbget
  if oids
    result = {:oids=>oids, :ip=>ip, :int=>'n/a'}
    if index = snmp.ip2index(ip.to_s)
      if int = snmp.ifdescr(index)
        result[:int] = int.downcase
      else
        Log.debug "no ifDescr for #{index} at #{ip}"
      end
    else
      Log.debug "no ifIndex for #{ip}"
    end
  end
  snmp.close
  result
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/corona/core.rb', line 26

def run
  cidr          = @opts.delete :cidr
  @output       = @opts.delete :output
  if not @output
    @output = Logger.new $stdout
    @output.formatter = proc { |_ ,_ ,_ , msg| msg + "\n" }
  end
  poll, ignores = resolve_networks cidr
  @mutex        = Mutex.new
  @db           = DB.new
  threads       = []
  Thread.abort_on_exception = true
  poll.each do |net|
    net.to_range.each do |ip|
      next if ignores.any? { |ignore| ignore.include? ip }
      while threads.size >= CFG.threads
        threads.delete_if { |thread| not thread.alive? }
        sleep 0.01
      end
      threads << Thread.new do
        result = poll ip
        @mutex.synchronize { process result } if result
      end
    end
  end
  threads.each { |thread| thread.join }
end