合格linux运维人员必会的30道shell编程面试题及讲解

时间:2021-07-13 21:51:10

企业面试题1:(生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)

我的脚本===================

[root@master day4]# cat mysql_slave.sh 

#
!/bin/bash
[
-f /etc/init.d/functions ] && . /etc/init.d/functions

[ $UID
-ne 0 ] &&{

echo "only allow root to exec this cmd."
exit
}

USER
=root
PASSWORD
=123456
PORT
=3307
error
=(1158 1159 1008 1007 1062)
MYSQLCMD
="mysql -u$USER -p$PASSWORD -P$PORT"

is_run(){
[ `lsof
-i:$PORT|wc -l` -lt 2 ]&&{
echo "mysql server is stopped."
exit
1
}
}

status_array(){
status
=($($MYSQLCMD -e "show slave status\G"|egrep "_Running|Last_Errno|Behind_Master"|awk '{print $NF}'))
}

status_error(){
for((i=0;i<${#error[*]};i++))
do
if [ "$1" == "${error[$i]}" ]
then
$MYSQLCMD
-e "stop slave;set global sql_slave_skip_counter=1;start slave;"
else
echo "Mysql slave is failed,errorno is $1"
fi
done
}

judge_salve(){
status_array
if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a "${status[3]}" == "0" ];then
echo "Mysql slave is ok"
else
status_error ${status[
2]}
fi
}

main(){
while true
do
is_run
judge_slave
sleep 30
done
}
main