keepalived健康检查及双主MySQL健康检查脚本

一、http检查

HTTP_GET:工作在第5层,向指定的URL执行http请求,将得到的结果用md5加密并与指定的md5值比较看是否匹配,不匹配则从服务器池中移除;此外还可以指定http返回码来判断检测是否成功。HTTP_GET可以指定多个URL用于检测,这个一台服务器有多个虚拟主机的情况下比较好用。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

real_server 10.21.10.30 80 {

weight 1

HTTP_GET {

url {

path /keeptest/index`.html`

digest 3b9bc8d36a113971683321ad4fa02daf

}

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

}

}

1

2

#使用命令获取URL的hash

/usr/bin/genhash  -s 10.21.10.30 -p 80 -u /keeptest/index`.html`

二、tcp端口检查

SSL_GET:跟上面的HTTP_GET相似,不同的只是用SSL连接

三、tcp端口检查

TCP_CHECK:工作在第4层,keepalived向后端服务器发起一个tcp连接请求,如果后端服务器没有响应或超时,那么这个后端将从服务器池中移除。

1

2

3

4

5

6

7

8

9

real_server 172.16.0.66 3306 {

weight 1

TCP_CHECK {

connect_timeout 3

retry 3

delay_before_retry 3

connect_port 3306

}

}

四、脚本检查

MISC_CHECK:用脚本来检测,脚本如果带有参数,需将脚本和参数放入双引号内。脚本的返回值需为:             0)  检测成功             1)  检测失败,将从服务器池中移除             2-255)检测成功;如果有设置misc_dynamic,权重自动调整为 退出码-2,如退出码为200,权重自动调整为198=200-2。

五、邮件检查

SMTP_CHECK:用来检测邮件服务的smtp的。没用过不描述了自己搜吧

keepalive的双主MySQL健康检查

1

2

3

4

5

6

7

8

9

10

11

12

13

real_server 172.16.0.66 3306 {

weight 1

TCP_CHECK {

connect_timeout 3

retry 3

delay_before_retry 3

connect_port 3306

}

MISC_CHECK {

misc_path "/etc/keepalived/mysql_check.sh 172.16.0.66"

misc_timeout 3

}

}

mysql_check.sh内容如下

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

#!/bin/bash

if [ $`# -ne 1 ];then`

echo "Error: please use $0 172.16.0.66"

exit 1

else

CHECK_IP=$1

Slave_status=$(mysql -u guoyabin -p`'*' -h $CHECK_IP -e "show slave statusG" | grep -wE 'Slave_SQL_Running|Slave_IO_Running' | awk -F`": " '{print $2}' | wc -l)

Seconds_Behind_Master=$(mysql -u guoyabin -p`'*' -h $CHECK_IP -e "show slave statusG" | grep -w Seconds_Behind_Master | awk -F": "` `'{print $2}')`

if [ ${Slave_status} -`eq 2 ] && [ ${Seconds_Behind_Master} -lt 300 ];`then

exit 0

else

#异步调用钉钉发送通知。第一次脚本用了for i in {1..20}次,发现keepalive每次检查都要发送钉钉通知,故只通知一次。

{

message=`"$CHECK_IP Slave_status=$Slave_status Seconds_Behind_Master = $Seconds_Behind_Master"`

curl --connect-timeout 2 "https://oapi.dingtalk.com/robot/send?access_token=**********************************************" ``

-H "Content-Type: application/json" ``

-d '{`"msgtype":` `"text",`

"text"`: {`

"content"`: "'"${message}"'"`

}

}'

} &

exit 1

fi

fi

你可能感兴趣的:(深度学习)