一个日志自动搜索解压脚本

服务器上的日志每隔半小时压缩成一个zip包,编写了一个脚本在zip包中搜索指定模块的指定关键字,同时可以设置搜索时间范围。

#!/bin/bash

# -------------------------------------------------------------
#   Unzip and search assigned key words in trace zip files
# -------------------------------------------------------------

trace_unzipped=trace_unzipped
output_file=`basename $0 .sh`.log
trace_path=/var/ftp/trace
trace_module=*lte*
whether_append=0
whether_delete=0
whether_unzip=0
whether_print=0
whether_regular=E
key_words="RSSI|BS"
start_time=
stop_time=
hours=0
version=3

function processfile()
{
    if [ $# -le 0 ];
    then
        return
    fi

    filename=$1

    pure_name=`basename $filename`
    # echo "pure_name = $pure_name, filename = $filename, trace_path = $trace_path"

    if [ -n "$start_time" ] && [ -n "$stop_time" ];
    then
        # from start time to stop time
        file_cnt=`find $trace_path -name $pure_name -newermt "$start_time" ! -newermt "$stop_time" | wc -l`
        # echo "[$start_time ~ $stop_time] pure name is $pure_name, file count is $file_cnt"
        if [ $file_cnt -eq 0 ];
        then
            return
        fi
    elif [ -n "$start_time" ];
    then
        # from start time to now
        file_cnt=`find $trace_path -name $pure_name -newermt "$start_time" | wc -l`
        # echo "[$start_time ~ now) pure name is $pure_name, file count is $file_cnt"
        if [ $file_cnt -eq 0 ];
        then
            return
        fi
    elif [ -n "$stop_time" ];
    then
        # from past to stop time
        file_cnt=`find $trace_path -name $pure_name ! -newermt "$stop_time" | wc -l`
        # echo "(past ~ $stop_time] pure name is $pure_name, file count is $file_cnt"
        if [ $file_cnt -eq 0 ];
        then
            return
        fi
    fi

    extension=${filename##*.}
    # echo "extension of '$filename': $extension"

    if [ $extension == "zip" ];
    then
        # This is a zip file
        extractpath=$trace_path/$trace_unzipped/`basename $filename`
        # echo "extract path is '$extractpath'"

        if [ $whether_unzip -eq 1 ];
        then
            unzip -o $filename $trace_module -d $extractpath >/dev/null 2>&1
            # echo "unzip -o $filename $trace_module -d $extractpath"
        fi

        # echo "Analyzing zipped trace, please wait ..."
        grep -nH$whether_regular "$key_words" $extractpath/$trace_module >> $output_file
        # temp=`grep -n$whether_regular "$key_words" $extractpath/$trace_module`
        # echo -e $temp
        # echo -e $temp >> $output_file

        if [ $whether_delete -eq 1 ];
        then
        # echo "Deleting unzipped trace, please wait ..."
            rm -rf $extractpath
        fi
    else
        # This is not a zip file
		# echo trace module is $trace_module, pure name is $pure_name
        if [[ $pure_name == $trace_module ]]
        then
            grep -nH$whether_regular "$key_words" $filename >> $output_file
        fi
    fi
}

function reviewdir()
{
    for element in `ls $1`
    do
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ]
        then
            reviewdir $dir_or_file
        else
            processfile $dir_or_file
        fi
    done
}

while getopts "H:B:E:C:O:M:K:k:aduphv" opt
do
    case $opt in
            H ) hours=$OPTARG;;
            B ) start_time=$OPTARG;;
            E ) stop_time=$OPTARG;;
            C ) trace_path=$OPTARG;;
            O ) output_file=$OPTARG;;
            M ) trace_module=*$OPTARG*;;
            K ) key_words=$OPTARG;;
            k ) whether_regular=
                key_words=$OPTARG;;
            a ) whether_append=1;;
            d ) whether_delete=1;;
            u ) whether_unzip=1;;
            p ) whether_print=1;;
            v ) echo "$0 version $version"
                exit 0;;
            h ) echo "Unzip and search assigned key words in trace of BSC/MSC, version $version"
                echo "Default: Search RSSI data in lte trace"
                echo "Usage: $0 [options]"
                echo "Options:"
                echo "  -C <Directory>  Directory to analyze, default '$trace_path'."
                echo "  -O <Output>     Output file name, default '$output_file'."
                echo "  -M <Module>     Which module to search, default '$trace_module'."
                echo "  -K <Key words>  Regular  key words to search, default regular '$key_words'."
                echo "  -k <Key words>  Ordinary key words to search, default regular '$key_words'."
                echo "  -B <Start Time> Start time, default NULL."
                echo "  -E <Stop  Time> Stop time, default NULL."
                echo "  -H <Hours>      Time interval in hours, 0 for all the time."
                echo "  -a              Append trace file without recover it."
                echo "  -d              Delete unzipped file after analyzing."
                echo "  -u              Need to unzip file."
                echo "  -p              Print search results."
                echo "  -v              Print version of this shell."
                echo "  -h              Print this help information."
                echo "Example: $0 -duM brucheck.log -k \"lte\""
                exit 0;;
            ? ) exit 1;;
    esac
done

if [ ! -d $trace_path/$trace_unzipped ];
then
    mkdir $trace_path/$trace_unzipped
fi

if [ $whether_append -eq 0 ];
then
    rm -f $output_file
fi

if [ -z "$start_time" ] && [ -z "$stop_time" ] && [ $hours -gt 0 ];
then
    # hours ago to now
    start_time=`date -d "$hours hour ago" +"%Y-%m-%d %H:%M:%S"`
    stop_time=`date -d "10 minute" +"%Y-%m-%d %H:%M:%S"`
elif [ -z "$start_time" ] && [ -n "$stop_time" ] && [ $hours -gt 0 ];
then
    # hours before stop time
    let timestamp=`date -d "$stop_time" +%s`-$hours*3600
    start_time=`date -d @$timestamp +"%Y-%m-%d %H:%M:%S"`
elif [ -n "$start_time" ] && [ -z "$stop_time" ] && [ $hours -gt 0 ];
then
    # hours after start time
    let timestamp=`date -d "$start_time" +%s`+$hours*3600
    stop_time=`date -d @$timestamp +"%Y-%m-%d %H:%M:%S"`
fi

# echo "Time interval [$start_time, $stop_time]"
echo "Analyzing trace, please wait ..."

reviewdir $trace_path

if [ $whether_print -eq 1 ];
then
    cat $output_file
fi

if [ $whether_delete -eq 1 ];
then
    # echo "Deleting unzipped trace, please wait ..."
    rm -rf $trace_path/$trace_unzipped
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值