shell监听日志发送邮件通知

  • 写了个shell脚本监听日志变化,grep Exception发送邮件通知,直接把代码贴出来,也可以到我的git地址 https://github.com/kittaaron/blog/blob/master/sendmail.sh 查看。注意:需要先安装mailx(yum install mailx)。

给公众号(三岁嘟嘟)打下广告:


shell监听日志发送邮件通知_第1张图片
mp_qrcode.jpg
#!/bin/bash

#--------------------------------------------
# 监听服务器日志, 如果出现错误,发邮件告警
# author:kittaaron
# site: http://kittaaron:1888/static/index.html
#--------------------------------------------

cd `dirname $0`
logfile='logs/out_sports.log'
beforelinenum=`sed -n '$=' logs/out_sports.log`
echo $beforelinenum
echo "开始监听..."
secondspan=3
ip=`ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}'`
while true
do
  afterlinenum=`sed -n '$=' logs/out_sports.log`
  line=$(( $afterlinenum - $beforelinenum ))
  #echo "line:"$line
  beforelinenum=$afterlinenum
  content=`tail -n $line logs/out_sports.log |grep -A 20 'Exception'`
  if [ -n "$content" ]; then
    echo -e "服务器发生异常,准备发送告警邮件"
    echo -e $content  |  mail  -s  "服务器$ip 发生异常,请及时关注" -r [email protected]
    echo "发送邮件完成..."
  fi
  sleep $secondspan
done
echo "程序退出."
exit

你可能感兴趣的:(shell监听日志发送邮件通知)