健康检查shell脚本

现在有个微服务集群,其中网关是单点的服务,需要定时检测服务状态,避免服务宕机导致整个应用不可用的情况

springcloud-gateway添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

启用监控 暴露helath接口返回{"status":"UP"}

shell脚本

$ vi /root/health.sh

function sms()
{
for phone in "$@"
do
echo "$phone"
sms_key='key'
sms_secret='secret'
sms_url='http://sms_send_url'
sms_vn='vn'
sms_tem='tem'
uuid=$(cat /proc/sys/kernel/random/uuid)
timestamp=`date +%s%3N`
echo timestamp:$timestamp
echo uuid:$uuid
keys=$sms_secret$uuid$timestamp
echo keys:$keys
sign=`echo -n $keys|md5sum|cut -d ' ' -f1`
echo sign:$sign
sign=`echo $sign | tr "[:lower:]" "[:upper:]"`
echo sign:$sign
content='smsParam={"service":"xxxx网关服务告警","reason":"网关服务异常","id":"0","reqid":"0"}&smsSignId=000001&smsTemplateId=000001&smsType=0&telNo
='$phone'&'
commmand= curl -H "Content-Type:application/x-www-form-urlencoded;charset=utf-8" -H "key:$sms_key" -H "timestamp:$timestamp" -H "uuid:$uuid" -H "token:$sign" -X "POST" $sms_url -d $content
$(command)
done
}

res=`curl http://127.0.0.1:8080/actuator/health`
uptag='UP'
if [[ $res =~ $uptag ]]
then
    echo "健康检查成功"
else
    echo "健康检查失败"
    sms "15800001111" "15800002222"
fi

释义:首先定义一个发送短信的方法,然后调用/actuator/health接口,返回值如果不包含UP,发送短信到指定的2个号码
包含shell语法:
时间戳 uuid md5 字符串拼接 小写转大写 包含 定义function function传参 获取function参数 执行变量命令 if-else curl请求接口

定时任务:

crontab -e
*/2 * * * * /root/health.sh

编辑定时任务,2分钟执行一次健康检查

发表新评论