#!/bin/bash
#Nifty: Detect new CPUs and/or RAM in OS
for CPU in $(ls /sys/devices/system/cpu/ | grep cpu | grep -v idle)
do
        CPU_DIR="/sys/devices/system/cpu/${CPU}"
        echo "Found cpu: \"${CPU_DIR}\" ..."
        CPU_STATE_FILE="${CPU_DIR}/online"
        if [ -f "${CPU_STATE_FILE}" ]; then
                STATE=$(cat "${CPU_STATE_FILE}" | grep 1)
                if [ "${STATE}" == "1" ]; then
                        echo -e "\t${CPU} already online"
                else
                         echo -e "\t${CPU} is new cpu, onlining cpu ..."
                         echo 1 > "${CPU_STATE_FILE}"
                fi
        else
                echo -e "\t${CPU} already configured prior to hot-add"
        fi
done

echo "#####"
echo "##### CPU HOT ADD DONE"
echo "#####"

for mem in /sys/devices/system/memory/memory*;
do
        if grep -q "offline" "$mem/state"; then
                echo Found offline memory, onlining... "$mem"
                echo "online" > "$mem/state"
        else
                echo "Already online - $mem"
        fi
done

echo "#####"
echo "##### CPU HOT ADD DONE"
echo "#####"

