Class: WatchmonkeyCli::Checkers::MysqlReplication

Inherits:
WatchmonkeyCli::Checker show all
Defined in:
lib/watchmonkey_cli/checkers/mysql_replication.rb

Constant Summary

Constants included from Helper

Helper::BYTE_UNITS

Instance Attribute Summary

Attributes inherited from WatchmonkeyCli::Checker

#app

Instance Method Summary collapse

Methods inherited from WatchmonkeyCli::Checker

#_tolog, #blank_config, checker_name, checker_name=, #debug, descendants, #error, #info, inherited, #init, #initialize, #local, maxrt, maxrt=, #rsafe, #safe, #spawn_sub, #start, #stop

Methods included from Helper

#human_filesize, #human_number, #human_seconds

Constructor Details

This class inherits a constructor from WatchmonkeyCli::Checker

Instance Method Details

#_parse_response(res) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/watchmonkey_cli/checkers/mysql_replication.rb', line 38

def _parse_response res
  {}.tap do |r|
    res.split("\n").map(&:strip).reject(&:blank?).each do |line|
      next if line.start_with?("***")
      chunks = line.split(":")
      key = chunks.shift.strip
      val = chunks.join(":").strip

      # value cast
      val = false if %w[no No].include?(val)
      val = true if %w[yes Yes].include?(val)
      val = val.to_i if val.to_s =~ /^\d+$/

      r[key] = val
    end
  end
end

#check!(result, host, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/watchmonkey_cli/checkers/mysql_replication.rb', line 13

def check! result, host, opts = {}
  cmd = ["mysql"]
  cmd << "-u#{opts[:user]}" if opts[:user]
  cmd << "-p#{opts[:password]}" if opts[:password]
  cmd << "-h#{opts[:host]}" if opts[:host]
  cmd << "-P#{opts[:port]}" if opts[:port]
  cmd << %{-e "SHOW SLAVE STATUS\\G"}
  result.command = cmd.join(" ")
  result.result = host.exec(result.command)
  result.data = _parse_response(result.result)

  io  = result.data["Slave_IO_Running"]
  sql = result.data["Slave_SQL_Running"]
  sbm = result.data["Seconds_Behind_Master"]
  pres = io.nil? && sql.nil? ? "\n\t#{res}" : ""

  if !io && !sql
    result.error! "MySQL replication is offline (IO=#{io},SQL=#{sql})#{pres}"
  elsif !io || !sql
    result.error! "MySQL replication is BROKEN (IO=#{io},SQL=#{sql})#{pres}"
  elsif sbm > opts[:sbm_threshold]
    result.error! "MySQL replication is #{sbm} SECONDS BEHIND master (IO=#{io},SQL=#{sql})#{pres}"
  end
end

#enqueue(host, opts = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/watchmonkey_cli/checkers/mysql_replication.rb', line 6

def enqueue host, opts = {}
  opts = { host: "127.0.0.1", user: "root", sbm_threshold: 60 }.merge(opts)
  host = app.fetch_connection(:loopback, :local) if !host || host == :local
  host = app.fetch_connection(:ssh, host) if host.is_a?(Symbol)
  app.enqueue(self, host, opts)
end