Class: Snmp2mkr::Discoverer

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp2mkr/discoverer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, host_manager: nil, logger: Logger.new(File::NULL), mib: Mib.default) ⇒ Discoverer

Returns a new instance of Discoverer.



9
10
11
12
13
14
# File 'lib/snmp2mkr/discoverer.rb', line 9

def initialize(host, host_manager: nil, logger: Logger.new(File::NULL), mib: Mib.default)
  @host = host
  @host_manager = host_manager
  @logger = logger
  @mib = mib
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/snmp2mkr/discoverer.rb', line 20

def host
  @host
end

#host_managerObject (readonly)

Returns the value of attribute host_manager.



20
21
22
# File 'lib/snmp2mkr/discoverer.rb', line 20

def host_manager
  @host_manager
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/snmp2mkr/discoverer.rb', line 20

def logger
  @logger
end

#mibObject (readonly)

Returns the value of attribute mib.



20
21
22
# File 'lib/snmp2mkr/discoverer.rb', line 20

def mib
  @mib
end

Instance Method Details

#inspectObject



16
17
18
# File 'lib/snmp2mkr/discoverer.rb', line 16

def inspect
  "#<#{self.class}:#{'%x' % __id__}, #{host.name.inspect}>"
end

#perform!Object



22
23
24
25
26
27
28
# File 'lib/snmp2mkr/discoverer.rb', line 22

def perform!
  if host_manager
    host_manager.set_vhosts(host.name, vhosts)
  end

  vhosts
end

#primary_vhostObject



37
38
39
40
41
42
43
44
# File 'lib/snmp2mkr/discoverer.rb', line 37

def primary_vhost
  @primary_vhost ||= Vhost.new(
    name: host.name,
    roles: host.definition.roles,
    metrics: host.metric_definitions + discover_metrics(host.metric_discoveries),
    mib: mib
  )
end

#snmp_treesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/snmp2mkr/discoverer.rb', line 46

def snmp_trees
  @snmp_trees ||= host.snmp do |snmp|
    oids = [*host.metric_discoveries.values, *host.vhost_discoveries.values].
      compact.
      flat_map { |_| _.keys.values.map(&:evaluate) }.
      uniq.
      map { |_| Snmp2mkr::Oid.new(mib.name_to_oid(_), name: _) }.
      uniq.
      sort_by { |_| -_.to_a.size }

    oids.map do |oid|
      [oid.to_s, snmp.subtree(oid).to_a]
    end.to_h
  end
end

#vhostsObject



30
31
32
33
34
35
# File 'lib/snmp2mkr/discoverer.rb', line 30

def vhosts
  @vhosts ||= [
    primary_vhost,
    *discover_vhosts(host.vhost_discoveries),
  ]
end