合格linux运维人员必会的30道shell编程实践题及讲解-01

企业实践题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=rootPASSWORD=123456PORT=3307error=(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"    fidone}judge_salve(){status_arrayif [ "${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 truedo    is_run    judge_slave    sleep 30done}main 

出门走好路,出口说好话,出手做好事。

合格linux运维人员必会的30道shell编程实践题及讲解-01

相关文章:

你感兴趣的文章:

标签云: