Skip to content

Instantly share code, notes, and snippets.

@johanmeiring
Created July 3, 2012 09:06
Show Gist options
  • Select an option

  • Save johanmeiring/3038649 to your computer and use it in GitHub Desktop.

Select an option

Save johanmeiring/3038649 to your computer and use it in GitHub Desktop.

Revisions

  1. johanmeiring created this gist Jul 3, 2012.
    43 changes: 43 additions & 0 deletions checkreplication.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/bin/bash
    # MySQL Replication Status Checker
    # This script will run the SHOW SLAVE STATUS query on the specified server, and then report it if Last_IO_Errno or Last_SQL_Errno do not equal 0.
    # Most effective when used as a cron job that runs once every minute.
    # Version 0.1

    # DB Info
    DBHOST=localhost
    USERNAME=root
    PASSWORD=lolololol

    # Machine's name. Change to something else if you want to.
    HOSTNAME=`hostname`

    # Array of email addresses to send emails to.
    EMAILS=("me@myaddress.com" "dba@myaddress.com")

    # Array of cellphone numbers to send SMS's to.
    CELLPHONES=("0821112223")

    # Run query.
    sql_result=`mysql -h${DBHOST} -u${USERNAME} -p${PASSWORD} -e "SHOW SLAVE STATUS\G" | egrep "(Last_IO_Errno|Last_SQL_Errno)"`

    # Do checking.
    if [ `echo $sql_result | awk '{print $2}'` -ne 0 ] || [ `echo $sql_result | awk '{print $4}'` -ne 0 ]; then
    # Seems to be borked...
    for email in ${EMAILS[@]}
    do
    (echo "Warning: Replication on ${HOSTNAME} seems to be borked. DO SOMETHING!") | mailx -s "Replication Error: ${HOSTNAME}" $email
    done

    msg="Replication failure on ${HOSTNAME}. DO SOMETHING!"

    # URL Encode the SMS
    #msg="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${msg}")"
    msg="$(python -c "import sys; import urllib; print urllib.quote_plus(sys.argv[1]);" "${msg}")"
    for cell in ${CELLPHONES[@]}
    do
    # Use e.g. cUrl to consume some service that sends SMSs.
    done
    fi

    exit 0