MySQL的keepalived高可用监控脚本
时间:2021-07-01 10:21:17
帮助过:14人阅读
2
3
4
5
6
7 . /etc/init.d/
functions
8
9
10
if [
$# -ne 1 ];
then
11
echo "usage:sh $0 {tcp_port}"
12
exit 1
13
fi
14
15 check=`netstat -lnutp|grep
$1|wc
-l`
16
17
18 count=`ps -ef|grep
"$0 $1"|grep -v
"grep"|wc
-l`
19
20
if [
$count -gt 2 ];
then
21
echo -e "\nERROR:There is already have a same monitoring!"
22
exit 1
23
fi
24
25
function dmail(){
26
echo "$1-$(hostname):down" >/var/
log/
$1.log
27 mail
-s "$1-$(hostname):down" 00000000@qq.com </var/
log/
$1.log
28 }
29
30
function umail(){
31
echo "$1-switch-success" >/var/
log/
$1.log
32 mail
-s "$1-switch-success" 00000000@qq.com </var/
log/
$1.log
33 }
34
35
36
if [
$check -eq 0 ];
then
37
echo "$1 is not listening!pls input again!"
38
exit 1
39
else
40
while true
41
do
42
if [ `netstat -lnutp|grep
$1|wc
-l`
-eq 0 ];
then
43
echo 0
44 dmail
$1
45 /etc/init.d/keepalived stop
46 sleep 5
47 ping -c 2 -W 2 192.168.1.22 &>/dev/null
48
if [ $?
-eq 0 ];
then
49
echo 1
50 umail
$1
51
break
52
else
53
echo "$1-swith-failed" >/var/
log/
$1.log
54 mail
-s "$1-switch-failed" 00000000@qq.com </var/
log/
$1.log
55
break
56
fi
57
fi
58
done
59
fi
脚本简单说明 :
1.运行的条件:当前监控的端口号必须是监听状态,如果没启用会提示先启动服务
2.实现原理:实现监控的原理是用while循环+if判断
3.失败的处理机制:如果有心思的同学可以尝试,在服务down了之后,加上一些处理措施,比如说,让服务尝试重启,如果重启成功继续监控,不成功则关闭keepalive
4.事件的记录:每一个过程的成功与否,都要记录到相应的文件,如果有必要,进行邮件通知(一个好的运维,要懂得进行必须的故障记录,方便日后总结)
ps.不是特别难的脚本,重在梳理过程,如果有不妥的地方希望大家指正。
MySQL的keepalived高可用监控脚本
标签:原理 bash uil 启用 string 自身 相同 mysql name