Class: Arpdb::Arp

Inherits:
Object
  • Object
show all
Defined in:
lib/arpdb/arp.rb

Overview

Class to fetch ARP tables from Juniper firewalls, maintain them in memory and perform search operations on the cached data.

Defined Under Namespace

Classes: ArpdbError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snmp_transports) ⇒ Arp

Oh gimme my syringe, I want to do some dependency injection! On a more serious note: snmp_transports is expected to be either Arpdb::SNMPTransport or array of those. This way we can easily pass mocked SNMPTransport here for off-the-hook testing.



16
17
18
19
# File 'lib/arpdb/arp.rb', line 16

def initialize(snmp_transports)
  @snmp_transports = Array(snmp_transports)
  @db = Array.new
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



10
11
12
# File 'lib/arpdb/arp.rb', line 10

def db
  @db
end

#snmp_transportsObject

Returns the value of attribute snmp_transports.



10
11
12
# File 'lib/arpdb/arp.rb', line 10

def snmp_transports
  @snmp_transports
end

Instance Method Details

#ip_to_mac(ip) ⇒ Object



53
54
55
# File 'lib/arpdb/arp.rb', line 53

def ip_to_mac(ip)
  dblookup(:ip, ip, :mac)
end

#locate_ip(ip) ⇒ Object



61
62
63
# File 'lib/arpdb/arp.rb', line 61

def locate_ip(ip)
  dblookup(:ip, ip, :location)
end

#locate_mac(mac) ⇒ Object



57
58
59
# File 'lib/arpdb/arp.rb', line 57

def locate_mac(mac)
  dblookup(:mac, mac_flatten(mac), :location)
end

#mac_to_ip(mac) ⇒ Object



49
50
51
# File 'lib/arpdb/arp.rb', line 49

def mac_to_ip(mac)
  dblookup(:mac, mac_flatten(mac), :ip)
end

#rescanObject



21
22
23
# File 'lib/arpdb/arp.rb', line 21

def rescan
  scan
end

#scanObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/arpdb/arp.rb', line 25

def scan
  sys_location = '1.3.6.1.2.1.1.6.0'
  ip_net_to_media_phys_address = '1.3.6.1.2.1.4.22.1.2'
  ip_net_to_media_net_address = '1.3.6.1.2.1.4.22.1.3'
  @db = Array.new
  handle_exceptions do
    dirty = false
    snmp_transports.each do |st|
      begin
        st.walk([ip_net_to_media_phys_address, ip_net_to_media_net_address]).each do |mac, ip|
          @db << {mac: mac, ip: ip, host: st.host, location: st.get(sys_location)}
        end
      rescue => e
        dirty = true
        msgs ||= []
        msgs << "SNMPTransport error while scanning #{st.host}; moving to next host (if any)"
        next
      end
    end
    raise msgs.join("; ") if dirty
  end
  self
end