File: //lib64/nagios/plugins/check_a2_mysql-monitor.shared.mvps.mdedi
#!/bin/bash
#
# MySQL and InnoDB engine Monitoring
#
service_name="mysql_monitor"
if /usr/bin/pgrep -x pdns_recursor > /dev/null; then
echo "${service_name} - MySQL not expected"
exit 0
fi
if [ -f /sbin/plesk ]; then
ISPLESK=1
fi
if [ -f /root/.my.cnf ]; then
MYSQLCMD="mysql --defaults-extra-file=/root/.my.cnf"
else
MYSQLCMD="mysql"
fi
_innodb_check() {
if [[ ! $ISPLESK == 1 ]]; then
if echo show engines|$MYSQLCMD |grep -q "InnoDB.*YES";then
if $MYSQLCMD -e "SHOW VARIABLES LIKE 'innodb_force_recovery'" |grep -q "0$"; then
echo "${service_name} - MySQL is up and InnoDB is loaded"
exit 0
else
echo "${service_name} - MySQL is up and InnoDB is loaded, but in force recovery, please check!"
exit 2
fi
else
echo "${service_name} - MySQL is up but InnoDB is NOT loaded"
exit 2
fi
else
if MYSQL_PWD=`cat /etc/psa/.psa.shadow` mysql -u admin -e "show engines" | grep -q "InnoDB.*YES";then
echo "${service_name} - MySQL is up and InnoDB is loaded"
exit 0
else
echo "${service_name} - MySQL is up but InnoDB is NOT loaded"
exit 2
fi
fi
}
_mysql_check() {
mysql_safe_count=$(pgrep mysqld_safe |wc -l)
mysqld_count=$(pgrep 'mysqld$' |wc -l)
if (( $mysql_safe_count > 1 )) || (( $mysqld_count > 1 )); then
echo "[check_mk] MySQL running multiple instances" >> /var/log/mysql.error.log
echo "${service_name} - Too many MySQL instances running, please check."
exit 2
elif (( $mysql_safe_count == 1 )) || (( $mysqld_count == 1 )); then
if [[ ! $ISPLESK ]]; then
if [ -e /tmp/mysql.sock ]; then
echo "[check_mk] MySQL appears to be stuck" >> /var/log/mysql.error.log
echo "${service_name} - MySQL seems stuck and needs investigated."
exit 2
else
echo "${service_name} - MySQL is in crash recovery."
exit 2
fi
fi
else
echo "[check_mk] MySQL is 'not running'" >> /var/log/mysql.error.log
echo "${service_name} - MySQL is not running, please check."
exit 2
fi
}
if [[ ! $ISPLESK == 1 ]]; then #&& ! mysql --defaults-file=/root/.my.cnf -e ';' --connect_timeout=10 &> /dev/null;then
if [[ -e "/root/.my.cnf" ]]; then
if ! mysql --defaults-file=/root/.my.cnf -e ';' --connect_timeout=10 &> /dev/null;then
_mysql_check
else
_innodb_check
fi
else
_innodb_check
fi
elif [[ $ISPLESK == 1 ]]; then
if ! MYSQL_PWD=`cat /etc/psa/.psa.shadow` mysql -u admin -e ';' --connect_timeout=10 &> /dev/null;then
echo "${service_name} - MySQL is not responding, please check."
exit 2
else
_innodb_check
fi
else
_innodb_check
fi