企业微信添加机器人,监控实时任务,如果失败就重启,并向群里推送预警
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分钟执行一次检测脚本。当然这里的时间可以自己调整
最终发送的群消息提醒如下图

1635

被折叠的 条评论
为什么被折叠?



