#!/bin/bash

# Now make sure the modules are loaded
for module in ipmi_si ipmi_devintf ipmi_msghandler; do
    if lsmod |grep -q $module; then
        echo "$module already loaded"
    else
        echo "Loading $module..."
        modprobe $module
        # if ipmi_si fails to load, it's safe to assume the system
        # has no BMC, so we'll just politely exit.
        if [ $? -eq 1 ] && [ "$module" == "ipmi_si" ]; then
            echo "No BMC found. Aborting."
            exit 0
        fi
    fi
done

# Now get our info from ipmitool to make sure communication works
# First lest check chassis status
echo "Checking for chassis status"
ipmitool chassis status && echo "Successfully got chassis status" && chassis=0 || chassis=1
echo "Checking to see if we can get sensor data"
ipmitool sdr list full && echo "Successfully got sensor data" && sensor=0 || sensor=1
echo "Checking to see if we can get info on the BMC"
ipmitool bmc info && echo "Successfully got BMC information" && bmc=0 || bmc=1

# if everything passes, exit 0
[ $chassis -eq 0 ] && [ $sensor -eq 0 ] && [ $bmc -eq 0 ] && exit 0 || echo "FAILURE: chassis: $chassis  sensor: $sensor  bmc: $bmc"

# otherwise exit 1
exit 1
