返回顶部

收藏

把tomcat里面的项目生成的日志进行分析切割和分类

更多

针对每个tomcat项目生成的日志进行分析切割,把异常信息导入一份文件里面分析统计

这段代码只能参考,要复制使用需要根据自己的实际情况来修改

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
clear

logs_path="/var/log/tomcat"
pro_logs_path="/path/tomcatlog/processed"
ratedate=$(date -d "Yesterday" +"%Y-%m-%d")
app_grp=(s1 s2 s3 s4 s5)
function log_cut()
{
    for i in ${app_grp[@]}
    do
        for j in $(cd $logs_path ; echo $i.log.*)
        do
            [ $j = $i.log.\\* ] && continue
            ratemonth=`echo ${j##*.}|awk -F '-' '{print $1"-"$2}'`
            [ ! -f $pro_logs_path/$i\\_$ratemonth.txt ] && touch $pro_logs_path/$i\\_$ratemonth.txt
            if [ "s1" = $i -o "s2" = $i ] ; then
                [ $ratedate != ${j##*.} ] && grep -v 'INFO' $logs_path/$j >> $pro_logs_path/$i\\_$ratemonth\\.txt || grep -v 'INFO' $logs_path/$j >> $pro_logs_path/$i\\_$ratedate\\.txt
            elif [ "s3" = $i ] ; then
                [ $ratedate != ${j##*.} ] && egrep -v 'INFO|TransServlet.java:36' $logs_path/$j >> $pro_logs_path/$i\\_$ratemonth\\.txt || egrep -v 'INFO|TransServlet.java:36' $logs_path/$j >> $pro_logs_path/$i\\_$ratedate\\.txt
            elif [ "s4" = $i ] ; then
                [ $ratedate != ${j##*.} ] && egrep -v 'INFO|InceptData=true' $logs_path/$j >> $pro_logs_path/$i\\_$ratemonth\\.txt || egrep -v 'INFO|InceptData=true' $logs_path/$j >> $pro_logs_path/$i\\_$ratedate\\.txt
            elif [ "s5" = $i ] ; then
                [ $ratedate != ${j##*.} ] && egrep 'threw|at ' $logs_path/$j >> $pro_logs_path/$i\\_$ratemonth\\.txt || egrep 'threw|at ' $logs_path/$j >> $pro_logs_path/$i\\_$ratedate\\.txt
            fi
            rm $logs_path/$j
        done
    done
}

function hmd_cut()
{
    for k in $(cd $logs_path ; echo *0000.txt)
    do
        mddate=${k:2:6}
        if [ $mddate != $(date -d "last month" +"%Y%m") -a $mddate != $(date +"%Y%m") ] ; then
            ls -lh $logs_path/$k|awk '{print $NF,$5}'|sed 's/\\/var\\/log\\/tomcat\\///g' >> $pro_logs_path/hmd.txt
            rm $logs_path/$k
        fi
    done
}

function txt_clear_up()
{
for w in ${app_grp[@]}
do
    for tt in $(cd $pro_logs_path ; echo $w.*)
    do
        ratemonth=`awk -F '-|\\_' '{print $2"-"$3}' $tt`
        [ $tt != $w\\_$ratemonth\\.txt -a $tt != $w\\_$ratedate\\.txt ] && cat $pro_logs_path/$tt >> $pro_logs_path/$w\\_$ratemonth\\.txt
        rm $pro_logs_path/$tt
    done
done
}

log_cut
hmd_cut
txt_clear_up
#ls -lh $pro_logs_path
#该片段来自于http://outofmemory.cn

标签:shell,文件

收藏

0人收藏

支持

0

反对

0

发表评论