shell脚本
#!/bin/bash
#monitor application error log
#获取服务err日志
logfiles=`find / -type f -name *.err | grep -v '[0-9]\{2,\}'`
user=() #接收告警用户列表
for logfile in ${logfiles[@]}
do
#获取变量值
title=`echo ${logfile} | awk -F/ '{print $4}'`
ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d "addr:"`
time=`date +"%Y-%m-%d %T"`
err_num=`cat ${logfile} | wc -l`
last_err_info=`cat ${logfile} | tail -n 1 | sed 's/"//g' | sed "s/'//g"`
#组合告警信息
msg=`echo "服务名称: $title\n机器IP: $ip\n日志路径: $logfile\n告警时间: $time\n错误日志数量: $err_num 条\n最新错误日志信息: $last_err_info"`
echo ${msg}
#发送告警
if [ $err_num -gt 0 ];then
for id in ${user[@]}
do
curl -i -X POST -H "'Content-type':'application/json'" -d '{"touser":"'"$id"'","content":"'"$msg"'"}' #微信告警接口 > /dev/null 2>&1
done
fi
done