#!/bin/bash
source /etc/profile
ip1=`ip -4 addr show | grep "inet" | awk '{print $2}' | cut -d/ -f1| grep -v "127.0.0.1"`
ser1=$1
user1=$2
date1=`date +"%Y-%m-%d %H:%M:*" -d "1 minutes ago"`
#查询
if [ ! -f "/data/monitor/dict/$ser1" ];then
        mkdir -p /data/monitor/dict
	echo "qizhining">/data/monitor/dict/$ser1
fi
#创建文件夹存放临时文件
if [ ! -f "/data/monitor/errorlog/temp" ];then
        mkdir -p /data/monitor/errorlog/temp
fi

if [ ! -f "/data/TSP_LOG/$ser1/server.error.log" ];then
        echo "no error"
else
#       获取一分钟内报错
        err1=`cat /data/TSP_LOG/$ser1/server.error.log |grep -A10 "$date1"`
        if [ "$err1" == "" ]; then
                echo '无错误'
        else
                echo $err1 >/data/monitor/errorlog/temp/$ser1.log
                date2=`date +"%Y-%m-%d"`
				echo  $date2
#				按照年月日把文件分行
                 sed 's/'$date2'/\n'$date2'/g' /data/monitor/errorlog/temp/$ser1.log > /data/monitor/errorlog/temp/${ser1}_2.log
#				删除第一行
#                sed -i '1d' /data/monitor/errorlog/temp/${ser1}_2.log
#				获取跳过报警关键字
                list1=`cat /data/monitor/dict/$ser1`
#               设置循环分隔条件为换行符
                OLDIFS=$IFS;IFS=$'\n';for i in $list1;do
#				    按关键字删除行
                    sed -i '/'$i'/d' /data/monitor/errorlog/temp/${ser1}_2.log
                done
#				再次判断文件中是否有报错
				err2=`cat /data/monitor/errorlog/temp/${ser1}_2.log |grep "ERROR"`
				if [ "$err2" == "" ]; then
				    echo '无错误'
                else
                    echo '报警' 
                    date3=`date +"%y%m%d%H%M"`
                    date4=`date +"%y%m%d"`
                    cp /data/monitor/errorlog/temp/${ser1}_2.log    /data/monitor/errorlog/temp/${ser1}_${ip1}_$date3.log
                    /usr/local/bin/mc cp --recursive /data/monitor/errorlog/temp/${ser1}_${ip1}_$date3.log   alarm/alarm/server/errorlog/$date4/${ser1}/
                    errlog="$ip1时间$date1服务$ser1日志有ERROR信息，请处理，如正常请加过滤关键字/data/monitor/dict/$ser1下，详情请点击连接: http://101.132.37.121/server/errorlog/${date4}/${ser1}/${ser1}_${ip1}_$date3.log"
                    . /data/monitor/fs_error.sh
                    fs_warn "$errlog"
                    . /data/monitor/wx_warn_err.sh
                    wx_warn "$errlog" "$user1"
                    wx_sendfile "/data/monitor/errorlog/temp/${ser1}_${ip1}_$date3.log"
                    rm -rf /data/monitor/errorlog/temp/${ser1}_${ip1}*
		fi
				   
        fi
fi
