Class: Netdisco::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/netdisco/output.rb,
lib/netdisco/output/dot.rb

Defined Under Namespace

Classes: Dot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(peers, poll_map) ⇒ Output

对象初始化入口函数 将邻居发现清单以及本地的轮询列表清单比对后,格式化输出相关数据



10
11
12
13
14
# File 'lib/netdisco/output.rb', line 10

def initialize(peers, poll_map)
  @peers    = peers
  @poll_map = poll_map
  @resolve  = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

动态方法

Raises:

  • (NoMethodError)


17
18
19
20
21
22
23
# File 'lib/netdisco/output.rb', line 17

def method_missing(name, *args)
  raise NoMethodError, "invalid method #{name} for #{inspect}:#{self.class}" unless name.match?(/to_.*/)
  output = File.basename name[3..-1]
  require_relative "output/" + output
  output = Netdisco::Output.const_get output.capitalize
  output.send :output, self
end

Instance Attribute Details

#peersObject (readonly)

类对象属性



6
7
8
# File 'lib/netdisco/output.rb', line 6

def peers
  @peers
end

#poll_mapObject (readonly)

类对象属性



6
7
8
# File 'lib/netdisco/output.rb', line 6

def poll_map
  @poll_map
end

#resolveObject (readonly)

类对象属性



6
7
8
# File 'lib/netdisco/output.rb', line 6

def resolve
  @resolve
end

Instance Method Details

#clean!void

This method returns an undefined value.

自动移除非探测名单范围内的设备 remove peers not matching to configured CIDR



42
43
44
45
46
# File 'lib/netdisco/output.rb', line 42

def clean!
  @peers.values.each do |peers|
    peers.delete_if { |peer| not @poll_map.include? peer.ip }
  end
end

#resolve!void

This method returns an undefined value.

自动更新设备主机名,并更新解析状态 resolves ip addresses of peers and @peers keys



28
29
30
31
32
33
34
35
36
37
# File 'lib/netdisco/output.rb', line 28

def resolve!
  @resolve  = true
  new_peers = {}
  @peers.each do |host, peers|
    peers.each { |peer| peer.name }
    name            = DNS.getname host
    new_peers[name] = peers
  end
  @peers = new_peers
end

#to_aArray

Returns of nodes found.

Returns:

  • (Array)

    of nodes found



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/netdisco/output.rb', line 62

def to_a
  nodes = []
  @peers.each do |host, peers|
    nodes << host
    peers.each do |peer|
      nodes << @resolve ? peer.name : peer.ip
    end
  end
  # 将 nodes 展开为属组并去重排序
  nodes.flatten.uniq
end

#to_hHash

Returns of nodes found.

Returns:

  • (Hash)

    of nodes found



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/netdisco/output.rb', line 49

def to_h
  hash = {}
  @peers.each do |host, peers|
    array = []
    peers.each do |peer|
      array << peer.to_h
    end
    hash[host] = array
  end
  hash
end

#to_hashString

Returns pretty print of hash.

Returns:

  • (String)

    pretty print of hash



75
76
77
78
79
80
# File 'lib/netdisco/output.rb', line 75

def to_hash
  require "pp"
  out = ""
  PP.pp(to_h, out)
  out
end

#to_jsonString

Returns json of hosts and peers found.

Returns:

  • (String)

    json of hosts and peers found



90
91
92
93
94
# File 'lib/netdisco/output.rb', line 90

def to_json
  require "json"
  JSON.generate(to_h)
  # JSON.pretty_generate to_h
end

#to_yamlString, Instance

Returns yaml of hosts and peers found.

Returns:

  • (String, Instance)

    yaml of hosts and peers found



83
84
85
86
87
# File 'lib/netdisco/output.rb', line 83

def to_yaml
  require "yaml"
  File.open("./neighbors.yml", "w") { |file| file.write to_h.to_yaml }
  YAML.dump(to_h)
end