返回顶部

收藏

把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

»更多 您可能感兴趣的代码
  1. 2013-07-18 19:19:38shell删除某个目录以其子目录下的某些文件 by 好剑
  2. 2014-03-16 16:00:59输出倒文 by 凤歌
  3. 2014-04-20 16:56:45shell更新jar包内容 by Echo
  4. 2014-08-05 10:02:24将二进制转化为ascii文本 by zjbpku
  5. 2014-08-28 10:34:53Linux 查找指定文件并删除 by 屠夫
  6. 2014-03-16 16:35:48枚举当前目录及子目录大小 by xfk
  7. 2014-04-20 17:16:34keepalived配置文件详解 by Echo
  8. 2014-04-22 21:10:08统计python项目得代码行数 by Smile_love
  9. 2014-05-06 16:31:47Powershell操作Word by 好剑
  10. 2014-09-17 11:10:55从zip中解压部分文件 by 好剑
  11. 2014-10-30 09:55:34批量查找替换文件名 by 凤歌

发表评论