shell_08:error_log日记增加则告警

写一个shell脚本,每隔30秒查看一次日记文件/data/logs/error.log,看有没有新的日记,如果有,需要发邮件告警

判断时间点,如果是 0 点 0 分需考虑特殊情况,记录上一次日记的行数,对比本次日记行数和上一次日记行数;

假设:1)   该日记0点0分会清空                 2)  邮件脚本路径/usr/local/sbin/mail.py

思路:用 while 循环,  sleep 30,

[root@localhost_002 shell]# cat 12.sh 
#!/bin/bash
while : 
do
  t=`date +%H%M`
  if [ $t == "00" ]          #判断是否是00:
  then
    rm -fr /tmp/log_line.txt  
    sleep 60
  fi

if [ ! -f /tmp/log_line.txt ]
then
  touch /tmp/log_line.txt
  wc -l /data/log/error.log|awk '{print $1}' >> /tmp/log_line.txt   
fi
last_n=`tail -1 /tmp/log_line.txt`               #新增日记的行;
n=`wc -l /data/logs/error.log|awk '{print $1}'`  #总日记的行;
echo $n >> /tmp/log_line.txt
if [ $n -gt $loat_n ]
then
  python /usr/local/sbin/mail.py yuanhh@163.com "错误日记增加" “tail /data/log/errlor.log”
fi
   sleep 30
done

第二个方法:自己整理:

#!/bin/bash
mail=yuanhh@163.com
 logfile=/data/logs/error.log
t1=`date +%H`
t1=`date +%M`
n=`cat $logfile |wc -l`
while  :
do
n1=`cat $logfile |wc -l`
if  [  -z $n  ]   ||   [  -z  $n1  ]
then 
         echo  "The  value none"
         exit
fi
if [ $n1  -gt  $n  ]
then  
/usr/local/sbin/mail.py $mail  "error.log "   "errorlog  增加"
fi
if [ $t1 == "00" -a  $t2 == "00" ]
      then
              echo  $n  > /tmp/1.log 
             echo  $n1  >> /tmp/1.log 
fi
sleep  30
done 

 

转载于:https://my.oschina.net/yuanhaohao/blog/2997158

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值