Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple script to check if MySQL replication is working normally.
#!/usr/bin/ruby
password = `cat /root/mysql_root_password`
password.strip!
result = `mysql -u root -p#{password} -E -e "show slave status"`
values = {}
result.split(/\n/).each do |line|
if md = line.match(/:/)
key = md.pre_match.strip.to_sym
value = md.post_match.strip
values[key] = value
end
end
message = ''
if values[:Slave_IO_Running] != 'Yes'
message << "Slave_IO_Running: #{values[:Slave_IO_Running]}\n"
end
if values[:Slave_SQL_Running] != 'Yes'
message << "Slave_SQL_Running: #{values[:Slave_SQL_Running]}\n"
end
if values[:Last_Errno] != '0'
message << "Last_Errno: #{values[:Last_Errno]}\n"
end
if values[:Seconds_Behind_Master].to_i > 60
message << "Seconds_Behind_Master: #{values[:Seconds_Behind_Master]}\n"
end
if message != ''
system "echo #{message} | mailx -s 'Replication Alert' admin@example.com"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.