1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
我的博客已迁移到xdoujiang.com请去那边和我交流
一、具体代码
#!/bin/bash
#--------------------------------------------------
#Author:jimmygong
#Email:jimmygong@taomee.com
#FileName:checkslave.sh
#Function:
#Version:1.0
#Created:2016-01-01
#--------------------------------------------------
if
[[ `
id
-u` -
ne
0 ]]
then
echo
"script need root"
exit
1
fi
function
usage ()
{
echo
"Usage:bash $0 start"
echo
"Usage:bash $0 stop"
}
if
[[ $
# -ne 1 ]]
then
usage
exit
1
fi
#根据情况修改变量信息
mysqluser=
"root"
mysqlpass=
"123456"
mysqlport=3398
mysqlsocket=
"/opt/mysql/$mysqlport/$mysqlport.sock"
currdate=$(
date
+%Y%m%d)
logpath=
"/root/checkslave"
logfile=$logpath/${currdate}
#各个错误代码解释
1158网络错误,出现读错误,请检查网络连接状况
1159网络错误,读超时,请检查网络连接状况
1008数据库不存在,删除数据库失败
1007数据库已存在,创建数据库失败
1062字段值重复,入库失败
1452不能删除或更新父行,外键约束失败
PS:1452这个根据情况跳过错误
allerror=(1158 1159 1008 1007 1062 1452)
sleeptime=60
[[ -d $logpath ]] ||
mkdir
$logpath
function
echosucc ()
{
succstatus=
"[ Ok ]"
printf
"\033[32m $succstatus $* \033[0m\n"
}
function
mysqlconn ()
{
comm
=$1
mysql -u${mysqluser} -p${mysqlpass} -S ${mysqlsocket} -e
"$comm"
}
function
checkrun ()
{
esport=$(
lsof
-i:$mysqlport|
wc
-l)
if
[[ $esport -lt 2 ]]
then
echo
"Mysql Server Failed"
exit
1
fi
}
function
statuserror ()
{
for
((i=0;i<${
#allerror[*]};i++))
do
if
[[
"$1"
==
"${allerror[$i]}"
]]
then
mysqlconn
"stop slave;set global sql_slave_skip_counter=1;start slave;"
else
echo
"$(date +"
%m-%d %H:%M:%S
") Mysql Slave Failed $1"
>> $logfile
fi
done
}
function
checkslave ()
{
status=($(mysqlconn
"show slave status\G"
|
egrep
-i
"_running|last_errno"
|
awk
'{print $NF}'
))
if
[[
"${status[0]}"
==
"Yes"
]] && [[
"${status[1]}"
==
"Yes"
]]
then
echo
"$(date +"
%m-%d %H:%M:%S
") Mysql Slave Ok"
>> $logfile
else
statuserror ${status[2]}
fi
}
function
start ()
{
echosucc
"Starting Monitor"
while
true
do
checkrun
checkslave
sleep
$sleeptime
done
}
function
stop ()
{
echosucc
"Stop Monitor"
echo
$$ >
"$logpath/pidfile"
for
PID
in
$(
ps
-eo pid,
command
|
grep
"$0"
|
grep
-
v
grep
|
awk
'{print $1}'
);
do
[[ $PID != $(
cat
$logpath
/pidfile
) ]] &&
kill
-s TERM $PID >&
/dev/null
done
}
case
$1
in
start)
start &
;;
stop)
stop
;;
*)
usage
;;
esac
二、相关日志都记录在
cat
checkslave
/20160102
|
more
01-02 21:19:10 Mysql Slave Failed 1452
01-02 21:19:10 Mysql Slave Failed 1452
01-02 21:19:10 Mysql Slave Failed 1452
01-02 21:19:10 Mysql Slave Failed 1452
01-02 21:19:10 Mysql Slave Failed 1452
01-02 21:34:58 Mysql Slave Ok
01-02 21:35:58 Mysql Slave Ok
01-02 21:36:59 Mysql Slave Ok
01-02 21:37:59 Mysql Slave Ok
三、相关进程
ps
-ef |
grep
-w checkslave.sh
root 12260 1 0 21:34 pts
/2
00:00:00
bash
checkslave.sh start
四、相关启动和关闭
bash
checkslave.sh start
[ Ok ] Starting Monitor
bash
checkslave.sh stop
[ Ok ] Stop Monitor
|
本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1729738,如需转载请自行联系原作者