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

#!/usr/bin/ksh


WORKFILE="/tmp/df.work" # Holds filesystem data
>$WORKFILE              # Initialize to empty
OUTFILE="/tmp/df.outfile" # Output display file
>$OUTFILE  # Initialize to empty
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
THISHOST=`hostname` # Hostname of this machine
MIN_MB_FREE="50MB"     # Min. MB of Free FS Space


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

while read FSNAME FSLIMIT
do
    # 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
    fi
done < $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_EXCEPTIONS
fi

# 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 FSMOUNT
do
    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
    fi
done < $WORKFILE

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值