使用shell脚本进行服务器系统监控——文件系统监控(2)

    技术2022-05-11  67

    #!/usr/bin/ksh

    WORKFILE="/tmp/df.work" # Holds filesystem data>$WORKFILE              # Initialize to emptyOUTFILE="/tmp/df.outfile" # Output display file>$OUTFILE  # Initialize to emptyBINDIR="/usr/local/bin" # Local bin directoryTHISHOST=`hostname` # Hostname of this machineFSMAX="85"              # Max. FS percentage value

    EXCEPTIONS="${BINDIR}/exceptions" # Overrides $FSMAXDATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o #, comments

    function load_EXCEPTIONS_file{cat $EXCEPTIONS |  grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS}

    function check_exceptions{

    # set -x # Uncomment to debug this function

    while read FSNAME NEW_MAX # Feeding Ddata from Bottom of Loop!!!do        if [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?        then    # Get rid of the % sign, if it exists!                NEW_MAX=$(echo $NEW_MAX | sed s//%//g)

                    if [ $FSVALUE -gt $NEW_MAX ]                then #  Over Limit...Return a "0", zero                        return 0 # FOUND MAX OUT - Return 0                fi        fi

    done < $DATA_EXCEPTIONS # Feed from the bottom of the loop!!

    return 1 # Not found in File}

    ######## START OF MAIN #############

    [[ -s $EXCEPTIONS ]] && load_EXCEPTIONS_file

    # Get the data of interest by stripping out the# /cdrom row and keeping columns 1, 5 and 6

    df -k | tail +2 | egrep -v '/cdrom' /   | awk '{print $1, $5, $6}' > $WORKFILE

    # Loop through each line of the file and compare column 2

    while read FSDEVICE FSVALUE FSMOUNTdo     # Strip out the % sign if it exists     FSVALUE=$(echo $FSVALUE | sed s//%//g) # Remove the % sign     if [[ -s $EXCEPTIONS ]] # Do we have a non-empty file?     then # Found it!

            # Look for the current $FSMOUNT value in the file        # using the check_exceptions function defined above.

            check_exceptions        RC=$?        if [ $RC -eq 0 ] # Found it Exceeded!!        then            echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" /                  >> $OUTFILE        elif [ $RC -eq 1 ] # Not founf in exceptions file, use defaults        then             if [ $FSVALUE -gt $FSMAX ] # Use Script Default             then                  echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" /                        >> $OUTFILE             fi        fi     else # No exceptions file use the script default             if [ $FSVALUE -gt $FSMAX ] # Use Script Default             then                  echo "$FSDEVICE mount on $FSMOUNT is ${FSVALUE}%" /                        >> $OUTFILE             fi     fidone < $WORKFILE # Feed the while loop from the bottom...

    # Display output if anything is exceeded...

    if [[ -s $OUTFILE ]]then      echo "/nFull Filesystem(s) on $THISHOST/n"      cat $OUTFILE      printfi 


    最新回复(0)