//
you're reading...
DBA Scripts

Check the Disk Space Usage on Linux Server


## +----------------------------------------------------------------------------------------------------------+
## |                             Davie Wang                                                                   |
## |----------------------------------------------------------------------------------------------------------|
## | OS       : SLES 11 SP2                                                                                   |
## | FILE     : checkDiskSpaceUsage.ksh                                                                       |
## | PURPOSE  : check all disk space usage on the server                                                      |
## | NOTE     : As with any code, ensure to test this script in a development                                 |
## |            environment before attempting to run it in production.                                        |
## | Usage    : Check the disk space on the server every hour                                                 |
## | 0 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,20,21,22,23 * * * /root/adminScripts/checkDiskSpaceUsage.ksh |
## +----------------------------------------------------------------------------------------------------------+

#!/bin/ksh
# ----------------------------
# BINARY FILE LOCATIONS
# ----------------------------
AWK_BIN=/bin/awk
CAT_BIN=/bin/cat
CP_BIN=/bin/cp
CPIO_BIN=/bin/cpio
DATE_BIN=/bin/date
DF_BIN=/bin/df
EGREP_BIN=/bin/egrep
FIND_BIN=/usr/bin/find
GREP_BIN=/bin/grep
GZIP_BIN=/bin/gzip
HOSTNAME_BIN=/bin/hostname
ID_BIN=/usr/bin/id
LS_BIN=/bin/ls
MV_BIN=/bin/mv
PS_BIN=/bin/ps
RM_BIN=/bin/rm
SENDMAIL_BIN=/usr/lib/sendmail
MAIL_BIN=/usr/bin/mail
TEE_BIN=/usr/bin/tee
TOUCH_BIN=/bin/touch
UNAME_BIN=/bin/uname
WC_BIN=/usr/bin/wc
ZIP_BIN=/usr/bin/zip
RSYNC_BIN=/usr/bin/rsync

#Timestamp for the backup sets
TIMESTAMP=`date +"%Y%m%d%H%M"`

# ----------------------------
# HOSTNAME VARIABLES
# ----------------------------
HOSTNAME=`hostname`
HOSTNAME_UPPER=`echo $HOSTNAME | tr '[:lower:]' '[:upper:]'`
HOSTNAME_SHORT=${HOSTNAME%%.*}
HOSTNAME_SHORT_UPPER=`echo $HOSTNAME_SHORT | tr '[:lower:]' '[:upper:]'`

Task="DiskSpaceCheck"

UNIQUE_SCRIPT_IDENTIFIER=${HOSTNAME}_${Task}

# ----------------------------
# SCRIPT NAME VARIABLES
# ----------------------------
SCRIPT_NAME_FULL=$0
SCRIPT_NAME=${SCRIPT_NAME_FULL##*/}
SCRIPT_NAME_NOEXT=${SCRIPT_NAME%.?*}

# ----------------------------
# LOG AND TEMP FILE VARIABLES
# ----------------------------
LOG_FILE_NAME=/root/adminScripts/${UNIQUE_SCRIPT_IDENTIFIER}_${TIMESTAMP}.log
CHECK_SCRIPT_RUNNING_FLAG_FILE=${CUSTOM_ORACLE_TEMP_DIR}/${SCRIPT_NAME_NOEXT}_${UNIQUE_SCRIPT_IDENTIFIER}_${HOSTNAME_SHORT_UPPER}.running

MAIL_RECIPIENT_WARNING="dba@test.com"
MAIL_FROM="${HOSTNAME}@test.com"
MAIL_REPLYTO=""
MAIL_TO_NAME=""
MAIL_TEMP_FILE_NAME=/root/adminScripts/mail_temp_file_name

# +----------------------------------------------------------------------------+
# |                                                                            |
# |                   DEFINE ALL INTERNAL GLOBAL FUNCTIONS                     |
# |                                                                            |
# +----------------------------------------------------------------------------+

function sendEmail {

    # -------------------------------
    # POSSIBLE L_SEVERITY VALUES ARE:
    #     SUCCESSFUL
    #     RUNNING
    #     WARNING
    #     FAILED
    # -------------------------------
    typeset -r L_SEVERITY=${1}
    typeset -r L_EMAIL_ADDRESS_LIST=${2}
    typeset -r L_UNIQUE_SCRIPT_IDENTIFIER=${3}
    typeset    L_IMPORTANCE
    typeset    L_X_PRIORITY
    typeset    L_X_MSMAIL_PRIORITY
    typeset    L_EMAIL_ADDRESS

    case ${L_SEVERITY} in
        SUCCESSFUL)
            L_IMPORTANCE="Normal"
            L_X_PRIORITY="3"
            L_X_MSMAIL_PRIORITY="Normal"
            ;;
        RUNNING)
            L_IMPORTANCE="High"
            L_X_PRIORITY="1"
            L_X_MSMAIL_PRIORITY="High"
            ;;
        WARNING)
            L_IMPORTANCE="High"
            L_X_PRIORITY="1"
            L_X_MSMAIL_PRIORITY="High"
            ;;
        FAILED)
            L_IMPORTANCE="High"
            L_X_PRIORITY="1"
            L_X_MSMAIL_PRIORITY="High"
            ;;
        *)
            L_IMPORTANCE="High"
            L_X_PRIORITY="1"
            L_X_MSMAIL_PRIORITY="High"
        ;;
    esac

    for L_EMAIL_ADDRESS in $L_EMAIL_ADDRESS_LIST; do
        {
            echo "Importance: ${L_IMPORTANCE}"
            echo "X-Priority: ${L_X_PRIORITY}"
            echo "X-MSMail-Priority: ${L_X_MSMAIL_PRIORITY}"
            echo "Subject: [${HOSTNAME_SHORT_UPPER}] - ${L_SEVERITY}: ${SCRIPT_NAME} "
            echo "To: ${MAIL_TO_NAME} "
            echo "From: ${MAIL_FROM}"
            echo "Reply-To: ${MAIL_REPLYTO}"
            echo ""
            cat ${LOG_FILE_NAME}
        } > ${MAIL_TEMP_FILE_NAME}
        
        ${SENDMAIL_BIN} -v $L_EMAIL_ADDRESS > ${LOG_FILE_NAME}
    sendEmail "WARNING" "${MAIL_RECIPIENT_WARNING}" ${UNIQUE_SCRIPT_IDENTIFIER}
    ${RM_BIN} -f ${UNIQUE_SCRIPT_IDENTIFIER}
    ${RM_BIN} -f ${LOG_FILE_NAME}
  else
    echo ""
    ${RM_BIN} -f ${UNIQUE_SCRIPT_IDENTIFIER}
    ${RM_BIN} -f ${LOG_FILE_NAME}
  fi
done
Advertisements

About daviewning

I am an Oracle DBA

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: