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
23
24
# 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[:connect_timeout] = options['connect_timeout'] || 1
  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']
  @hide = false

  initialize_mysql2_cx(m2opts)
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



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

def comment
  @comment
end

#display_nameObject

Returns the value of attribute display_name.



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

def display_name
  @display_name
end

#hideObject

Returns the value of attribute hide.



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

def hide
  @hide
end

#ipObject

Returns the value of attribute ip.



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

def ip
  @ip
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#dead?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/mmtop/host.rb', line 74

def dead?
  @dead
end

#hide_if_empty?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/mmtop/host.rb', line 43

def hide_if_empty?
  !!@hide_if_empty
end

#hostinfoObject



109
110
111
# File 'lib/mmtop/host.rb', line 109

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

#initialize_mysql2_cx(m2opts) ⇒ Object



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

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



70
71
72
# File 'lib/mmtop/host.rb', line 70

def mark_dead!
  @dead = true
end

#processlistObject



103
104
105
106
107
# File 'lib/mmtop/host.rb', line 103

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

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

#query(q) ⇒ Object



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

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



78
79
80
81
82
83
# File 'lib/mmtop/host.rb', line 78

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



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

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)


85
86
87
# File 'lib/mmtop/host.rb', line 85

def wedge_monitor?
  @options['wedge_monitor']
end