记录3——快速检查机器的各种实时错误日志并且及时定位问题———修订版1

任务需求:

(1)把相关类似的功能组合成一个函数调用,增加代码复用度,并且精简代码

(2)增加用户输错信息提示和判断

(3)判断所检察文件是否存在再进行检察

难点:

相关功能组合成一个函数时遇到了变量间接引用的问题,已在相应位置标注


该脚本还有有很多可以改进的地方(如有不好地方尽情指正)

#!/bin/bash
#       This program shows the user's choice
# History:
# Author :Murphy Ma
# Create: 2014/12/19
# Modify: 2015/01/15

function print_usage(){
        cat << !USAGE!
Usage:
        `basename $0` all             -----> All Check
        `basename $0` fds             -----> $fds_path 
        `basename $0` pm              ------> $pm_path 
        `basename $0` dteagent        ------> $dteagent_path
        `basename $0` backup          ------> $backup_path
        `basename $0` tomcat          ------> $tomcat_path
        `basename $0` fdsweb          ------> $fdsweb_path
        `basename $0` exit            ------>  exit this script
!USAGE!
}

function importPath(){
        os_path=/var/log/messages
        fds_path=/var/sog/logs/fdsserver/FDS-PL.log
        pm_path=/var/sog/logs/pm/pm-PL.log
        dteagent_path=/var/sog/logs/dteagent/dteagent.log
        backup_path=/var/sog/logs/backup/backup.log
        tomcat_path=/var/sog/logs/tomcat/tomcat.log
        fdsweb_path=/var/sog/logs/www/fdsweb.log
}

function excuteLog(){
        [ -d /var/tmp/errorLogCheck ] || mkdir -p /var/tmp/errorLogCheck
        datetime=`date +"%Y%m%d%H%M%S"`
        logpath="/var/tmp/errorLogCheck/"$1"_"$datetime".log"
        needCheckPath="$1"_path
        #注释 
        #这里需要一个简介变量引用
        #假设 $1 = os 则needCheckPath =os_path ;直接使用$needCheckPath 时则输出的结果是os_path ,而非os_path对应的值/var/log/messages ; 因此此地方需要特别注意
        #echo ${!needCheckPath} 与 eval s=\$$needCheckPath echo $s是同等的
        if [ -f "${!needCheckPath}" ];then
                echo "===============Start time:" `date +"%Y-%m-%d %T"` "===============" >> $logpath
                echo "===============log file is :" $needCheckPath"            ===============" >> $logpath
                echo "===============log file is located in :" $logpath
                log_error_tmp=`cat ${!needCheckPath} | grep -ni $findMessage  >> $logpath`
        else
                echo ${!needCheckPath} "is not exist!!"
        fi
}

importPath
print_usage   
 
until [ "$item" == "exit" ]
do
        read -p "input a chect item: " item
        echo "this round is to check logfile: " $item
        findMessage="error"
        case $item in
                all)
                        importPath
                        excuteLog "os"
                        excuteLog "fds"
                        excuteLog "pm"
                        excuteLog "dteagent"
                        excuteLog "backup"
                        excuteLog "tomcat"
                        excuteLog "fdsweb"
                        ;;
                os)
                        importPath
                        excuteLog "os"
                        ;;
                fds)    
                        importPath  
                        excuteLog "fds"
                        ;;
                pm)
                        importPath
                        excuteLog "pm"
                        ;;
                dteagent)
                        importPath
                        excuteLog "dteagent"
                        ;;
                backup)
                        importPath
                        excuteLog "backup"
                        ;;       
                tomcat)
                        importPath
                        excuteLog "tomcat"
                        ;;   
                fdsweb)
                        importPath
                        excuteLog "fdsweb"
                        ;; 
                h|help)
                        print_usage
                        echo "Usage $item {all|os|fds|pm|dteagent|backup|tomcat|fdsweb}"
                        ;;
                *)
                        echo "Invalid option -- '$1' "
                        print_usage
                        exit 1
                        ;;
                esac
done

你可能感兴趣的:(记录3——快速检查机器的各种实时错误日志并且及时定位问题———修订版1)