shell监控后端错误日志

该脚本用于监控ESL业务的日志文件log_error.log,检查文件的修改时间和行数变化。当有新错误产生时,它会通过钉钉发送告警消息,包括主机名、公网IP、告警时间和详细错误信息。此外,它还处理日志文件的切割和存储旧的行数以供比较。
摘要由CSDN通过智能技术生成
  • 过滤出实时产生的日志记录 并通过钉钉告警
#!/usr/bin/env bash

set -x
: ${AWK=awk}
G_esl_business="/usr/local/esl/zk-refactor-esl-business/log/log_error.log"
G_esl_send="/usr/local/esl/zk-refactor-esl-business/log/send.log"
G_old_change_time="/server/scripts/log/old_change_time"
G_old_number1="/server/scripts/log/old_number"
G_host=`hostname`
G_public_network=`curl ifconfig.me`
G_time=`date '+%F %T'`
G_log_file="/server/scripts/log/esl_send_dingding"

#test
#webhook="https://oapi.dingtalk.com/robot/send?access_token="
webhook="https://oapi.dingtalk.com/robot/send?access_token=" 


access_log(){
      printf "[$G_time]: $*\n" >>$G_log_file
}

function SendMsgToDingding() {
    curl $webhook -H 'Content-Type: application/json' -d "
    {
        'msgtype': 'text',
        'text': {
            'content': '\n 系统:HK集群环境-01${G_host}-esl-服务日志的error监控 \n 关键字: 1  \n 主机公网IP: $G_public_network \n 告警时间: $G_time  \n 告警等级: 严重 \n 告警人员: 钉钉机器人 \n 告警详情:esl-服务日志中出现error了!\n 告警日志文件: $G_esl_business \n 日志切割: $cut \n 当前状态: PROBLEM \n ERROR信息:\n $msg '
        },
        'at': {
            'isAtAll': false
        }
    }"
}

function old(){
     #local old_change_time=$(stat $G_esl_business |grep -E "Change|改动"|awk '{print $2,$3}')
     local new_change_time=$(stat $G_esl_business |grep -E "Change|改动"|awk '{print $2,$3}')
      #echo  $old_change_time > $G_old_change_time
     old_time=`cat $G_old_change_time`
     
     if [ "$new_change_time" = "$old_time" ];then
        access_log "[info] File time has not changed"
     else
        #new.time > old.time 
        echo $new_change_time >$G_old_change_time
        access_log "give an alarm"
        lines      
     fi
}

function lines(){
       new_number=`cat $G_esl_business|wc -l`
       local old_number="/server/scripts/log/old_number"
       #echo $new_number >$old_number
       #set old.lines
       G_old_number1=`cat $old_number`
       #echo $old_number1
       # new.lines > old.lines
       if  [ $new_number -gt $G_old_number1 ] 
       then
          cut="未切割"
          \cp $G_esl_business -r $G_esl_send
          sed 's/[[:space:]]//g' -i $G_esl_send
          sed 's/'"'"/'/g' -i $G_esl_send
          access_log "[info] The number of log lines does not want to wait"
          msg=`$AWK  -v var="$G_old_number1" 'NR>var' $G_esl_send`
          echo $new_number > $old_number
          access_log `SendMsgToDingding`
       elif [ $new_number -lt $G_old_number1 ]
       then
            access_log "[info]  cut log files"
            #echo $new_number > $old_number
            old_lines_gt_new_lines
          
       else 
            echo "Does not emit lasers"
       fi          


}

function old_lines_gt_new_lines(){
       #cut log files
       old_lines=$(cat $old_number)
       new_lines=$(wc -l /usr/local/esl/zk-refactor-esl-business/log/log_error.log |awk '{print $1}')

       if [ $old_lines -gt $new_lines ]
       then
          cut="日志切割"
          \cp $G_esl_business -r $G_esl_send
          sed 's/[[:space:]]//g' -i $G_esl_send
          sed 's/'"'"/'/g' -i $G_esl_send                  
          access_log "[info] old > new"
          msg=$(cat $G_esl_send)
          echo $new_lines > /server/scripts/log/old_number
          access_log `SendMsgToDingding`
       fi

}



main(){
  old
}

main "$@"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值