Skip to content

Instantly share code, notes, and snippets.

@feixionglee
Forked from blasterpal/check_slave_status.rb
Created July 20, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feixionglee/8afb6a18050120682f81df063766b610 to your computer and use it in GitHub Desktop.
Save feixionglee/8afb6a18050120682f81df063766b610 to your computer and use it in GitHub Desktop.
Check MySQL Slave status from Shell using Ruby
#!/usr/bin/env ruby
@slave_status = Hash[%x(mysql -uroot -e 'SHOW SLAVE STATUS \\\G').split(/\s*\n\s*/).map { |e| spl = e.split(/\:\s*/); spl.size == 2 ? [spl.first, spl.last] : nil }.compact]
def slave_healthy?
@slave_status['Slave_IO_Running'] == 'Yes' &&
@slave_status['Slave_SQL_Running'] == 'Yes' &&
@slave_status['Seconds_Behind_Master'] != 'NULL' &&
@slave_status['Seconds_Behind_Master'].to_i < 1800
end
if slave_healthy?
puts "Slave OK"
else
puts "Slave ERROR: #{@slave_status}"
abort
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment