Template NSH script for pre-patch checks


#!/bin/nsh

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

# TSSA Pre-Patching Checks Script

# Purpose:

#   Run essential pre-checks on a target server before executing patching,

#   reducing risk of job failures and predictable downtime.

#   This is a NSH Script type 1

#

# Supported OS: Windows, Linux

#

# Summary of checks:

#   - Pending reboot on server

#   - Space check

#   - Filesystem existance check

#   - RPM lock check

#

# Inputs:

#   - $1 HOSTNAME (required): Server name to perform these checks on

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

HOSTNAME=${1}

MIN_SPACE_GB=10

#MIN_SPACE_KB=`expr $MIN_SPACE_GB \* 1024 \* 1024`

SLEEPTIMER=1

FINAL_ERROR_CODE=0

# Server properties to check

REBOOT_PROPERTY="IS_PENDING_REBOOT*"

log_msg() {

    echo "`date '+%Y-%m-%d %H:%M:%S'` : $1"

}

check_error() {

    local return_code="$1"

    local err_bit="$2"

    local failure_msg="$3"

    local success_msg="$4"

    local success_msg="$4"

    if [ $return_code -eq 0 ]; then

        log_msg "[ OK ] $success_msg"

    else

        log_msg "[FAIL] $failure_msg"

        (( FINAL_ERROR_CODE |= err_bit ))

        if [ $err_bit -eq 16 ]; then

            exit FINAL_ERROR_CODE

        fi

    fi

}

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

# Server Property Check

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

check_server_reboot_property() {

    log_msg "Checking server properties via blcli"

    PROP_VALUE=`blcli Server printPropertyValue $HOSTNAME "$REBOOT_PROPERTY" 2>&1`

    check_error $? 1 "Failed to read server property $REBOOT_PROPERTY" "Property $REBOOT_PROPERTY=<$PROP_VALUE> read successfully"

    # Normalize value (trim + lowercase)

    PROP_VALUE_CLEAN=`echo "$PROP_VALUE" | tr 'A-Z' 'a-z'`

    if [ "$PROP_VALUE_CLEAN" = "yes" ]; then

        check_error -1 1 "Server property $REBOOT_PROPERTY indicates pending reboot on $HOSTNAME" "Server property $REBOOT_PROPERTY indicates no pending reboot on $HOSTNAME"

    fi

}

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

# Linux Checks

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

linux_checks() {

    log_msg "Detected Linux OS"

    # Disk space check

    SPACE_CHECK_FS=(/opt/bmc /var/tmp/stage)

    for FS in ${SPACE_CHECK_FS[@]}

    do

        log_msg "Checking free space on $FS"

        FREE_KB=`df -k $FS | tail -1 | awk '{print $4}'`

        if [ ! -d "$FS" ]; then

            echo "Filesystem path $FS does not exist"

            continue

        fi

        FREE_GB=`echo "scale=2; $FREE_KB / (1024 * 1024)" | bc`

        FREE_GB=`echo "$FREE_GB" | cut -d. -f1`

        if [ $FREE_GB -lt $MIN_SPACE_GB ]; then

            check_error -1 2 "ERROR: Insufficient space <$FREE_GB>GB on $FS, (Required ${MIN_SPACE_GB}GB)" "Sufficient space available on $FS <$FREE_GB>GB"

        fi

    done

    #RPM lock

    log_msg "Checking RPM lock file"

    if [ -f /var/lib/rpm/.rpm.lock ] ; then   

        if [ `ps -ef | egrep -w "rpm|yum|dnf" | grep -v grep | wc -l` -gt 0 ]; then

            sleep $SLEEPTIMER;

            check_error 1 8 "/var/lib/rpm/.rpm.lock exists, and yum/dnf or rpm is running."

        else

            sleep $SLEEPTIMER;

            check_error 1 8 "/var/lib/rpm/.rpm.lock exists, and yum/dnf or rpm is not running."

        fi

    else

        sleep $SLEEPTIMER;

        log_msg "/var/lib/rpm/.rpm.lock does not exist"

    fi

    log_msg "RPM lock check completed"

}

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

# Windows Checks

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

windows_checks() {

    log_msg "Detected Windows OS"

    # Disk space check (C drive example)

    log_msg "Checking disk space on C drive"

    # Run fsutil on Windows target #TODO findstr

#FSUTIL_OUT=`nexec -i localhost cmd /c "fsutil volume diskfree C: | grep 'Total free bytes'" 2>/dev/null`

 FSUTIL_OUT=`nexec -i localhost cmd /c "fsutil volume diskfree C:" | findstr /C:"Total free bytes" 2>/dev/null`

    # Extract free bytes (first line)

    FREE_GB=`echo "$FSUTIL_OUT"| sed 's/.*( *//; s/ GB).*//'`

    FREE_GB=`echo "$FREE_GB" | cut -d. -f1`

    echo "FREE_GB:<$FREE_GB>"

    # Threshold check

    if [ $FREE_GB -lt $MIN_SPACE_GB ]; then

        check_error 1 2 "ERROR: Insufficient disk space on C drive <$FREE_GB>GB, (Required ${MIN_SPACE_GB}GB)" "Sufficient space available on C drive <$FREE_GB>GB"

    fi

}

report_final_status() {

    local code="$1"

    echo

    echo "Decoding error code: $code"

    echo "--------------------------------"

    [ $((code & 1)) -ne 0 ] && echo "[WARN]: Pending reboot on server"

    [ $((code & 2)) -ne 0 ] && echo "[WARN]: Insufficient space"

    [ $((code & 4)) -ne 0 ] && echo "[WARN]: Filesystem does not exist"

    [ $((code & 8)) -ne 0 ] && echo "[WARN]: RPM lock exist"

    if [ "$code" -eq 0 ]; then

        echo "[SUCCESS]: All checks passed"

    fi

}

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

# Main

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

log_msg "Starting TSSA Precheck Script"

OS_NAME=`uname`

echo "OS_NAME: <$OS_NAME>"

check_server_reboot_property

if echo $OS_NAME | grep -i linux > /dev/null 2>&1; then

    echo "Proceeding with linux Checks"

    linux_checks

else

    echo "Proceeding with Windows Checks"

    windows_checks

fi

report_final_status "$FINAL_ERROR_CODE"

exit "$FINAL_ERROR_CODE"

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*

TrueSight Server Automation 26.1