Space usage analyzing utility “space_check”

September 16, 2020

by Eric Garcia

Intended Audience Level: Beginner/Intro

Code Sample Type: Complete Script

Nutanix Technologies: General

Minimum Product Version: 5.10

Script/Code Language: Bash Shell

REST API Sample? No

REST API Version: N/A

This script takes the manual actions from KB-1540 and simplifies them so that the steps to be done by an SRE do not take as long. This could also be used by the customer so that they can provide additional information to the SREs for faster case resolution.

Code Sample Details

This section may be empty if additional code sample details are not available.
#! /bin/bash

################################################################################
#   Script Name: space_check
#   Filename: space_check.sh
#   Script Version: 1.0
#   Created by: Eric Garcia
#   Contact: eric.garcia@nutanix.com
################################################################################

################################# PREREQUISITES ################################
#
#    1. This script must be executed from a Nutanix CVM as the nutanix user
#
################################################################################

################################### SYNOPSIS ###################################
#
#    This sript will review the space usage of the CVM in depth and report back
#    suggestions to the administrator
#
################################################################################

################################## DISCLAIMER ##################################
#
#    This code is intended as a standalone example.  Subject to licensing
#    restrictions defined on nutanix.dev, this can be downloaded, copied and/or
#    modified in any way you see fit.
#
#    Please be aware that all public code samples provided by Nutanix are
#    unofficial in nature, are provided as examples only, are unsupported and
#    will need to be heavily scrutinized and potentially modified before they
#    can be used in a production environment.  All such code samples are
#    provided on an as-is basis, and Nutanix expressly disclaims all warranties,
#    express or implied.
#
#    All code samples are © Nutanix, Inc., and are provided as-is under the MIT
#    license. (https://opensource.org/licenses/MIT)
#
################################################################################

################################# SET VARIABLES ################################
#
fileSize="+80M"
logDir="/home/nutanix/data/logs"
foundationDir="/home/nutanix/foundation"
excludeFoundation="-not ( -path /home/nutanix/foundation/lib -prune )"
logSaving=`find $logDir -type f -size $fileSize -exec du -ch {} +\
 | tail -1 | grep -o -P '.{0,5}[A-Z]'`
foundationSaving=`find $foundationDir -type f -size $fileSize -exec du -ch {} +\
 | tail -1 | grep -o -P '.{0,5}[A-Z]'`
#
################################################################################

################################### SET ARRAYS #################################
#
logFiles=( $(find $logDir -type f -size $fileSize) )
foundationFiles=( $(find $foundationDir $excludeFoundation -type f \
-size $fileSize) )
#
################################################################################

############################### DEFINE FUNCTIONS ###############################
#

# Show the file size in human readable format from the logFiles array
function logFiles() {

    # Iterate over every item in the logFiles array
    for file in "${logFiles[@]}"
    do
        # Show the size and time of last edit for each iteration
        du -h --time $file
    done
}

# Show the file size in human readable format from the foundationFiles array
function foundationFiles() {

    # Iterate over every item in the foundationFiles array
    for file in "${foundationFiles[@]}"
    do

        # Show the size and time of last edit for each iteration
        du -h --time $file
    done
}

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

########################### DEFINE SCRIPT FEEDBACK #############################
#

# Show output if the logFiles array has at least one item in it
if (( ${#logFiles[@]} > 0 ))
then
    echo "===================================================================="
    echo "These are the largest files under the $logDir path:"
    echo "===================================================================="
    echo
    logFiles | sort -h
    echo
    echo "===================================================================="
    echo "You could regain $logSaving of space if you delete the above files"
    echo "from within the $logDir directory."
    echo
    echo "It is recommended to keep at least three days of logs."
    echo "===================================================================="
    echo
fi

# Show output if the foundationFiles array has at least one item in it
if (( ${#foundationFiles[@]} > 0 ))
then
    echo
    echo "===================================================================="
    echo "These are the largest files under the $foundationDir path:"
    echo "===================================================================="
    echo
    foundationFiles | sort -h
    echo
    echo "===================================================================="
    echo "You could regain $foundationSaving of space if you delete the above"
    echo "files from within the $foundationDir directory."
    echo
    echo "It is recommended to review these files with support before deleting"
    echo "===================================================================="
    echo
fi
#
################################################################################