Skip to content

Instantly share code, notes, and snippets.

@erincerys
Created January 23, 2015 17:41
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 erincerys/48dd2d5cb91d5454ffd4 to your computer and use it in GitHub Desktop.
Save erincerys/48dd2d5cb91d5454ffd4 to your computer and use it in GitHub Desktop.
A shell script to monitor a MySQL replication connection.
#!/bin/bash
# Monitor and report a status code somewhere about a MySQL slave's replication connection
# Useful for when traditional monitoring cannot be achieved due to firewalling.
D=`date -u +"%Y%m%d%H%M"`
mysqlparams='-h... -ureplicator -p...'
query='show slave status\G'
logfile='/var/log/replication_check.log'
io_thread=$(mysql $mysqlparams -e "${query}" | grep -iE 'io_running\:' | awk '{print $2}')
sql_thread=$(mysql $mysqlparams -e "${query}" | grep -iE 'sql_running\:' | awk '{print $2}')
if [[ "${io_thread}" == 'No' || "${sql_thread}" == 'No' ]] ; then
if [[ "${io_thread}" == 'No' && "${sql_thread}" == 'No' ]] ; then
postcode=400
postmsg='Replication%20has%20stopped!'
elif [[ "${io_thread}" == 'Yes' && "${sql_thread}" == 'No' ]] ; then
postcode=300
postmsg='Replication%20has%20stalled'
fi
slave_error=$(mysql $mysqlparams -e "${query}" | grep -iE 'last_error\:' | awk '{print $2}')
echo '[${D}] $postmsg -- $slave_error' >> $logfile
else
postcode=200
postmsg='Replicating%20OK'
fi
# Custom monitoring reporting logic goes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment