Class: MMTop::Topology

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Topology

Returns a new instance of Topology.



7
8
9
# File 'lib/mmtop/filters/map_topology.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#create_sort_array(t) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mmtop/filters/map_topology.rb', line 43

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mmtop/filters/map_topology.rb', line 55

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mmtop/filters/map_topology.rb', line 84

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



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mmtop/filters/map_topology.rb', line 28

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mmtop/filters/map_topology.rb', line 11

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mmtop/filters/map_topology.rb', line 71

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