From 4f02974dc4ee92ffe18fb8482aa7a8c2442877e0 Mon Sep 17 00:00:00 2001 From: Shoaib Khan Date: Mon, 14 Aug 2023 23:56:07 -0400 Subject: [PATCH] add service and script, update readme --- README.md | 25 ++++++++++++++ fan_control_dyn.sh | 84 ++++++++++++++++++++++++++++++++++++++++++++++ fancontrol.service | 20 +++++++++++ 3 files changed, 129 insertions(+) create mode 100644 fan_control_dyn.sh create mode 100644 fancontrol.service diff --git a/README.md b/README.md index ff7f8af..e6a9740 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,28 @@ Date 04-09-2020 10:24:52 --> Setting fan speed to 20% ``` Once you have verified the script is working you can set it to run every 5 minuites via cron. On TrueNAS this can be found under the Tasks menu --> Cron Jobs. + +##Running as a service + +Once the service is up and running, the temprature will be checked every `INTERVAL_SEC` seconds. Fan speed will change if the temprature has changed and warrants a speed change. + +There is a delay before the temprature monitoring begins and is controlled by the variable `INITIAL_START_DELAY_SEC`. After this initial delay the time between checks is governed by the `INTERVAL_SEC` value. + +When the server is shutdown/rebooted or started, the manual control is reset, this is to avoid any left over low fan speeds from previous power outage/powerdown/shutdown etc. + +The files required to run the service are `fan_control_dyn.sh` `fancontrol.service` + + +Simply execute the following to get the service set up. +``` +sudo cp fan_control_dyn.sh /usr/local/sbin/fan_control_dyn.sh +sudo chmod 755 /usr/local/sbin/fan_control_dyn.sh +sudo cp fancontrol.service /etc/systemd/system/fancontrol.service +sudo systemctl enable fancontrol.service +sudo systemctl start fancontrol.service +``` + +If you are using a location other than `/usr/local/sbin/fan_control_dyn.sh` then you'll need to modify the location in the `fancontrol.service` file as well +``` +ExecStart=/MY_ABSOLUTE_PATH/fan_control_dyn.sh +``` \ No newline at end of file diff --git a/fan_control_dyn.sh b/fan_control_dyn.sh new file mode 100644 index 0000000..bd06d0d --- /dev/null +++ b/fan_control_dyn.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +# Variables +IDRAC_IP="IP address of iDRAC" +IDRAC_USER="user" +IDRAC_PASSWORD="passowrd" +INTERVAL_SEC=5 +INITIAL_START_DELAY_SEC=60 + +TEMP_THRESHOLD=35 +TEMP_SENSOR="04h" # Inlet Temp +#TEMP_SENSOR="01h" # Exhaust Temp +#TEMP_SENSOR="0Eh" # CPU 1 Temp +#TEMP_SENSOR="0Fh" # CPU 2 Temp + +FCTRL=0 #disabled, enabled=1 +LAST_PCT=0 + + +toggle() { + ipmitool -I lanplus -H $IDRAC_IP -U $IDRAC_USER -P $IDRAC_PASSWORD raw 0x30 0x30 0x01 $1 2>&1 >/dev/null +} + +reset_manual() { + toggle 0x01 + FCTRL=0 #disabled +} + +set_manual() { + toggle 0x00 + FCTRL=1 #enabled +} + +graceful_exit() { + reset_manual + exit 0 +} + +trap graceful_exit SIGINT SIGTERM + + +# need the reset in case the system boots up with the last set value +reset_manual + +#start delay +sleep $INITIAL_START_DELAY_SEC + + +while [ 1 ] +do + + # Get temperature from iDARC. + T=$(ipmitool -I lanplus -H $IDRAC_IP -U $IDRAC_USER -P $IDRAC_PASSWORD sdr type temperature 2>/dev/null | grep $TEMP_SENSOR | cut -d"|" -f5 | cut -d" " -f2) + + # If ambient temperature is above 35deg C enable dynamic control and exit, if below set manual control. + if [[ $T -ge $TEMP_THRESHOLD ]] + then + if [[ $FCTRL -ne 0 ]] + then + reset_manual + fi + else + + # This gives a Percent that is a multiple of 5 for ranges of 5 degC + PCT=$(( 5 * ( T / 5 ) )) + # Min PCT Allowed is 10 + PCT=$(( PCT < 10 ? 10 : PCT )) + + + if [[ $LAST_PCT -ne $PCT ]] + then + if [[ $FCTRL -eq 0 ]] + then + set_manual + fi + PCTHEX=$(printf '0x%02x' $PCT) + ipmitool -I lanplus -H $IDRAC_IP -U $IDRAC_USER -P $IDRAC_PASSWORD raw 0x30 0x30 0x02 0xff $PCTHEX 2>&1 >/dev/null + LAST_PCT=$PCT + fi + fi + + + sleep $INTERVAL_SEC +done \ No newline at end of file diff --git a/fancontrol.service b/fancontrol.service new file mode 100644 index 0000000..df5780e --- /dev/null +++ b/fancontrol.service @@ -0,0 +1,20 @@ +[Unit] +Description=Fan Control script + +StartLimitIntervalSec=14400 +StartLimitBurst=10 + +[Service] +Restart=on-abnormal + +User=root +Group=root + +ExecStart=/usr/local/sbin/fan_control_dyn.sh + +ProtectSystem=full + +TimeoutStopSec=5s + +[Install] +WantedBy=multi-user.target \ No newline at end of file