#!/bin/bash
# 钉钉告警
function send_notify(){
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxx' \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "markdown",
"at": {
"atMobiles": [
"158xxx",
"132xxx",
"199xxx"
],
"atUserIds":[
"xxxx"
]
},
"markdown": {
"title": "Logstash错误告警",
"text": "### Logstash错误告警 @158xxx@132xxx \n > '$1' "
}
}'
}
# 查看lostash日志文件
## 扫描时间
dt=$(date +"%Y-%m-%d %H:%M:%S")
## 查看行尾10万行数据是否有错误产生,并将错误信息写入tail.log
tail -n 100000 /run/log_consumer.log|grep ERROR > /run/dingding_alert_robot_sh/tail.log
# 将tail.log最后一行内容去除空格赋值给msg
msg=`tail -n 1 /run/dingding_alert_robot_sh/tail.log|sed 's/ //g'`
echo "error--------->$msg"
# 获取上次执行时间
# last_dt=`tail -n 1 /run/dingding_alert_robot_sh/logstash_error_alert.log|awk -F '=====>' {print $1}`
#判断msg是否是空字符串,如果不是发送钉钉告警
if [ $msg == ""]
then
# 记录日志
echo "$dt=====>未发生错误" >> /run/dingding_alert_robot_sh/logstash_error_alert.log
else
# 发送告警信息
res=$(send_notify $msg)
# 记录日志
echo "$dt=====>发生了错误----->$msg" >> /run/dingding_alert_robot_sh/logstash_error_alert.log
fi