Class: MMTop::Topology

Inherits:
Object
  • Object
show all
Defined in:
lib/mmtop/filters/map_topology.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Topology

Returns a new instance of Topology.



15
16
17
# File 'lib/mmtop/filters/map_topology.rb', line 15

def initialize(config)
  @config = config
end

Class Method Details

.discover(config) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mmtop/filters/map_topology.rb', line 7

def self.discover(config)
  if !config.options['topology.discovered']
    config.options['topology.discovered'] = true
    config.hosts = MMTop::Topology.new(config).new_hostlist
  end
  config
end

Instance Method Details

#create_sort_array(t) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mmtop/filters/map_topology.rb', line 51

def create_sort_array(t)
  array = []
  t.values.select { |v|
    v[:levels] == 0
  }.sort_by { |v|
    v[:hostname]
  }.each { |v|
    insert_host_into_sort_array(t, v, array)
  }
  array
end

#fill_chain_info(host, topology) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mmtop/filters/map_topology.rb', line 63

def fill_chain_info(host, topology)
  levels = 0
  stack = []
  master = host
  while master = topology[master[:master]]
    # loop detection
    break if stack.include?(master)

    last_master = master
    levels += 1
    stack.push(master)
  end

  host[:levels] = levels
end

#find_master_slaveObject



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
# File 'lib/mmtop/filters/map_topology.rb', line 92

def find_master_slave
  @config.hosts.each { |host|
    host.ip = resolve_to_ip(host.name)
  }

  topology = @config.hosts.inject({}) { |accum, h|
    next unless h.ip

    status = h.slave_status

    if status && status[:Master_User] != 'test'
      master_host = status[:Master_Host]
    end

    master_host = resolve_to_ip(master_host)

    accum[h.ip] = {:master => master_host, :hostname => h.name, :ip => h.ip}
    accum
  }

  # fill in :is_master
  topology.each { |k, v|
    master_top = topology[v[:master]]
    if master_top
      master_top[:is_master] = 1
    end
  }
  topology
end

#insert_host_into_sort_array(t, host, array) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mmtop/filters/map_topology.rb', line 36

def insert_host_into_sort_array(t, host, array)
  array << host
  t.select { |k, v|
    # find hosts who are our slaves
    v[:master] == host[:ip]
  }.sort_by { |k, v|
    # add those without children of their own first
    v[:is_master].to_i
  }.each { |k, s|
    insert_host_into_sort_array(t, s, array)
  }
  array
end

#new_hostlistObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mmtop/filters/map_topology.rb', line 19

def new_hostlist
  topology = find_master_slave
  topology.each { |name, t| fill_chain_info(t, topology) }

  new_top = create_sort_array(topology)

  hosts = @config.hosts.sort_by { |h| new_top.find_index { |t| t[:hostname] == h.name } || 1_000_000 }
  hosts.each { |h|
    top = new_top.find { |t| t[:hostname] == h.name }
    next unless top
    if top[:levels] > 0
      h.display_name = ("  " * top[:levels]) + '\_' + h.name
    end
  }
  hosts
end

#resolve_to_ip(hostname) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mmtop/filters/map_topology.rb', line 79

def resolve_to_ip(hostname)
  return nil if hostname.nil?
  return hostname if hostname =~ /\d+\.\d+\.\d+\.\d+\./

  begin
    arr = Socket::gethostbyname(hostname)
  rescue 
    return nil
  end

  arr && arr.last.unpack("CCCC").join(".")
end