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

释放双眼,带上耳机,听听看~!

#!/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
EXCEPT_FILE="N"        # Assume no $EXCEPTIONS FILE
THISHOST=

1
1`

hostname

1
1`

    # Hostname of this machine

MIN_MB_FREE="100MB"    # Min. MB of Free FS Space
MAX_PERCENT="85%"      # Max. FS percentage value
FSTRIGGER="1000MB"     # Trigger to switch from % Used to MB Free

# Both of these variables need to multiplied by 1024 blocks
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g)  * 1024 ))

function get_OS_info
{
typeset -u OS  
OS=

1
1`

uname

1
1`

  
print $OS  
}

function check_exceptions
{

while read FSNAME FSLIMIT
do
IN_FILE="N"

    echo $FSNAME | grep ':' >/dev/null /
&& FSNAME=$(echo $FSNAME | cut -d ':' -f2)

    if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]
then
if [[ $FSNAME = $FSMOUNT ]] # Found it!
then
# Check for "MB" Characters…Set IN_FILE=MB
echo $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" /
&& (( FSLIMIT = $(echo $FSLIMIT /
| sed s/MB//g) * 1024 )) >/dev/null 2>&1
# check for "%" Character…Set IN_FILE=PC, for %
echo $FSLIMIT | grep "%" >/dev/null && IN_FILE="PC" /
&& FSLIMIT=$(echo $FSLIMIT | sed s//%//g) /
>/dev/null 2>&1
case $IN_FILE in
MB)
# Up-case the characters, if they exist
FSLIMIT=$(echo $FSLIMIT | tr '[a-z]' '[A-Z]')
# Get rid of the "MB" if it exists
FSLIMIT=$(echo $FSLIMIT | sed s/MB//g)
# Test for blank and null values
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
then
# Test for a valid filesystem "MB" limit
if (( $FSLIMIT >= 0 && $FSLIMIT < $FSSIZE ))
then
if (( $FSMB_FREE < $FSLIMIT ))
then
return 1 # Found out of limit using MB Free method
else
return 3 # Found OK
fi
else
(( FSLIMIT = $FSLIMIT / 1024 )) # To get a good display value
(( FSSIZE = $FSSIZE / 1024 ))   # To get a good dispaly value
echo "/nERROR: Invalid filesystem MINIMUM MB FREE specified"
echo "for $FSMOUNT – ${FSLIMIT}MB — Current size is ${FSSIZE}MB."
echo "Exceptions file value must be less than or equal to the"
echo "size of the filesystem measured Megabytes/n"
fi
else
echo "/nERROR: Null value specified in excepeptions file"
echo "       for the $FSMOUNT mount point./n"
fi
;;
PC)
# Strip out the % sign if it exists
PC_USED=$(echo $PC_USED | sed s//%//g)
# Test for blank and null values
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
then
# Test for a valid percentage, i.e. 0-100
if (( $FSLIMIT >= 0 && $FSLIMIT <= 100 ))
then
if (( $PC_USED > $FSLIMIT ))
then
return 2 # Found exceeded by % Used method
else
return 3 # Found OK
fi
else
echo "/nERROR: Invalid percentage for $FSMOUNT – $FSLIMIT"
echo "       Exceptions file values must be"
echo "       between 0 and 100%/n"
fi
else
echo "/nERROR: Null value specified in excepeptions file"
echo "       for the $FSMOUNT mount point./n"
fi
;;
N)
# Method Not Specified – Use Script Defaults
if (( $FSSIZE >= $FSTRIGGER ))
then # This is a "large" filesystem
if (( $FSMB_FREE < $MIN_MB_FREE ))
then
return 1 # Found out of limit using MB Free method
else
return 3 # Found OK
fi
else # This is a standard filesystem
PC_USED=$(echo $PC_USED | sed s//%//g) # Remove the %
FSLIMIT=$(echo $FSLIMIT | sed s//%//g) # Remove the %
if (( $PC_USED > $FSLIMIT ))
then
return 2 # Found exceeded by % Used method
else
return 3 # Found OK
fi
fi
;;
esac
fi
fi
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!

return 4 # Not found in $EXCEPTIONS file
}

####################################

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

####################################

function load_EXCEPTIONS_data
{
# Ingore any line that begins with a pound sign, #
# and omit all blank lines

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

####################################

function load_AIX_FS_data
{

   df -k | tail +2 | egrep -v '/dev/cd[0-9]|/proc' /
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
}

####################################

function load_HP_UX_FS_data
{

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

####################################

function load_LINUX_FS_data
{

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

####################################

function load_Solaris_FS_data
{

   df -k | tail +2 | egrep -v '/dev/fd|/etc/mnttab|/proc'/
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
}

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

# THIS_OS=

1
1`

get_OS_info

1
1`

# Execute the function

# case $THIS_OS in
case $(get_OS_info) in
AIX)   # Load filesystem data for AIX
load_AIX_FS_data
;;
HP-UX) # Load filesystem data for HP-UX
load_HP_UX_FS_data
;;
LINUX) # Load filesystem data for Linux
load_LINUX_FS_data
;;
SUNOS) # Load filesystem data for Solaris
load_Solaris_FS_data
;;
*)     # Unsupported in script
echo "/nUnsupported Operating System for this Script…EXITING/n"
exit 1
esac

# Do we have a non-zero size $EXCEPTIONS file?

if [[ -s $EXCEPTIONS ]]
then # Found a non-empty $EXCEPTIONS file

    load_EXCEPTIONS_data
EXCEP_FILE="Y"
fi

while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
do
if [[ $EXCEP_FILE = "Y" ]]
then
check_exceptions
CE_RC="$?" # Check Exceptions Return Code (CE_RC)

         case $CE_RC in
1) # Found exceeded in exceptions file by MB Method
(( FS_FREE_OUT = $FSMB_FREE / 1000 ))
echo "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" /
>> $OUTFILE
;;
2) # Found exceeded in exceptions file by %Used method
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" /
>> $OUTFILE
;;
3) # Found OK in exceptions file
: # NO-OP Do Nothing
;;

         4) # Not found in exceptions file – Use Default Triggers
if (( $FSSIZE >= $FSTRIGGER ))
then # This is a "large" filesystem
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 has ${FS_FREE_OUT}MB Free" /
>> $OUTFILE
fi
else # This is a standard filesystem
PC_USED=$(echo $PC_USED | sed s//%//g)
MAX_PERCENT=$(echo $MAX_PERCENT | sed s//%//g)
if (( $PC_USED > $MAX_PERCENT ))
then
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" /
>> $OUTFILE
fi
fi
;;
esac

     else # NO $EXECPTIONS FILE USE DEFAULT TRIGGER VALUES

          if (( $FSSIZE >= $FSTRIGGER  ))
then # This is a "large" filesystem – Use MB Free Method
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 has ${FS_FREE_OUT}MB Free" /
>> $OUTFILE
fi
else # This is a standard filesystem – Use % Used Method
PC_USED=$(echo $PC_USED | sed s//%//g)
MAX_PERCENT=$(echo $MAX_PERCENT | sed s//%//g)
if (( $PC_USED > $MAX_PERCENT ))
then
echo "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" /
>> $OUTFILE
fi
fi
fi
done < $WORKFILE

display_output

# End of Script 

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

网站制作需要素材的实用网站

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索