监控mysql脚本
#!/bin/bash
#********************************************************************
# File Name: mysql.sh
# Version: V1.0
# Author: dahuangji
# Email:
# Created Time : 2022-02-19 21:41:25
# Description:
#********************************************************************
RED='\E[31;1m'
GREEN='\E[32;1m'
END='\E[0m'
m_user="-uroot"
m_ps=""
m_fun=$1
m_state=$2
mysql_state(){
mysql $m_user $m_ps -e "show global status"|grep -iw "$m_state"|awk '{print $2}'
}
mysql_var(){
mysql $m_user $m_ps -e "show global variables"|grep -iw "$m_state"|awk '{print $2}'
}
mysql_slave_check(){
m1=`mysql -uroot -e "show slave status\G;" | grep "Slave_IO_Running" | awk -F':| +' '{print $4}'`
m2=`mysql -uroot -e "show slave status\G;" | grep "Slave_SQL_Running:" | awk -F':| +' '{print $4}'`
if [[ $m1 = "Yes" && $m2 = "Yes" ]];then
echo 50
else
echo 100
fi
}
err(){
if [[ $m_state = "" ]];then
echo -e "$RED必须给定mysql信息(大小写不敏感) $END"
echo "----------------------------------"
echo "用法: $0 功能 状态"
echo "示例: $0 mysql_state threads_running"
echo "示例: $0 mysql_slave_check"
return 1
fi
}
case $1 in
mysql_slave_check)
mysql_slave_check ;;
mysql_state)
err && test $? = "0" && mysql_state ;;
mysql_var)
err && test $? = "0" && mysql_var ;;
*)
echo -e "$RED必须给定mysql信息(大小写不敏感) $END"
echo "----------------------------------"
echo "用法: $0 功能 状态"
echo "示例: $0 mysql_state threads_running"
echo "示例: $0 mysql_slave_check"
;;
esac