企业微信添加机器人,监控实时任务,如果失败就重启,并向群里推送预警

企业微信添加机器人,监控实时任务,如果失败就重启,并向群里推送预警

1.添加机器人

只能在企业微信的群聊里才可加机器人,如下图右键群点击添加机器人,新建一个机器人,然后给机器人起个名字就好
在这里插入图片描述

这个时候就会在群机器人那里看到你添加的机器人,鼠标放上去就会看到机器人的Webhook地址
在这里插入图片描述

点击地址就可以打开机器人的配置说明了。配置文档很清晰就不多说了。
在这里插入图片描述

2.重点:监控脚本编写

编写检测实时任务是否挂掉的脚本,如果任务挂了就重启并执行微信推送脚本weixin_monitor.sh发送消息推送到企业微信群
检测实时任务的脚本:stream_monitor.sh

#!/bin/bash
source $HOME/.bash_profile

cnt=0
counter=3
basePath="/data/app/streamserver"
logFile="/data/log/monitorFile/monitor.log"
declare -A job_map=(
["这里是yarn中提交的实时任务名称"]=$basePath"/stream_01.sh"
["这里是yarn中提交的实时任务名称"]=$basePath"/stream_02.sh"
)

while [ ${counter} -ne 0 ]
do
normal=0
echo -e "\nThe $((4-$counter)) time scanning..........."
for s in "${!job_map[@]}"
do
    is_running=$(yarn application -list | awk '{print $2}' | grep ${s} | grep -v grep)
    if [[ -z ${is_running} ]]; then 
        normal=1
        if [ -e ${job_map[$s]} ];then 
            echo `sh ${job_map[$s]} -d &>/dev/null 2>&1` &&  sleep 10s
                if [[ -z ${is_running} ]]; then
                         echo -e `sh "/data/app/monitor_shell/winxin_monitor.sh" '18720080000' "job $s 重启成功运行.\r\n第$((4-$counter)) 次重启."`
                fi
        else
            echo -e `sh "/data/app/monitor_shell/winxin_monitor.sh" '18720080000' "job $s 沒有运行.\r\n重启失败,请登入系统检查手动重启,第$((4-$counter)) 次重启."`
        fi
    else
        echo -e "\033[1;32mjob $s is running. \033[0m" >>$logFile
    fi      
done        
counter=$(( $counter-1 ))     
if [[ ${normal} -eq 0 ]];then
    counter=0
fi            
done  

run_date=`date '+%F %T %:::z'`
if [[ ${normal} -ne 0 ]];then
    echo "any streaming job restart failed!!!" >>$logFile
    message="Error!!! streaming job restart failed!!! date:$run_date"
        echo message >>$logFile 
    exit 1
else
    echo "all streaming job is running!" >>$logFile
    exit 0
fi

微信推送脚本:weixin_monitor.sh 发送消息到群并@指定手机号的人

#!/bin/bash
phone=$1
message=$2
WEBHOOK_URL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=111111111111111111"

curl --location --request POST ${WEBHOOK_URL} \
--header 'Content-Type: application/json' \
-d "{\"msgtype\": \"text\",\"text\": {\"content\":\"$message\",\"mentioned_mobile_list\":[\"$phone\"]}}"

最后将检测实时任务的脚本stream_monitor.sh 加到定时任务crontab中

*/10 * * * * sh /data/app/monitor_shell/stream_monitor.sh

每10分钟执行一次检测脚本。当然这里的时间可以自己调整

最终发送的群消息提醒如下图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值