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

    技术2022-05-11  72

    #!/usr/bin/ksh

    WORKFILE="/tmp/df.work" # Holds filesystem data>$WORKFILE              # Initialize to emptyOUTFILE="/tmp/df.outfile" # Output display file>$OUTFILE  # Initialize to emptyEXCEPTIONS="/usr/local/bin/exceptions" # Override data fileDATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rowsTHISHOST=`hostname` # Hostname of this machineMIN_MB_FREE="50MB"     # Min. MB of Free FS Space

    function check_exceptions{# set -x # Uncomment to debug this function

    while read FSNAME FSLIMITdo    # Do an NFS sanity check    echo $FSNAME | grep ":" >/dev/null /         && FSNAME=$(echo $FSNAME | cut -d ":" -f2)    if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]    then        (( FSLIMIT = $(echo $FSLIMIT | sed s/MB//g) * 1024 ))        if [[ $FSNAME = $FSMOUNT ]]        then            # Get rid of the "MB" if it exists            FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)            if (( $FSMB_FREE < $FSLIMIT ))            then                return 1 # Found out of limit            else                return 2 # Found OK            fi        fi    fidone < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!

    return 3 # Not found in $EXCEPTIONS file}

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

    if [[ -s $EXCEPTIONS ]]then    # Ignore all line beginning with a pound sign, #    # and omit all blank lines

        cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONSfi

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

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

    # Format Variables for the proper MB value(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))

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

    while read FSDEVICE FSMB_FREE FSMOUNTdo    if [[ -s $EXCEPTIONS ]]    then      check_exceptions      RC="$?"      if [ $RC -eq 1 ] # Found out of exceptions limit      then          (( FS_FREE_OUT = $FSMB_FREE / 1000 ))           echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" /                >> $OUTFILE      elif [ $RC -eq 2 ] # Found in exceptions to be OK      then # Just a sanity check - We really do nothing here...           # The colon, :, is a NO-OP operator in KSH

              : # No-Op - Do Nothing!

          elif [ $RC -eq 3 ] # Not found in the exceptions file      then          FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"          if (( $FSMB_FREE < $MIN_MB_FREE ))          then              (( FS_FREE_OUT = $FSMB_FREE / 1000 ))              echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" /                    >> $OUTFILE          fi      fi    else # No Exceptions file use the script default      FSMB_FREE=$(echo $FSMB_FREE | sed s/MB//g) # Remove the "MB"      if (( $FSMB_FREE < $MIN_MB_FREE ))      then          (( FS_FREE_OUT = $FSMB_FREE / 1000 ))          echo "$FSDEVICE mounted on $FSMOUNT only has ${FS_FREE_OUT}MB Free" /                >> $OUTFILE      fi    fidone < $WORKFILE

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


    最新回复(0)