日志分析,并将结果写入文件

#-----------------------------------------------------------------
#判断是否闰年
#input:year
#output: "true" "fase"
check_leap() 
{
    Y=`expr substr $1 1 4`
    r1=`expr $Y \% 4`
    r2=`expr $Y \% 100`
    r3=`expr $Y \% 400`
    if [ "$r1" -eq 0 -a "$r2" -ne 0 -o "$r3" -eq 0 ];then
        FRUN="true"
    else
        FRUN="false"
    fi
    echo $FRUN
}
#-----------------------------------------------------------------
# 获取月份最大日期
#方法1
get_mon_days()
{
    Y=`expr substr $1 1 4`
    M=`expr substr $1 5 2`
    case "$M" in
         01|03|05|07|08|10|12) days=31;;
         04|06|09|11) days=30;;
         02)
        _tmpStr=`check_leap "$Y"`  #判断是否闰年
        if [ "$_tmpStr" = "true" ] ; then
            #闰年
            days=29
        else
            days=28
        fi
        ;;
         *)
        days=0
        ;;
    esac
    echo $days
}
#-----------------------------------------------------------------
check_date()
{
    #检查是否传入一个参数
    [ $# -ne 1 ] && return 1
    #检查字符串长度
    _lenStr=`expr length "$1"`
    [ "$_lenStr" -ne 8 ] && return 1
    #检查是否输入的是非0开头的数字
    _tmpStr=`echo "$1" | grep "^[^0][0-9]*$"`
    [ -z "$_tmpStr" ] && return 1
    Y=`expr substr $1 1 4`
    M=`expr substr $1 5 2`
    D=`expr substr $1 7 2`
    #检查月份
    [ "$M" -lt 1 -o "$M" -gt 12 ] && return 1
    #取当月天数
    days=`get_mon_days "$Y$M"`
    #检查日
    [ "$D" -lt 1 -o "$D" -gt "$days" ] && return 1
    return 0
}



# -----------------------------------------------------------------------------
# 脚本说明:
# 	错误统计脚本
#	生成1.原始数据文件:errorReport.txt.yyyy-MM-dd 
#	    2.错误类型数据文件:errorReportType.txt.yyyy-MM-dd
# -----------------------------------------------------------------------------
# 参数1:统计日期,格式yyyyMMdd,为空则为当天
# 参数2:日志文件路径,为空默认当前目录
# 参数3:生成数据文件路径,为空默认当前目录
# -----------------------------------------------------------------------------


#!/bin/sh
if [ -n "$1" ]; then
   check_date $1
	if [ $? -eq 1 ];then
	    echo "<ERROR>输入日期[$1]格式错误!示例:(`date +%Y%m%d`)"
	    exit 1
	fi
   dt=$1
else
   dt=`date +'%Y%m%d'`
fi

if [ -n "$2" ]; then
   srcPath=$2
else
   srcPath=.
fi

if [ -n "$3" ]; then
   targetPath=$3
else
   targetPath=.
fi

# 日志文件
logFile=${srcPath}/UpopWeb.log.$dt*
# 生成的文件名,以yyyy-MM-dd结尾
formattedDate=${dt:0:4}-${dt:4:2}-${dt:6:2}
# 错误数据文件
errorReportFile=${targetPath}/errorReport.txt.$formattedDate
# 错误类型文件
errorReportTypeFile=${targetPath}/errorReportType.txt.$formattedDate

echo "<info>报文错误统计开始,日志文件:["$logFile"]"

# 判断文件是否存在
if (ls $logFile) >/dev/null 2>&1;then
	# 开始统计时间
	date_start=$(date +%s)
	
	# 统计原始表数据
	# 生成数据格式为:
	grep "=== OrderInfo Start ===" -A9 $logFile|
		grep "com.unionpay.upop.web.exceptions.ValidationException" -B8|
		awk 'BEGIN{FS="\n";RS="--\n"}{print $1,":" ,$2,":",$3,":",$4,":",$5,":",$6,":",$7,":",$9}'|
		awk -F ':' '{print $17,"|", $2,"|null|",$4,"|",$6,"|",$8,"|",$10,"|",$12,"|",$14,":",$15}'|
		awk '{gsub(/[ \t\n\r]/,"")}{print $1}'| 
		awk -v var="$formattedDate" '{print $1"|"var}'>$errorReportFile
	
	echo "<info>原始表数据文件生成完毕,路径:["$errorReportFile"]"
	
	# 统计错误类型
	createTime=`date +%Y-%m-%d\ %H:%M:%S`
	grep "=== OrderInfo Start ===" -A9 $logFile|
		grep "com.unionpay.upop.web.exceptions.ValidationException"|
		awk -v var="$createTime" -F ": " '{print $2"|"var}'|
		sort | uniq	>$errorReportTypeFile
		
	echo "<info>错误类型数据文件生成完毕,路径:["$errorReportTypeFile"]"
else
	echo "<error>["$logFile"]不存在"
fi

date_end=$(date +%s)
echo "<info>["$dt"]报文错误统计完毕,执行时间:$((date_end-date_start))秒"

猜你喜欢

转载自john-cn.iteye.com/blog/1678362