#!/bin/bash
#
# $Id: dellfanctl,v 1.11 2019/06/22 02:35:31 root Exp root $
#
# dellfanctl : Reconfigure LSI cache behaviour upon boot/shutdown
#
# chkconfig: 2345 75 02
# description:	Enables manual IPMI Dell Fan control after boot
#

if [ -r /lib/lsb/init-functions ]; then
	# LSB, SLES, ...
	. /lib/lsb/init-functions
elif [ -r /etc/init.d/functions ]; then
	# Red Hat / VMware
	. /etc/init.d/functions
fi
# Get config.
if [ -f /etc/sysconfig/network ]; then
	. /etc/sysconfig/network
fi

prog=dellfanctl
LOCKFILE=/var/lock/subsys/${prog}
LOGFILE=/var/log/dellfanctl.log

PATH=/sbin:/bin:/usr/bin:/usr/sbin

MAX_TEMP=65
GOOD_MSG="(II) Dell FAN tuning utility (${prog})"

[ -x /usr/bin/ipmitool ] || exit 1

dellfancommon() {
	# GLobal Settings
	OK=0

	return $?
}

start() {
	#
	RETVAL=0
	dellfancommon 2>&1 |sed -e '/^\r.*$/d' -e '/^$/d' -e '/Exit Code:/d'

	
	GOOD_MSG="(II) Enabled Manual fan Control on host $(uname -n)"
	case "$(uname -n)" in
		thorbardin)
			# Set to Manual
			/usr/bin/ipmitool raw 0x30 0x30 0x01 0x00 > /dev/null 2>&1
			# Set to 1200rpm
			# /usr/bin/ipmitool raw 0x30 0x30 0x02 0xff 0x0f > /dev/null 2>&1
			# Set to 1320rpm
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0xff 0x12 > /dev/null 2>&1
			;;
		daltigoth)
			# Set to Manual
			/usr/bin/ipmitool raw 0x30 0x30 0x01 0x00 > /dev/null 2>&1
			# Set to 1080rpm
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0xff 0x0b > /dev/null 2>&1
			;;
		palanthas)
			# Set to Manual
			/usr/bin/ipmitool raw 0x30 0x30 0x01 0x00 > /dev/null 2>&1
			# Set to 1200rpm (all fans)
			#/usr/bin/ipmitool raw 0x30 0x30 0x02 0xff 0x0d > /dev/null 2>&1
			# Back fans get set to 1320rpm
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0x00 0x10 > /dev/null 2>&1
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0x01 0x10 > /dev/null 2>&1
			# Front fans get set to 960rpm
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0x02 0x08 > /dev/null 2>&1
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0x03 0x08 > /dev/null 2>&1
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0x04 0x08 > /dev/null 2>&1
			/usr/bin/ipmitool raw 0x30 0x30 0x02 0x05 0x08 > /dev/null 2>&1
			;;
		*)
			# Do nothing
			GOOD_MSG="(II) Nothing to do for host $(uname -n)"
			;;
	esac

	RETVAL=$(($? + $RETVAL))

	if [ ${RETVAL} -eq 0 ]; then
		/bin/touch ${LOCKFILE} && log_success_msg "${GOOD_MSG}"
	else
		/bin/touch ${LOCKFILE}
		log_failure_msg "${prog} returned ${RETVAL}"
	fi

	return ${RETVAL}
}

stop() {
	#
	RETVAL=0
	dellfancommon 2>&1 |sed -e '/^\r.*$/d' -e '/^$/d' -e '/Exit Code:/d'

	GOOD_MSG="(II) Not stopping manual fan Control on host $(uname -n)"
	case "$(uname -n)" in
		palanthas)
			# Set back to Auto ## Disabled 20190621
			#/usr/bin/ipmitool raw 0x30 0x30 0x01 0x01 > /dev/null 2>&1
			;;
		daltigoth|thorbardin)
			# Set back to Auto ## Disabled 20190621
			#/usr/bin/ipmitool raw 0x30 0x30 0x01 0x01 > /dev/null 2>&1
			;;
		*)
			# Do nothing
			GOOD_MSG="(II) Nothing to do for host $(uname -n)"
			;;
	esac

	RETVAL=$(($? + $RETVAL))

	if [ ${RETVAL} -eq 0 ]; then
		/bin/rm -f ${LOCKFILE} && log_success_msg "${GOOD_MSG}"
        else
                rm -f ${LOCKFILE}
                log_failure_msg "(**) ${prog} returned ${RETVAL}"
	fi

	return ${RETVAL}
}

