1. Check if file system more than 75% full and remove files which are older than +mtime
  2. it checks with specific mount file system who has RW access on the server

#!/bin/sh
# Script used to cleanup any Oracle environment.
# Author: Sandeep Reddy
# Version: V1
# Tested: dbadeeds.com servers
# Cleans: Zfs filesystem directories

########################################################
FILESYSTEM_SpaceThreshold=75            ### Edit the threshold value for Filesystem used percent
#recipient=sandeep    ### Edit the recipient address for sending email. If there are more recipient then add them with comma(',') as sperator
recipient=dbadeeds.com
logs="/tmp/zfs_cleanup"
prepend_sub="(ZFS Mount Cleanup $FILESYSTEM_SpaceThreshold Threshold Alert: `hostname`)"
output_file=${logs}/Alert.log
dflog=${logs}/logs.log
touch $dflog
touch $output_file
LS="ls -ltr"
RM="rm -rf"
######################################################
#  Below is for seperation and sending alerts to mail
######################################################
seperate()
{
${ECHO} "\n=============================================================================" >> ${logs}/Alert.log
}
divide()
{
echo "------------------" >> ${logs}/Alert.log
}
fdivide()
{
${ECHO} "\n\n------------------" >> ${logs}/Alert.log
}
notify()
{
case ${uname} in
Linux)
mailx -s "${prepend_sub}" ${recipient} <  ${logs}/Alert.log
;;
*)
mailx -s "${prepend_sub}" ${recipient} <  ${logs}/Alert.log
;;
esac
}
#####################################################
#####################################################
# Filesystem Space Check Method
#####################################################
CheckSpace ()
{
> ${logs}/fsspace_Check.log
case ${uname} in
Linux)
#df -k | tail -n+2 |grep % | egrep -v '/mnt/cdrom|/dev/fd|/etc/mnttab|/proc|/dev/cd[0-9]' > ${logs}/logs.log

for getrwzfs in `findmnt -s | egrep -i 'zfss|/dev/cd[0-9]' | grep -i "rw" | awk '{print $1}'`; do
echo "Displaying only Read-Write ZFS File systems"
df -k | tail -n+2 |grep % | egrep -i $getrwzfs >> ${logs}/logs.log
done
esac

ALRTCNT3=`cat ${logs}/logs.log | wc -l`
ALRTSTA3=0
if [ ${ALRTCNT3} -gt ${ALRTSTA3} ]; then
    cat ${logs}/logs.log | while read fsdisk
        do
        #echo "fsdisk = $fsdisk"
        Arg2=$(echo $fsdisk | ${AWK} -F '%' '{print $1}')
        Partition2=$(echo $fsdisk | ${AWK} -F '%' '{print $2}')
        kount=`echo $Arg2 |wc -c`
        Percnt2=`echo $Arg2 | cut -c$((${kount}-2))-$((${kount}-1)) | ${AWK} '{print $1}'`

        if [ ${Percnt2:-0} -gt ${FILESYSTEM_SpaceThreshold} ]
        then
            ${ECHO} "\n The filesystem $Partition2 is ($Percnt2%) used on Server `hostname` as of `date`\n" >> ${logs}/Alert.log
            #df -Th | grep '${Partition2}' | awk '{print $1}'
            ${ECHO} "\n The filesystem $Partition2 is ($Percnt2%) used on Server `hostname` as of `date`\n"
            ${ECHO} "\n ZFS " "$(df ${Partition2} | awk '{print $1}') \n"
            seperate
            ${ECHO} "\n The filesystem $Partition2 is ($Percnt2%) used on Server `hostname` as of `date`\n" >> ${logs}/Alert.log
            echo "Getting Directories list under this File System" >> ${logs}/Alert.log
            for f in ${Partition2}/*; do

                if [ -d "$f" ]; then
                    # Will not run if no directories are available
                    divide
                    echo "\n Removing files which are older than 365 days in Directory $f : ZFS ${Partition2} \n" >> ${logs}/Alert.log
                    find $f/* -type f -mtime +365 -exec $RM {} \; >> ${logs}/Alert.log
                    #divide
                    #echo "\n Removing files older than 365 days in ZFS ${Partition2} Mount Directories\n"  >> ${logs}/Alert.log
                    #find $f/* -type f -mtime +365 -exec $RM {} \; >> ${logs}/Alert.log

                fi
            done
            seperate
        fi
    done
    echo "No ZFS Filesystems Found on this server `hostname` and date `date` less than $FILESYSTEM_SpaceThreshold Threshold" >> ${logs}/Alert.log
    echo "No ZFS Filesystems Found on this server `hostname` and date `date` less than $FILESYSTEM_SpaceThreshold Threshold"
else
    echo "No ZFS Read-Write filesystems found `hostname` and date `date`"
    echo "ERROR: Filesystem cannot be monitor, please monitor it manually" >> ${logs}/Alert.log
    echo "ERROR: Filesystem cannot be monitor, please monitor it manually"
    echo "No ZFS Read-Write filesystems found `hostname` and date `date`" >> ${logs}/Alert.log
fi
}
#####################################################
#####################################################
# Network Errors and Packet drops
#####################################################
netstat_check()
{
netstat -s | awk 'NF==1 {
 protocol=$0
 next}
  {match($0,"fragments dropped") || match($0,"packet receive errors") || match($0,"udpInOverflows")
if (RSTART>0) {
 printf("%s %s\n",protocol, $0)
RSTART=0
}
}
' >> ${logs}/Alert.log
}
#######################################################################
#######################################################################
# Checking the Unix Flavour and substituting few commands
#######################################################################
uname=`uname`
case ${uname} in
SunOS) AWK=nawk
ora_tab=/var/opt/oracle/oratab
;;
AIX) AWK=awk
ora_tab=/etc/oratab
;;
Linux) ora_tab=/etc/oratab
AWK=awk
;;
*) ora_tab=/etc/oratab
AWK=awk
;;
esac
case $SHELL in
*/bin/bash) ECHO="echo -e"
;;
*/bin/Bash) ECHO="echo -e"
;;
*/bin/sh) ECHO="echo -e"
;;
*) ECHO=echo
;;
esac
##################################################################################
# Calling Filesystem space function
##################################################################################
seperate
divide
echo "Checking Filesystem space on server `hostname`" >> ${logs}/Alert.log
divide
CheckSpace
fdivide
echo "Checking the Network drops" >> ${logs}/Alert.log
divide
netstat_check
seperate
notify
#mailx -s "${prepend_sub}" ${recipient} `hostname`@monitoring.com -f ${sender} <  ${logs}/Alert.log
mv ${logs}/logs.log ${logs}/logs.log_"$(date +'%d-%m-%Y %H:%M:%S:%3N')"
mv ${output_file} ${output_file}_"$(date +'%d-%m-%Y %H:%M:%S:%3N')"
find /tmp/zfs_cleanup/Alert.log_* -type f -mtime +7 -exec $RM {} \;
find ${logs}/logs.log_* -type f -mtime +7 -exec $RM {} \;
##################################################################################
#######################################################################################################################

Leave a comment