定时过滤日志发到飞书
作者:互联网
#每5分钟执行一次
#crontab –e
#*/5 * * * * *.sh 5
mins=5
if [ $# -gt 0 ]; then
mins=$1
fi
token=`curl -X POST https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal -H 'Content-Type: application/json' -d '{
"app_id": "cli_9dfdda101eb85101",
"app_secret": "bsI9wvNRTFyTVh3z5oxVAeupgFK6tldv"
}' | jq -r .tenant_access_token`
echo "token is "$token
time_all=`date +"%Y_%m_%d %H:%M:%S"`
log_dir="/usr/local/app/tars/app_log/ForGame"
pattern="ERROR"
cat /dev/null > data.txt
echo "server log monitor:$time_all" >> data.txt
for (( i = $mins; i > 0; i-- )) ; do
grep -arni --include *.log "$(date +"%Y-%m-%d %H:%M" -d "-$i min")" $log_dir | grep $pattern >> data.txt
done
linenum=`wc -l data.txt | cut -d" " -f1`
if [ $linenum -eq 1 ]; then
rm -f data.txt
exit 0
elif [ $linenum -gt 100 ]; then
echo "server log monitor:$time_all" > data.txt
echo "错误日志超过100条,速查" >> data.txt
fi
#sed -i '$d' data.txt
sed -i "s/\r//g" data.txt
sed -i "s/\"//g" data.txt
echo $(sed "$ ! s/$/\\\n*/" data.txt) > data.txt
context=$(cat data.txt)
curl -X POST -H "Authorization: Bearer ${token}" -H 'Content-Type: application/json' -d '{
"chat_id":"oc_c94690611f9ad7de9271bb0e33ec8e23",
"msg_type": "text",
"content": {
"text": "'"$context"'"
}
}' https://open.feishu.cn/open-apis/message/v4/send/
echo ""
rm -f data.txt
标签:发到,log,echo,token,sed,日志,txt,data,定时 来源: https://www.cnblogs.com/txtp/p/16360933.html