Skip to content

Instantly share code, notes, and snippets.

@kuroda
Last active December 14, 2015 04:18
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 kuroda/5026789 to your computer and use it in GitHub Desktop.
Save kuroda/5026789 to your computer and use it in GitHub Desktop.
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