Class: MMTop::Host

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, user, password, options) ⇒ Host

Returns a new instance of Host.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mmtop/host.rb', line 5

def initialize(hostname, user, password, options)
  m2opts = {}
  m2opts[:host] = hostname
  m2opts[:username] = user
  m2opts[:password] = password
  m2opts[:socket] = options['socket'] if options['socket']
  m2opts[:port] = options['port'] if options['port']
  m2opts[:reconnect] = true
  @options = options
  @name = hostname
  @display_name = @name
  @comment = options['comment']
  @last_queries = nil
  @port = options['port'] || 3306
  @hide_if_empty = options['hide_if_empty']

  initialize_mysql2_cx(m2opts)
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



38
39
40
# File 'lib/mmtop/host.rb', line 38

def comment
  @comment
end

#display_nameObject

Returns the value of attribute display_name.



38
39
40
# File 'lib/mmtop/host.rb', line 38

def display_name
  @display_name
end

#ipObject

Returns the value of attribute ip.



38
39
40
# File 'lib/mmtop/host.rb', line 38

def ip
  @ip
end

#nameObject

Returns the value of attribute name.



38
39
40
# File 'lib/mmtop/host.rb', line 38

def name
  @name
end

#optionsObject

Returns the value of attribute options.



38
39
40
# File 'lib/mmtop/host.rb', line 38

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



39
40
41
# File 'lib/mmtop/host.rb', line 39

def port
  @port
end

Instance Method Details

#dead?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/mmtop/host.rb', line 72

def dead?
  @dead
end

#hide_if_empty?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mmtop/host.rb', line 41

def hide_if_empty?
  !!@hide_if_empty
end

#hostinfoObject



107
108
109
# File 'lib/mmtop/host.rb', line 107

def hostinfo
  HostInfo.new(self, processlist, slave_status, stats)
end

#initialize_mysql2_cx(m2opts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mmtop/host.rb', line 24

def initialize_mysql2_cx(m2opts)
  begin
    @mysql = Mysql2::Client.new(m2opts)
  rescue Mysql2::Error => e
    if e.error_number == 2003
      mark_dead!
    else
      $stdout.puts("Got Error Number #{e.error_number} (#{e.inspect}) trying to connect to #{@name}")
      mark_dead!
      sleep(1)
    end
  end
end

#mark_dead!Object



68
69
70
# File 'lib/mmtop/host.rb', line 68

def mark_dead!
  @dead = true
end

#processlistObject



101
102
103
104
105
# File 'lib/mmtop/host.rb', line 101

def processlist
  processlist = query("show full processlist")

  processlist.map { |r| Process.new(r, self) }
end

#query(q) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mmtop/host.rb', line 45

def query(q)
  return [] if dead?

  res = []
  begin
    ret = @mysql.query(q)
  rescue Mysql2::Error => e
    if [2007, 2013, 2003].include?(e.error_number)
      mark_dead!
      return []
    else
      puts "Got error number " + e.error_number.to_s + " querying #{@name}"
      raise e
    end
  end

  return nil unless ret
  ret.each(:symbolize_keys => true) do |r|
    res << r
  end
  res
end

#slave_statusObject



76
77
78
79
80
81
# File 'lib/mmtop/host.rb', line 76

def slave_status
  return nil if @options['expect_slave'] == false
  res = query("show slave status")[0]
  return nil if res && res[:Master_User] == 'test'
  res
end

#statsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mmtop/host.rb', line 87

def stats
  stats = {}
  row = query("show global status like 'Questions'")
  return {} if row.empty?

  queries = row.first[:Value].to_i

  @qps ||= MMTop::QPS.new
  @qps.add_sample(queries, Time.now)
  stats[:qps] = @qps.calc.to_i

  stats
end

#wedge_monitor?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/mmtop/host.rb', line 83

def wedge_monitor?
  @options['wedge_monitor']
end