auto() {
	#
	RETVAL=0
	dellfancommon 2>&1 |sed -e '/^\r.*$/d' -e '/^$/d' -e '/Exit Code:/d'

	GOOD_MSG="(II) Enabled Auto fan Control on host $(uname -n)"
	case "$(uname -n)" in
		palanthas)
			# Set back to Auto
			/usr/bin/ipmitool raw 0x30 0x30 0x01 0x01 > /dev/null 2>&1
			;;
		daltigoth|thorbardin)
			# Set back to Auto
			/usr/bin/ipmitool raw 0x30 0x30 0x01 0x01 > /dev/null 2>&1
			;;
		*)
			# Do nothing
			GOOD_MSG="(II) Nothing to do for host $(uname -n)"
			;;
	esac

	RETVAL=$(($? + $RETVAL))

	if [ ${RETVAL} -eq 0 ]; then
		/bin/rm -f ${LOCKFILE} && log_success_msg "${GOOD_MSG}"
        else
                rm -f ${LOCKFILE}
                log_failure_msg "(**) ${prog} returned ${RETVAL}"
	fi

	return ${RETVAL}
}

status() {
	#
	RETVAL=0
	dellfancommon 2>&1 |sed -e '/^\r.*$/d' -e '/^$/d' -e '/Exit Code:/d'

	CUR_TEMP=$(/usr/bin/sensors |grep '^Core '|sed -e 's@.*:[[:space:]]*+@@' -e 's@°C.*@@' -e 's@\.0$@@'|sort -un|tail -1)
	### CUR_RPM=$(/usr/bin/ipmitool sdr type fan|awk '{ FS="|"; if ( $10 ==  "RPM" ) { print $9,$10 } }'|head -1)
	CUR_RPM=$(/usr/bin/ipmitool sensor get Fan1|awk 'BEGIN { FS=":" } {  if ( $2 ~ /RPM/ ) { print $2 } }'|head -1)
	GOOD_MSG="(II) MAX T: ${MAX_TEMP}C, Current T: ${CUR_TEMP}C, Fan: ${CUR_RPM}"

	RETVAL=$(($? + $RETVAL))

	if [ ${RETVAL} -eq 0 ]; then
		/bin/rm -f ${LOCKFILE} && log_success_msg "${GOOD_MSG}"
        else
                rm -f ${LOCKFILE}
                log_failure_msg "(**) ${prog} returned ${RETVAL}"
	fi
	return ${RETVAL}
}

check() {
	#
	RETVAL=0
	dellfancommon 2>&1 |sed -e '/^\r.*$/d' -e '/^$/d' -e '/Exit Code:/d'

	CUR_TEMP=$(/usr/bin/sensors |grep '^Core '|sed -e 's@.*:[[:space:]]*+@@' -e 's@°C.*@@' -e 's@\.0$@@'|sort -un|tail -1)
	if [ ${CUR_TEMP} -ge ${MAX_TEMP} ]; then
		auto > /dev/null 2>&1
		GOOD_MSG="(II) Over temp ( ${CUR_TEMP}C >= ${MAX_TEMP}C ), reverting to Auto fan control"
	else
		start > /dev/null 2>&1
		GOOD_MSG="(II) Temps OK ( ${CUR_TEMP}C < ${MAX_TEMP}C ), setting Manual fan control"
	fi

	RETVAL=$(($? + $RETVAL))

	if [ ${RETVAL} -eq 0 ]; then
		/bin/rm -f ${LOCKFILE} && log_success_msg "${GOOD_MSG}"
        else
                rm -f ${LOCKFILE}
                log_failure_msg "(**) ${prog} returned ${RETVAL}"
	fi
	return ${RETVAL}
}

dellfanlogme() {
	echo "$(date '+%Y%m%d%H%M%S') $(status)" >> ${LOGFILE}
	# Sleep for 0.7s
	#/bin/usleep 700000
}

case "$1" in
	start)
	    start
	    RETVAL=$?
	    ;;
	
	stop)
	    stop
	    RETVAL=$?
	    ;;
	
	auto)
	    auto
	    RETVAL=$?
	    ;;
	
	status)
	    status
	    RETVAL=$?
	    echo "$(date '+%Y%m%d%H%M%S') $(status)" >> ${LOGFILE}
	    ;;

	check)
	    check
	    RETVAL=$?
	    echo "$(date '+%Y%m%d%H%M%S') $(check)" >> ${LOGFILE}
	    ;;

	restart)
	    stop
	    start
	    ;;
	
	*)
	    echo $"Usage: $0 {start|stop|auto|restart|status}"
	    exit 1

esac

#

exit $RETVAL