centos下sh脚本监控文件内容告警

5 篇文章 0 订阅
3 篇文章 0 订阅

实现功能:  centos下脚本监听某个java项目下的Exception 日志,当监听到日志里出现 "Exception" 字符时进行告警

脚本启动命令:  nohup sh z_jiankong1.sh >/dev/null 2>&1 &

#!/bin/bash
#--------------------------------------------
# 监听服务器日志, 如果出现错误,发微信告警
# author:by zero
# start commod: nohup sh z_jiankong1.sh >/dev/null 2>&1 &
#--------------------------------------------
sitename='我的网站1'
logfile='/home/web/log/error.log'
shelllog='/home/web/log/wxerr01.log'
beforelinenum=`sed -n '$=' $logfile`
#echo $beforelinenum
secondspan=10
t1=`date '+%Y-%m-%d %H:%M:%S'`
echo "$t1 $sitename 开始监听 $logfile, $beforelinenum" >> $shelllog
while true
do
  afterlinenum=`sed -n '$=' $logfile`
  #当日志文件清空时重置起始点,通常日志会换天写文件
  if [ $afterlinenum -lt $beforelinenum ];then
    beforelinenum=0
  fi
  line=$(( $afterlinenum - $beforelinenum ))
  #echo " line:"$line >> $shelllog
  beforelinenum=$afterlinenum
  content=`tail -n $line $logfile |grep -A 20 'Exception'`
  if [ -n "$content" ]; then
        t1=`date '+%Y-%m-%d %H:%M:%S'`
        t2=$(date +\%Y\%m\%d\%H\%M\%S)
        echo "$t1 $sitename line:$line content:$content" >> $shelllog
        echo -e "服务器发生异常,准备发送告警web" >> $shelllog
        #curl -d "msg1="$sitename"&msg2=Exception" https://www.baidu.com/notify/wxmsg
        echo "发送完成..." >> $shelllog

  fi
  sleep $secondspan
done
echo "程序退出." >> $shelllog
exit

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值