zoukankan      html  css  js  c++  java
  • centos记录uptime,tomcat日志切割,远程拷贝日志脚本

    1.uptime日志脚本(每天记录)

    #!/bin/sh
    
    dir=/tmp/uptime_log
    
    process=`ps -ef|grep $0|grep -v "grep" |grep -v "vim"|grep -v "ps"|wc -l`
    if [ $process -gt 2 ];then
      echo $process
      exit;
    fi
    
    if [ ! -d $dir ];then
      mkdir -p $dir;
    fi
    
    while true
    do
      current_time=`date -d today +"%Y_%m_%d"`
      uptime >> ${dir}/${current_time}_system_check.txt
      sleep 902;
      find $dir -mtime +${time} -type f|xargs rm -rf
      chown -R xxadmin.xxadmin ${dir}/*
    done

    2.tomcat日志切割

    #!/bin/bash
    file1='/usr/local/tomcat7/logs'
    file2='/usr/local/tomcat7-2/logs'
    file3='/usr/local/tomcat7-3/logs'
    file4='/usr/local/tomcat7-4/logs'
    logtime="`date +20%y_%m_%d`"
    time=30
    for i in `seq 1 4`
    do
    {
            rm -rf tmp_${i}.txt
            eval echo $file${i} > tmp_${i}.txt
            file=`cat tmp_${i}.txt`
            cp $file/catalina-daemon.out $file/catalina-daemon_${logtime}.out
            if [ $? -eq 0 ];then
                    echo "" > $file/catalina-daemon.out
            fi
    
            chown zxadmin.zxadmin $file/catalina-daemon_${logtime}.out
    
            find $file -mtime +${time} -type f|grep catalina-daemon_*.out|xargs rm -rf
            rm -rf tmp_${i}.txt
    
    }
    done

    3.远程拷贝日志脚本

    #!/bin/sh
    
    time=`date -d "1 day ago" +"%Y_%m_%d"`
    
    file=catalina-daemon_${time}.out
    dir1=/usr/local/tomcat7/logs
    dir2=/usr/local/tomcat7-2/logs
    dir3=/usr/local/tomcat7-3/logs
    dir4=/usr/local/tomcat7-4/logs
    remote_ser1=111.111.111.1
    remote_ser2=111.111.111.2
    remote_ser3=111.111.111.3
    local_dir=/tmp/log_list
    times=30
    pro1=xxx_web
    pro2=xxx_api
    pro3=xxx_manage
    pro4=xxx_forward
    
    uptime_log_dir=/tmp/uptime_log
    uptime_file=${time}_system_check.txt
    local_uptime_file1=xxx_web
    local_uptime_file2=xxxx_db
    local_uptime_file3=xxx_nginx
    
    if [ ! -d $local_dir/$time ];then
      mkdir -p $local_dir/$time;
    fi
    rm -rf $local_dir/$time/*
    
    #tomcat_log
    for i in `seq 1 4`
    do
    {
    
            rm -rf /tmp/.tmp_a_${i}.txt
            eval echo $dir${i} > /tmp/.tmp_a_${i}.txt
            dir=`cat /tmp/.tmp_a_${i}.txt`
            
        rm -rf /tmp/.tmp_b_${i}.txt
            eval echo $pro${i} > /tmp/.tmp_b_${i}.txt
            pro=`cat /tmp/.tmp_b_${i}.txt`
    
        ssh $remote_ser1 "cd ${dir};tar -zcf catalina-daemon_${time}.out.gz catalina-daemon_${time}.out > /dev/null 2>&1"
            if [ $? -ne 0 ];then
                    echo `date`'--->tar tomcat_log faile' >> $local_dir/$time/error.log;
            exit;
            fi
    
        scp $remote_ser1:$dir/$file.gz $local_dir/$time > /dev/null 2>&1
            if [ $? -ne 0 ];then
                    echo `date`'--->scp tomcat_log.gz faile' >> $local_dir/$time/error.log;
                    exit;
            fi
    
        ssh $remote_ser1 'rm -rf $dir/${file}.gz > /dev/null 2 >& 1'
            if [ $? -ne 0 ];then
                    echo `date`'--->rm the tomcat_log.gz file faile' >> $local_dir/$time/error.log;
                    
            fi
    
        cd $local_dir/$time;tar -zxf $local_dir/$time/${file}.gz -O > ${pro}_${file};
    
    
    
            find $local_dir -mtime +${times} -type d |grep catalina*.out|xargs rm -rf
    
    }
    done
    
    #uptime_log
    for i in `seq 1 3`
    do 
    
            rm -rf /tmp/.tmp_c_${i}.txt
            eval echo $remote_ser${i} > /tmp/.tmp_c_${i}.txt
            remote_ser=`cat /tmp/.tmp_c_${i}.txt`
    
            rm -rf /tmp/.tmp_d_${i}.txt
            eval echo $local_uptime_file${i} > /tmp/.tmp_d_${i}.txt
            local_uptime_file=`cat /tmp/.tmp_d_${i}.txt`
    
        ssh $remote_ser "cd ${uptime_log_dir};tar -zcf ${uptime_file}.gz ${uptime_file} "
        if [ $? -ne 0 ];then
          echo `date`'--->tar uptime_file faile' >> $local_dir/$time/error.log;
          exit;
        fi
    
        scp $remote_ser:${uptime_log_dir}/${uptime_file}.gz $local_dir/$time > /dev/null 2>&1
        if [ $? -ne 0 ];then
          echo `date`'--->scp uptime_file faile' >> $local_dir/$time/error.log;
          exit;
        fi
    
        ssh $remote_ser 'rm -rf ${uptime_log_dir}/${uptime_file}.gz > /dev/null 2 >& 1'
        if [ $? -ne 0 ];then
            echo `date`'--->rm uptime_file.gz  faile' >> $local_dir/$time/error.log;
            exit;
        fi
    
        cd $local_dir/$time;tar -zxf $local_dir/$time/${uptime_file}.gz -O > ${local_uptime_file}_${uptime_file};
    done
    rm -rf $local_dir/$time/*.gz
    rm -rf /tmp/.tmp_*.txt
    
    /home/zxadmin/filter_xdaili_log.sh
    if [ $? -ne 0 ];then
      echo `date`'--->filter xdaili log file faile!' >> $local_dir/$time/error.log;
    fi

    4.日志过滤

    #!/bin/bash
    
    time=`date -d "1 day ago" +"%Y_%m_%d"`
    local_dir=/tmp/log_list
    files=`ls ${local_dir}/${time}|grep "xxdaili"`
    dir1=filter_files
    
    cd ${local_dir}/$time
    if [ ! -d $dir1 ];then
      mkdir -p  $dir1;
    fi
    rm -rf $dir1/*
    
    for i in $files
    do
      if [ ! -f $i ];then
        echo  $i'  not found' >>  $dir1/error.log;
      else
        name=`echo $i|awk -F"." '{print$2}'`
        if [ $name = "txt" ];then
            cat $i |grep -v "min"|awk '{print$1"	"$12}' > $dir1/$i
        elif [ $name = "out" ];then
            cat $i |grep 'Exception|at ' > $dir1/$i
        fi
    
      fi
        
    done
  • 相关阅读:
    三十四:布局之混合布局、圣杯布局、双飞翼布局
    三十三:布局之经典的列布局
    三十二:布局之经典的行布局
    三十一:CSS之CSS定位之position
    三十:CSS之用浮动实现网页的导航和布局
    二十九:CSS之浮动float
    二十八:CSS之列表list-type
    二十七:CSS之背景background
    二十六:CSS之盒子模型之小案例
    二十五:CSS之盒子模型之display属性
  • 原文地址:https://www.cnblogs.com/vijayfly/p/6439755.html
Copyright © 2011-2022 走看看