#!/bin/bash
#
# A mysql replication plugin for the check_mk nagios system.
# Place me in /usr/lib/check_mk_agent/local on the client
#
# Hereward Cooper <coops@iomart.com> - 16/06/11
if grep -q skip-slave-start /etc/my.cnf.d/mysql-server.cnf; then
echo "check_a2_whmcs_db_mysql-replication - Master Replication server"
exit 0
fi
MYSQL_USER="root"
MYSQL_PASS=""
MYSQL_SOCKET="/var/lib/mysql/mysql.sock"
# Anything below DELAY_OK is fine. Anything between
# DELAY_OK and DELAY_WARNING is a warning. Anything
# above DELAY_WARNING is critical.
DELAY_OK=600
DELAY_WARNING=43200
MYSQL_STATUS=$(mysql --defaults-extra-file="/root/.my.cnf" -u$MYSQL_USER -S $MYSQL_SOCKET -e "SHOW SLAVE STATUS\G" | egrep 'Slave_.*_Running|Seconds_Behind_Master' | sed 's/^ *//')
echo "${MYSQL_STATUS}" | sed -n '1p' | grep -q Yes && IO=1 || IO=0
echo "${MYSQL_STATUS}" | sed -n '2p' | grep -q Yes && SQL=1 || SQL=0
DELAY=$(echo "${MYSQL_STATUS}" | sed -n '3p' | cut -d " " -f 2)
replication_data_collect() {
## Check Slave_IO_Running status
if [ ${IO} = "1" ]; then
echo "MySQL_Rep_IO_WHMCS - OK - Replication IO Running"
else
echo "MySQL_Rep_IO_WHMCS - CRITICAL - Repication IO Stopped"
fi
## Check Slave_SQL_Running status
if [ ${SQL} = "1" ]; then
echo "MySQL_Rep_SQL_WHMCS - OK - Replication SQL Running"
else
echo "MySQL_Rep_SQL_WHMCS - CRITICAL - Replication SQL Stopped"
fi
## Check Seconds_Behind_Master value
if [ ${DELAY} = "NULL" ]; then
echo "MySQL_Rep_Delay_WHMCS - CRITICAL - Replication delay NULL"
elif [ ${DELAY} -lt ${DELAY_OK} ]; then
echo "MySQL_Rep_Delay_WHMCS - OK - Replication delay $DELAY seconds"
elif [ ${DELAY} -lt ${DELAY_WARNING }]; then
echo "MySQL_Rep_Delay_WHMCS - WARNING - Replication delay $DELAY seconds"
elif [ ${DELAY} -ge ${DELAY_WARNING} ]; then
echo "MySQL_Rep_Delay_WHMCS - CRITICAL - Replication delay $DELAY seconds"
fi
}
replication_data=$(replication_data_collect)
if $(echo "${replication_data}" | grep -q CRITICAL); then
echo "check_a2_whmcs_db_mysql-replication - ${replication_data}" | sed ':a;N;$!ba;s/\n/ | /g'
exit 2
else
echo "check_a2_whmcs_db_mysql-replication - ${replication_data}" | sed ':a;N;$!ba;s/\n/ | /g'
exit 0
fi