shell脚本设置钉钉告警模板

 

shell脚本模板

#!/bin/bash
source ~/.bash_profile
logfile=/tmp/rsync_dags.log
time=$(date "+%Y-%m-%d %H:%M:%S")
webhook='https://oapi.dingtalk.com/robot/send?access_token=4e13850af53becaa396694e4dq076d7feeecd7161c1ae3fec73036f263eb394c'
###webhook这个就是图五中要求复制下来的url
host='spark001'
报警函数:
function SendMsgToDingding() {
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'text',
'text': {
'content': '
时间:$time
服务器:$host
告警信息:rsync error报错$errorcount次
报错日志路径:[/tmp/rsync_dags.log]
报错脚本:[/home/admin/program/airflow/rsync_dags.sh]
请马上处理!!!'
#####content里的内容根据自己的报警内容编辑
},
'at': {
'isAtAll': true  ###实现效果 @所有人
}
}"
}
errorcount=`egrep -i "rsync error" $logfile|wc -l`
if [ $errorcount -gt 0 ];
then
SendMsgToDingding  

###如果符合报警条件就调用报警函数
fi
echo "$time rsync dags done."

 

 

 

运行效果如下图:

图一

下面是创建钉钉机器人的步骤:

图二

图三

图四

注解:比如我的自定义关键词是error,那么在我的content内容中,一定要包含error这个关键词

图五

posted @ 2020-01-14 09:23  任重而道远的小蜗牛  阅读(1757)  评论(1编辑  收藏  举报