From c04868954b65819c2498a6a1e2e5da51a14d725a Mon Sep 17 00:00:00 2001 From: df Date: Sun, 1 Nov 2026 02:36:54 +0800 Subject: [PATCH] feat: add configurable linear fan speed interpolation Enable piecewise interpolation between existing temperature anchors by default, rounding duty up without level hysteresis. Preserve the original stepwise downshift hysteresis when disabled, and expose the option in config, diagnostics, Compose, TUI, tests, and operational docs. --- .env.example | 12 ++- README.md | 36 ++++--- README.zh-TW.md | 34 ++++--- USAGE_GUIDE.md | 17 +++- docker-compose.yml | 2 + docs/TROUBLESHOOTING.md | 10 +- src/FanControlWithEsxiSmart.sh | 67 ++++++++++++- src/fan-control-tui.sh | 15 ++- tests/fan-control.test.sh | 174 +++++++++++++++++++++++++++++++++ tests/tui.test.sh | 29 ++++++ 10 files changed, 357 insertions(+), 39 deletions(-) diff --git a/.env.example b/.env.example index 3118b0a..44d35f2 100644 --- a/.env.example +++ b/.env.example @@ -90,7 +90,13 @@ REMOTE_GPU_TEMP_OFFSET=15 # ----------------------------------------------------------------------------- # Fan curve # ----------------------------------------------------------------------------- -# The controller chooses the highest matching level: +# Enabled by default: interpolate between the low/medium/high/critical anchors +# and round duty up to an integer percent. Below TEMP_LOW uses FAN_SPEED_IDLE; +# distinct idle/low speeds intentionally jump at TEMP_LOW (no idle temp anchor). +# At/above TEMP_CRITICAL, clamp to FAN_SPEED_CRITICAL. No hysteresis in this mode. +FAN_SPEED_INTERPOLATION=true +# Set false for the original stepwise curve and downshift-only HYSTERESIS: +# The controller then chooses the highest matching level: # idle: temp < TEMP_LOW # low: TEMP_LOW <= temp < TEMP_MEDIUM # medium: TEMP_MEDIUM <= temp < TEMP_HIGH @@ -101,13 +107,13 @@ TEMP_MEDIUM=70 TEMP_HIGH=75 TEMP_CRITICAL=80 -FAN_SPEED_IDLE=25 +FAN_SPEED_IDLE=30 FAN_SPEED_LOW=30 FAN_SPEED_MEDIUM=40 FAN_SPEED_HIGH=50 FAN_SPEED_CRITICAL=60 -# Prevent fan speed flapping near thresholds. Downshifts wait until the +# Stepwise mode only (FAN_SPEED_INTERPOLATION=false). Downshifts wait until the # temperature falls this many degrees below the previous level threshold. HYSTERESIS=2 diff --git a/README.md b/README.md index 86b24e5..b961f3b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![Container](https://img.shields.io/badge/container-GHCR-blue)](https://github.com/DF-wu/iDRACFanSpeedControl/pkgs/container/idrac-fan-control) [![License](https://img.shields.io/badge/license-MIT-green)](LICENSE) -A fan controller for Dell PowerEdge servers. It sets the fan duty cycle through iDRAC IPMI OEM raw commands, then selects a fan-curve level from pluggable temperature sources: ESXi NVMe SMART, iDRAC sensors, local Linux disks, and local or remote NVIDIA GPUs. +A fan controller for Dell PowerEdge servers. It sets the fan duty cycle through iDRAC IPMI OEM raw commands, then calculates fan speed from a configurable curve using pluggable temperature sources: ESXi NVMe SMART, iDRAC sensors, local Linux disks, and local or remote NVIDIA GPUs. > [!CAUTION] > This project has been tested on only my Dell PowerEdge R730xd with iDRAC 8. Other server models and iDRAC firmware versions are untested and must not be assumed compatible. This program temporarily overrides Dell's automatic fan control; use it at your own risk. During initial setup, keep the iDRAC Web UI or a physical console available. Verify `manual`, `restore`, and `diagnose` on your own hardware before running `auto` for an extended period. If the server overheats, readings become unreliable, or behavior is abnormal, run `restore` immediately and stop the container. @@ -173,10 +173,13 @@ Before unattended operation, confirm each item: ```mermaid flowchart TD R[All valid readings] --> M{Select highest adjusted temperature} - M --> L[temperature_level] + M --> I{FAN_SPEED_INTERPOLATION?} + I -- true --> C[Interpolate anchors and round duty up] + C --> F + I -- false --> L[temperature_level] L --> H{Lower than previous level?} H -- No --> S[Increase immediately or keep level] - H -- Yes --> X{Below previous threshold - HYSTERESIS?} + H -- Yes --> X{At or below previous threshold - HYSTERESIS?} X -- No --> S2[Keep previous level] X -- Yes --> S3[Move to lower level] S --> F[Apply fan speed] @@ -185,18 +188,24 @@ flowchart TD R -. All sources fail .-> FS[FAILSAFE_FAN_SPEED] ``` -The default curve is shown below. `validate` ensures that thresholds are strictly increasing, fan speeds never decrease, and the fail-safe speed is not lower than the critical speed. +The default interpolation anchors and boundary speeds are shown below. Between anchors, duty rises linearly; the logged level names still describe the temperature band. `validate` ensures that thresholds are strictly increasing, fan speeds never decrease, and the fail-safe speed is not lower than the critical speed. -| Level | Decision temperature | Default fan speed | +| Level | Anchor / boundary temperature | Default fan speed | | --- | --- | --- | -| `idle` | `<65°C` | 25% | -| `low` | `65–69°C` | 30% | -| `medium` | `70–74°C` | 40% | -| `high` | `75–79°C` | 50% | +| `idle` | `<65°C` | 30% | +| `low` | `65°C` | 30% | +| `medium` | `70°C` | 40% | +| `high` | `75°C` | 50% | | `critical` | `>=80°C` | 60% | | `failsafe` | All sources fail | 70% | -Moving to a lower fan level requires the temperature to fall by `HYSTERESIS` degrees below the relevant threshold, preventing repeated speed changes near a boundary. Moving to a higher level is immediate. Each source owns its adjustment: local and remote GPUs subtract their configured offset, Linux disks subtract `LINUX_DISK_TEMP_OFFSET`, and all results have a minimum of 0°C. +`FAN_SPEED_INTERPOLATION=true` is the default, including for existing configurations that omit the option. It joins `(TEMP_LOW, FAN_SPEED_LOW)`, `(TEMP_MEDIUM, FAN_SPEED_MEDIUM)`, `(TEMP_HIGH, FAN_SPEED_HIGH)`, and `(TEMP_CRITICAL, FAN_SPEED_CRITICAL)` with straight segments. Duty is rounded **up** to an integer percentage, stays monotonic within the validated 1–100% range, uses `FAN_SPEED_IDLE` below `TEMP_LOW`, and clamps to `FAN_SPEED_CRITICAL` at or above `TEMP_CRITICAL`. There is no invented idle-temperature anchor: distinct idle and low speeds intentionally cause a jump at `TEMP_LOW`. + +Interpolation ignores `HYSTERESIS` and the previous level in both directions. Each successful collection cycle uses the current demand immediately, including increases within the same level; unchanged integer duty does not send another fan command. There is no additional smoothing, PID, or rate limit. For example, thresholds `65/70/75/80` and speeds `30/35/45/60/65` produce `45/48/51/54/57/60%` at `70/71/72/73/74/75°C`. + +Set `FAN_SPEED_INTERPOLATION=false` to restore the original stepwise behavior: each anchor speed is held until the next threshold, and only downshifts wait for the temperature to reach the previous level threshold minus `HYSTERESIS` (equality releases the hold). Upshifts are immediate. In the TUI, change this switch under **Fan curve**; `config` and read-only `diagnose` show the mode and whether hysteresis is active. + +Each source owns its adjustment: local and remote GPUs subtract their configured offset, Linux disks subtract `LINUX_DISK_TEMP_OFFSET`, and all results have a minimum of 0°C. ## Configuration reference @@ -244,8 +253,9 @@ Moving to a lower fan level requires the temperature to fall by `HYSTERESIS` deg | Variable | Default | Description | | --- | --- | --- | | `TEMP_LOW/MEDIUM/HIGH/CRITICAL` | `65/70/75/80` | Strictly increasing thresholds in °C | -| `FAN_SPEED_IDLE/LOW/MEDIUM/HIGH/CRITICAL` | `25/30/40/50/60` | Values from 1–100% that must not decrease | -| `HYSTERESIS` | `2` | Degrees below the previous threshold required before lowering fan speed | +| `FAN_SPEED_IDLE/LOW/MEDIUM/HIGH/CRITICAL` | `30/30/40/50/60` | Values from 1–100% that must not decrease; unset idle follows low | +| `FAN_SPEED_INTERPOLATION` | `true` | Piecewise linear interpolation with ceiling rounding; `false` restores legacy steps | +| `HYSTERESIS` | `2` | Stepwise mode only: degrees below the previous threshold required before lowering fan speed | | `FAILSAFE_ON_ERROR` | `true` | Uses a conservative speed when all sources fail | | `FAILSAFE_FAN_SPEED` | `70` | Must be greater than or equal to the critical speed | | `MANUAL_FAN_SPEED` | `35` | Used by `manual` when no argument is provided | @@ -360,7 +370,7 @@ make validate-example # DRY_RUN smoke test that does not require .env make docker-build ``` -Tests cover the source interface, Linux SMART collection, multi-host remote GPUs, source offsets, SDR parsing, hysteresis, fail-safe behavior, configuration validation, credential redaction, health freshness, diagnostics, and safe TUI configuration round-trips. Real hardware, SSH targets, disks, iDRAC, and GPUs must still be verified with `diagnose` on your management network. +Tests cover the source interface, Linux SMART collection, multi-host remote GPUs, source offsets, SDR parsing, interpolation anchors/rounding/bounds, legacy hysteresis, control-cycle state and fail-safe recovery, configuration validation, credential redaction, health freshness, diagnostics, and safe TUI configuration round-trips. Real hardware, SSH targets, disks, iDRAC, and GPUs must still be verified with `diagnose` on your management network. ## Project structure diff --git a/README.zh-TW.md b/README.zh-TW.md index d90b73e..f57d2af 100644 --- a/README.zh-TW.md +++ b/README.zh-TW.md @@ -173,10 +173,13 @@ sequenceDiagram ```mermaid flowchart TD R[所有有效 readings] --> M{取最高 adjusted temperature} - M --> L[temperature_level] + M --> I{FAN_SPEED_INTERPOLATION?} + I -- true --> C[錨點間線性插值並向上取整] + C --> F + I -- false --> L[temperature_level] L --> H{比上一檔更低?} H -- 否 --> S[立即升檔或維持] - H -- 是 --> X{低於前一檔 threshold - HYSTERESIS?} + H -- 是 --> X{小於等於前一檔 threshold - HYSTERESIS?} X -- 否 --> S2[維持上一檔] X -- 是 --> S3[降到新檔] S --> F[套用 fan speed] @@ -185,18 +188,24 @@ flowchart TD R -. 全部失敗 .-> FS[FAILSAFE_FAN_SPEED] ``` -預設曲線如下;`validate` 會確保 threshold 嚴格遞增、fan speed 不遞減,且 fail-safe 不低於 critical speed。 +預設插值錨點與邊界速度如下。錨點間的 duty 線性增加,日誌 level 仍表示溫度區間;`validate` 會確保 threshold 嚴格遞增、fan speed 不遞減,且 fail-safe 不低於 critical speed。 -| Level | 決策溫度 | 預設風扇 | +| Level | 錨點/邊界溫度 | 預設風扇 | | --- | --- | --- | -| `idle` | `<65°C` | 25% | -| `low` | `65–69°C` | 30% | -| `medium` | `70–74°C` | 40% | -| `high` | `75–79°C` | 50% | +| `idle` | `<65°C` | 30% | +| `low` | `65°C` | 30% | +| `medium` | `70°C` | 40% | +| `high` | `75°C` | 50% | | `critical` | `>=80°C` | 60% | | `failsafe` | 所有來源失敗 | 70% | -降檔會等待 `HYSTERESIS` 度,避免溫度在閾值附近來回跳速;升檔不延遲。每個來源自行調整讀值:本機與遠端 GPU 分別扣除各自 offset,Linux 磁碟扣除 `LINUX_DISK_TEMP_OFFSET`,最低都不會小於 0°C。 +`FAN_SPEED_INTERPOLATION=true` 預設開啟;舊設定檔未加入此選項時也會啟用。控制器以 `(TEMP_LOW, FAN_SPEED_LOW)`、`(TEMP_MEDIUM, FAN_SPEED_MEDIUM)`、`(TEMP_HIGH, FAN_SPEED_HIGH)`、`(TEMP_CRITICAL, FAN_SPEED_CRITICAL)` 為四個錨點做分段線性插值,duty **向上取整**為整數百分比。驗證後的曲線單調不減且介於 1–100%;低於 `TEMP_LOW` 使用 `FAN_SPEED_IDLE`,達到或超過 `TEMP_CRITICAL` 固定使用 `FAN_SPEED_CRITICAL`。沒有額外的 idle 溫度錨點:idle 與 low 速度不同時,`TEMP_LOW` 處會有刻意保留的跳差。 + +插值模式在升降溫時都不套用 `HYSTERESIS`,也不受前次 level 限制。每次成功收集後立即依目前需求決定速度,同一 level 內升溫也立即提高 duty;整數 duty 未變時不重送命令。不額外加入平滑、PID 或速率限制。例如閾值 `65/70/75/80`、速度 `30/35/45/60/65` 時,`70/71/72/73/74/75°C` 對應 `45/48/51/54/57/60%`。 + +設定 `FAN_SPEED_INTERPOLATION=false` 可恢復原本階梯模式:維持該檔速度直到下一閾值,僅降檔需等到溫度低於或等於「前一檔閾值減 `HYSTERESIS`」,升檔不延遲。TUI 的 **Fan curve** 可切換此選項;`config` 與唯讀 `diagnose` 會顯示模式及遲滯是否生效。 + +每個來源自行調整讀值:本機與遠端 GPU 分別扣除各自 offset,Linux 磁碟扣除 `LINUX_DISK_TEMP_OFFSET`,最低都不會小於 0°C。 ## 設定參考 @@ -244,8 +253,9 @@ flowchart TD | 變數 | 預設 | 說明 | | --- | --- | --- | | `TEMP_LOW/MEDIUM/HIGH/CRITICAL` | `65/70/75/80` | 嚴格遞增的°C threshold | -| `FAN_SPEED_IDLE/LOW/MEDIUM/HIGH/CRITICAL` | `25/30/40/50/60` | 1–100%,不可遞減 | -| `HYSTERESIS` | `2` | 降檔所需低於前 threshold 的°C | +| `FAN_SPEED_IDLE/LOW/MEDIUM/HIGH/CRITICAL` | `30/30/40/50/60` | 1–100%,不可遞減;idle 未設定時沿用 low | +| `FAN_SPEED_INTERPOLATION` | `true` | 分段線性插值並向上取整;`false` 恢復原本階梯模式 | +| `HYSTERESIS` | `2` | 僅階梯模式:降檔所需低於前 threshold 的°C | | `FAILSAFE_ON_ERROR` | `true` | 所有來源失敗時使用保守速度 | | `FAILSAFE_FAN_SPEED` | `70` | 必須 >= critical speed | | `MANUAL_FAN_SPEED` | `35` | `manual` 未帶參數時使用 | @@ -360,7 +370,7 @@ make validate-example # 不需要 .env 的 DRY_RUN smoke test make docker-build ``` -測試涵蓋統一 source 介面、Linux SMART 收集、多主機 remote GPU、source offset、SDR parsing、hysteresis、fail-safe、設定驗證、credential redaction、health freshness、診斷 preview,以及 TUI 設定檔安全 round-trip。真實硬體、SSH target、磁碟、iDRAC 與 GPU 仍須在管理網路上以 `diagnose` 驗證。 +測試涵蓋統一 source 介面、Linux SMART 收集、多主機 remote GPU、source offset、SDR parsing、插值錨點/取整/邊界、原本降檔遲滯、控制循環狀態與 fail-safe 復原、設定驗證、credential redaction、health freshness、診斷 preview,以及 TUI 設定檔安全 round-trip。真實硬體、SSH target、磁碟、iDRAC 與 GPU 仍須在管理網路上以 `diagnose` 驗證。 ## 專案結構 diff --git a/USAGE_GUIDE.md b/USAGE_GUIDE.md index 0cd754c..d8c848a 100644 --- a/USAGE_GUIDE.md +++ b/USAGE_GUIDE.md @@ -165,13 +165,26 @@ GPU_TEMP_OFFSET=15 同時確認 Compose 的 `gpus: all` 已啟用、主機有 NVIDIA Container Toolkit,再用 `diagnose` 驗證;GPU 失敗不應被悄悄當作 0°C。 -### 降低閾值附近的跳速 +### 線性插值與階梯模式 ```dotenv +FAN_SPEED_INTERPOLATION=true +``` + +預設開啟,舊 `.env` 沒有此選項也會啟用。TUI 的 **Fan curve** 可切換,Compose 經由既有 `env_file: .env` 傳入;修改後需重建容器才會套用。`config` 與唯讀 `diagnose` 會顯示目前模式及遲滯是否生效。 + +插值以 `TEMP_LOW/MEDIUM/HIGH/CRITICAL` 及相對應的 `FAN_SPEED_LOW/MEDIUM/HIGH/CRITICAL` 為錨點,錨點間線性計算並向上取整為整數 duty。低於 `TEMP_LOW` 保留 `FAN_SPEED_IDLE`,達到/超過 `TEMP_CRITICAL` 固定為 critical speed;idle 與 low 不同時在 `TEMP_LOW` 會跳差,沒有另設 idle 溫度錨點。例如閾值 `65/70/75/80`、速度 `30/35/45/60/65` 時,`70/71/72/73/74/75°C` 對應 `45/48/51/54/57/60%`。 + +插值模式不使用 `HYSTERESIS`,不受前次 level 限制。每次收集後立即依需求升降速,同 level 內也會更新;整數 duty 相同時不重送命令。不加入額外平滑、PID 或速率限制。感測器最大值、offset、異常讀值過濾與 fail-safe 保護維持原有行為。 + +若要恢復原本階梯模式與降檔遲滯: + +```dotenv +FAN_SPEED_INTERPOLATION=false HYSTERESIS=3 ``` -Hysteresis 只延遲降檔,不延遲升檔。若溫度持續上升,critical 仍會立即生效。 +此時每檔維持固定速度;Hysteresis 只延遲降檔,溫度降到前一檔閾值減 `HYSTERESIS`(含等號)就釋放。升檔不延遲,critical 仍會立即生效。 ### 提高感測器失敗時的保護 diff --git a/docker-compose.yml b/docker-compose.yml index b17b84a..d37bef7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,8 @@ services: idrac-fan-control: image: ghcr.io/df-wu/idrac-fan-control:latest container_name: idrac-fan-control + # Fan options, including FAN_SPEED_INTERPOLATION=true by default, come + # from .env. Set it to false there for legacy steps and downshift hysteresis. env_file: - .env volumes: diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index a330107..c8faf0a 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -49,7 +49,7 @@ iDRAC Fan Control diagnostics (read-only) [PASS] iDRAC/IPMI 2.60, PowerEdge R730xd [PASS] source:idrac Inlet Temp=24C, Exhaust Temp=39C [PASS] source:esxi t10.NVMe...=46C -[PASS] decision preview 46C -> idle (25%), no command sent +[PASS] decision preview 46C -> idle (30%), no command sent Result: PASS - controller dependencies are ready. ``` @@ -201,4 +201,10 @@ docker inspect idrac-fan-control --format '{{json .Mounts}}' 請勿附上 `.env`、密碼、SSH private key、公開可達的管理 IP 或完整 service tag。 -文件最後檢視:2026-08-15。 +## 插值模式與預期速度不同 + +`FAN_SPEED_INTERPOLATION` 預設為 `true`,包括未設定此選項的舊 `.env`。先用 `config` 確認模式與四個錨點,再用唯讀 `diagnose` 檢查目前最高有效調整溫度對應的 duty;插值會向上取整,並忽略 `HYSTERESIS`。同一 level 中的 duty 也可能不同,日誌 level 僅表示溫度區間。 + +低於 `TEMP_LOW` 仍使用 idle,達到 `TEMP_LOW` 使用 low;兩者不同時會跳差,不會在兩者間另造溫度錨點。達到/超過 critical 固定使用 critical speed。若要保留舊階梯行為與只延遲降檔的遲滯,設 `FAN_SPEED_INTERPOLATION=false`,並在更新 `.env` 後重建容器。TUI 的 **Fan curve** 也可切換。診斷不會送出風扇控制命令。 + +文件最後檢視:2026-11-01。 diff --git a/src/FanControlWithEsxiSmart.sh b/src/FanControlWithEsxiSmart.sh index bb18c1c..2d0c503 100755 --- a/src/FanControlWithEsxiSmart.sh +++ b/src/FanControlWithEsxiSmart.sh @@ -50,6 +50,7 @@ PROGRAM_NAME="$(basename "$0")" : "${FAN_SPEED_MEDIUM:=40}" : "${FAN_SPEED_HIGH:=50}" : "${FAN_SPEED_CRITICAL:=60}" +: "${FAN_SPEED_INTERPOLATION:=true}" : "${FAILSAFE_FAN_SPEED:=70}" : "${FAILSAFE_ON_ERROR:=true}" : "${RESTORE_AUTO_ON_EXIT:=true}" @@ -377,7 +378,7 @@ validate_config() { fi if command_needs_temperature "$command"; then - for bool_name in WITH_GPU_TEMP FAILSAFE_ON_ERROR RESTORE_AUTO_ON_EXIT; do + for bool_name in WITH_GPU_TEMP FAILSAFE_ON_ERROR RESTORE_AUTO_ON_EXIT FAN_SPEED_INTERPOLATION; do if ! is_bool "${!bool_name}"; then log "ERROR" "${bool_name} must be a boolean value" error=1 @@ -987,6 +988,36 @@ fan_speed_for_level() { esac } +# The validated curve is nondecreasing with strictly increasing temperatures. +# Keep idle separate: TEMP_LOW is the first interpolation anchor. +interpolated_fan_speed() { + local temp="$1" + local lower_temp upper_temp lower_speed upper_speed + + if (( temp < TEMP_LOW )); then + printf '%s' "$FAN_SPEED_IDLE" + return 0 + elif (( temp >= TEMP_CRITICAL )); then + printf '%s' "$FAN_SPEED_CRITICAL" + return 0 + elif (( temp < TEMP_MEDIUM )); then + lower_temp="$TEMP_LOW" upper_temp="$TEMP_MEDIUM" + lower_speed="$FAN_SPEED_LOW" upper_speed="$FAN_SPEED_MEDIUM" + elif (( temp < TEMP_HIGH )); then + lower_temp="$TEMP_MEDIUM" upper_temp="$TEMP_HIGH" + lower_speed="$FAN_SPEED_MEDIUM" upper_speed="$FAN_SPEED_HIGH" + else + lower_temp="$TEMP_HIGH" upper_temp="$TEMP_CRITICAL" + lower_speed="$FAN_SPEED_HIGH" upper_speed="$FAN_SPEED_CRITICAL" + fi + + # Integer ceiling never rounds below the computed cooling demand. With + # validated anchors the result stays within this segment's duty bounds. + local span=$((upper_temp - lower_temp)) + local rise=$(((upper_speed - lower_speed) * (temp - lower_temp))) + printf '%s' "$((lower_speed + (rise + span - 1) / span))" +} + apply_hysteresis() { local temp="$1" local proposed_level="$2" @@ -1028,8 +1059,15 @@ choose_fan_speed() { local speed raw_level="$(temperature_level "$temp")" - level="$(apply_hysteresis "$temp" "$raw_level" "$previous_level")" - speed="$(fan_speed_for_level "$level")" + if is_true "$FAN_SPEED_INTERPOLATION"; then + # Level is diagnostic only in this mode; legacy level hysteresis would + # flatten the curve and can suppress required upward cooling. + level="$raw_level" + speed="$(interpolated_fan_speed "$temp")" + else + level="$(apply_hysteresis "$temp" "$raw_level" "$previous_level")" + speed="$(fan_speed_for_level "$level")" + fi printf '%s\t%s\n' "$level" "$speed" } @@ -1123,7 +1161,8 @@ auto_mode() { log "INFO" "Automatic fan control started" log "INFO" "Temperature sources: $(normalize_sources)" - log "INFO" "Check interval: ${CHECK_INTERVAL}s, hysteresis: ${HYSTERESIS}C, fail-safe: ${FAILSAFE_FAN_SPEED}%" + log "INFO" "Fan curve: $(fan_curve_mode); hysteresis: $(hysteresis_description)" + log "INFO" "Check interval: ${CHECK_INTERVAL}s, fail-safe: ${FAILSAFE_FAN_SPEED}%" trap cleanup_auto_mode EXIT trap 'exit 130' INT TERM @@ -1158,6 +1197,22 @@ config_row() { printf ' %-30s %s\n' "$1" "$2" } +fan_curve_mode() { + if is_true "$FAN_SPEED_INTERPOLATION"; then + printf 'piecewise linear (ceiling to integer duty)' + else + printf 'stepwise' + fi +} + +hysteresis_description() { + if is_true "$FAN_SPEED_INTERPOLATION"; then + printf '%s C (inactive in interpolation mode)' "$HYSTERESIS" + else + printf '%s C (downshift only)' "$HYSTERESIS" + fi +} + print_effective_config() { local esxi_auth="password" local remote_gpu_auth="password" @@ -1182,7 +1237,8 @@ print_effective_config() { config_row "GPU offsets" "local=${GPU_TEMP_OFFSET} C, remote=${REMOTE_GPU_TEMP_OFFSET} C" config_row "Fan thresholds" "${TEMP_LOW}/${TEMP_MEDIUM}/${TEMP_HIGH}/${TEMP_CRITICAL} C" config_row "Fan speeds" "${FAN_SPEED_IDLE}/${FAN_SPEED_LOW}/${FAN_SPEED_MEDIUM}/${FAN_SPEED_HIGH}/${FAN_SPEED_CRITICAL}%" - config_row "Hysteresis" "${HYSTERESIS} C" + config_row "Fan speed interpolation" "$FAN_SPEED_INTERPOLATION ($(fan_curve_mode))" + config_row "Hysteresis" "$(hysteresis_description)" config_row "Fail-safe" "enabled=${FAILSAFE_ON_ERROR}, speed=${FAILSAFE_FAN_SPEED}%" config_row "Restore on exit" "$RESTORE_AUTO_ON_EXIT" config_row "Timing" "interval=${CHECK_INTERVAL}s, command-timeout=${COMMAND_TIMEOUT}s" @@ -1230,6 +1286,7 @@ diagnose_mode() { printf 'iDRAC Fan Control diagnostics (read-only)\n' printf 'Generated: %s\n\n' "$(timestamp)" diagnostic_row "PASS" "configuration" "static validation passed" + diagnostic_row "PASS" "fan curve" "$(fan_curve_mode); hysteresis: $(hysteresis_description)" if [[ -z "$LOG_DIR" ]] || prepare_log_dir; then diagnostic_row "PASS" "log path" "${LOG_DIR:-disabled}" diff --git a/src/fan-control-tui.sh b/src/fan-control-tui.sh index 1e35769..54b61a8 100755 --- a/src/fan-control-tui.sh +++ b/src/fan-control-tui.sh @@ -20,7 +20,7 @@ CONFIG_KEYS=( REMOTE_GPU_SSH_PORT REMOTE_GPU_TEMP_OFFSET TEMP_LOW TEMP_MEDIUM TEMP_HIGH TEMP_CRITICAL FAN_SPEED_IDLE FAN_SPEED_LOW FAN_SPEED_MEDIUM FAN_SPEED_HIGH FAN_SPEED_CRITICAL - HYSTERESIS FAILSAFE_ON_ERROR FAILSAFE_FAN_SPEED MANUAL_FAN_SPEED + FAN_SPEED_INTERPOLATION HYSTERESIS FAILSAFE_ON_ERROR FAILSAFE_FAN_SPEED MANUAL_FAN_SPEED LOG_DIR LOG_FILE LOG_LEVEL HEALTHCHECK_MAX_AGE ) @@ -273,6 +273,9 @@ prompt_boolean() { local value current="$(config_get "$key" 2>/dev/null || true)" + if [[ "$key" == FAN_SPEED_INTERPOLATION && -z "$current" ]]; then + current=true + fi while true; do printf '%s (yes/no) [%s]: ' "$label" "${current:-empty}" IFS= read -r value || return 1 @@ -384,6 +387,10 @@ configure_curve() { header printf '%sFan curve%s\n' "$COLOR_BLUE" "$COLOR_RESET" printf '%sThresholds and speeds must rise from idle to critical.%s\n\n' "$COLOR_DIM" "$COLOR_RESET" + printf 'Interpolation uses low/medium/high/critical anchors, rounded up.\n' + printf 'Below low uses idle; distinct idle/low speeds jump at the low threshold.\n' + printf 'Hysteresis applies only when interpolation is disabled.\n\n' + prompt_boolean FAN_SPEED_INTERPOLATION "Enable piecewise linear interpolation" prompt_integer TEMP_LOW "Low threshold (C)" 0 120 prompt_integer TEMP_MEDIUM "Medium threshold (C)" 0 120 prompt_integer TEMP_HIGH "High threshold (C)" 0 120 @@ -401,7 +408,7 @@ configure_safety() { header printf '%sSafety, timing, and logging%s\n\n' "$COLOR_BLUE" "$COLOR_RESET" - prompt_integer HYSTERESIS "Downshift hysteresis (C)" 0 30 + prompt_integer HYSTERESIS "Downshift hysteresis (C; stepwise mode only)" 0 30 prompt_integer CHECK_INTERVAL "Automatic check interval (seconds)" 1 86400 prompt_integer COMMAND_TIMEOUT "External command timeout (seconds)" 1 300 prompt_boolean FAILSAFE_ON_ERROR "Apply fail-safe if every source fails" @@ -452,11 +459,13 @@ show_config() { local idrac_password local esxi_password local remote_gpu_password + local interpolation header idrac_password="$(config_get IDRAC_PASSWORD 2>/dev/null || true)" esxi_password="$(config_get ESXI_PASSWORD 2>/dev/null || true)" remote_gpu_password="$(config_get REMOTE_GPU_PASSWORD 2>/dev/null || true)" + interpolation="$(config_get FAN_SPEED_INTERPOLATION 2>/dev/null || true)" printf '%sEffective setup (secrets redacted)%s\n\n' "$COLOR_BLUE" "$COLOR_RESET" printf ' %-28s %s\n' "Mode" "$(config_get OPERATION_MODE 2>/dev/null || true)" printf ' %-28s %s\n' "Sources" "$(config_get TEMPERATURE_SOURCES 2>/dev/null || true)" @@ -470,6 +479,8 @@ show_config() { printf ' %-28s %s\n' "Remote GPU password" "$(masked_state "$remote_gpu_password")" printf ' %-28s %s/%s/%s/%s C\n' "Thresholds" "$(config_get TEMP_LOW)" "$(config_get TEMP_MEDIUM)" "$(config_get TEMP_HIGH)" "$(config_get TEMP_CRITICAL)" printf ' %-28s %s/%s/%s/%s/%s %%\n' "Fan speeds" "$(config_get FAN_SPEED_IDLE)" "$(config_get FAN_SPEED_LOW)" "$(config_get FAN_SPEED_MEDIUM)" "$(config_get FAN_SPEED_HIGH)" "$(config_get FAN_SPEED_CRITICAL)" + printf ' %-28s %s\n' "Fan speed interpolation" "${interpolation:-true}" + printf ' %-28s %s C (stepwise mode only)\n' "Hysteresis" "$(config_get HYSTERESIS)" printf ' %-28s enabled=%s, %s%%\n' "Fail-safe" "$(config_get FAILSAFE_ON_ERROR)" "$(config_get FAILSAFE_FAN_SPEED)" printf ' %-28s %s\n' "Log level" "$(config_get LOG_LEVEL 2>/dev/null || printf INFO)" printf '\n%sNo fan command was sent.%s\n' "$COLOR_DIM" "$COLOR_RESET" diff --git a/tests/fan-control.test.sh b/tests/fan-control.test.sh index 00ef328..f1369b6 100755 --- a/tests/fan-control.test.sh +++ b/tests/fan-control.test.sh @@ -126,6 +126,7 @@ test_esxi_password_is_not_in_process_arguments() { } test_temperature_levels() { + FAN_SPEED_INTERPOLATION=false TEMP_LOW=65 TEMP_MEDIUM=70 TEMP_HIGH=75 @@ -145,6 +146,7 @@ test_temperature_levels() { } test_hysteresis_only_delays_downshift() { + FAN_SPEED_INTERPOLATION=false TEMP_LOW=65 TEMP_MEDIUM=70 TEMP_HIGH=75 @@ -553,6 +555,178 @@ test_diagnose_reports_read_only_source_and_preview() { assert_contains "decision preview" "$output" "diagnose previews the fan decision" } +# Isolate curve settings and hardware stubs while retaining assertion totals. +run_isolated_test() { + local count + count="$("$1")" + PASS_COUNT="$count" +} + +interpolation_test_curve() { + TEMP_LOW=65 TEMP_MEDIUM=70 TEMP_HIGH=75 TEMP_CRITICAL=80 + FAN_SPEED_IDLE=30 FAN_SPEED_LOW=35 FAN_SPEED_MEDIUM=45 + FAN_SPEED_HIGH=60 FAN_SPEED_CRITICAL=65 HYSTERESIS=2 +} + +test_interpolation_enabled_by_default() { + local output + output="$(env -u FAN_SPEED_INTERPOLATION bash -c ' + source "$1" + TEMP_LOW=65 TEMP_MEDIUM=70 TEMP_HIGH=75 TEMP_CRITICAL=80 + FAN_SPEED_LOW=30 FAN_SPEED_MEDIUM=40 FAN_SPEED_HIGH=50 FAN_SPEED_CRITICAL=60 + choose_fan_speed 72 + ' bash "${ROOT_DIR}/src/FanControlWithEsxiSmart.sh")" + assert_eq $'medium\t44' "$output" "missing switch enables interpolation" +} + +test_interpolation_anchors_rounding_and_bounds() ( + interpolation_test_curve + FAN_SPEED_INTERPOLATION=true + local temp level speed previous=0 + local -a cases=( + '0 idle 30' '64 idle 30' '65 low 35' '66 low 37' + '69 low 43' '70 medium 45' '71 medium 48' '72 medium 51' + '73 medium 54' '74 medium 57' '75 high 60' '76 high 61' + '79 high 64' '80 critical 65' '120 critical 65' + ) + for fixture in "${cases[@]}"; do + read -r temp level speed <<< "$fixture" + assert_eq "${level}"$'\t'"${speed}" "$(choose_fan_speed "$temp" critical)" "interpolates ${temp}C regardless of previous level" + done + for ((temp=0; temp<=120; temp++)); do + IFS=$'\t' read -r level speed <<< "$(choose_fan_speed "$temp")" + (( speed >= previous && speed >= 30 && speed <= 65 )) || fail "curve must be monotonic and bounded at ${temp}C" + previous="$speed" + done + pass + + TEMP_LOW=10 TEMP_MEDIUM=13 TEMP_HIGH=20 TEMP_CRITICAL=31 + FAN_SPEED_IDLE=1 FAN_SPEED_LOW=1 FAN_SPEED_MEDIUM=2 FAN_SPEED_HIGH=6 FAN_SPEED_CRITICAL=100 + assert_eq $'idle\t1' "$(choose_fan_speed 9)" "preserves minimum idle duty below the first anchor" + assert_eq $'low\t1' "$(choose_fan_speed 10)" "preserves minimum anchor duty" + assert_eq $'low\t2' "$(choose_fan_speed 11)" "ceil fractional duty in a three-degree segment" + assert_eq $'medium\t3' "$(choose_fan_speed 14)" "uses actual uneven segment width" + assert_eq $'high\t49' "$(choose_fan_speed 25)" "ceil duty in an eleven-degree segment" + assert_eq $'critical\t100' "$(choose_fan_speed 120)" "clamps to maximum duty" + TEMP_MEDIUM=14 + assert_eq $'low\t2' "$(choose_fan_speed 12)" "ceil demand at an exact segment midpoint" + FAN_SPEED_IDLE=100 FAN_SPEED_LOW=100 FAN_SPEED_MEDIUM=100 FAN_SPEED_HIGH=100 + assert_eq $'medium\t100' "$(choose_fan_speed 16)" "accepts flat fan anchors" + printf '%s\n' "$PASS_COUNT" +) + +test_legacy_mode_exact_hysteresis() ( + interpolation_test_curve + FAN_SPEED_INTERPOLATION=false + assert_eq $'medium\t45' "$(choose_fan_speed 72)" "disabled interpolation restores stepwise speed" + assert_eq $'high\t60' "$(choose_fan_speed 74 high)" "legacy retains downshift inside band" + assert_eq $'medium\t45' "$(choose_fan_speed 73 high)" "legacy releases at exact hysteresis boundary" + assert_eq $'critical\t65' "$(choose_fan_speed 80 idle)" "legacy never delays upward cooling" + assert_eq $'low\t35' "$(choose_fan_speed 64 low)" "legacy retains low near idle boundary" + assert_eq $'idle\t30' "$(choose_fan_speed 63 low)" "legacy releases low at boundary" + assert_eq $'idle\t30' "$(choose_fan_speed 60 critical)" "legacy supports multi-level downshift" + printf '%s\n' "$PASS_COUNT" +) + +test_interpolation_validation_and_config() ( + interpolation_test_curve + OPERATION_MODE=auto TEMPERATURE_SOURCES=idrac WITH_GPU_TEMP=false DRY_RUN=true + FAN_SPEED_INTERPOLATION=invalid + if validate_config auto >/dev/null 2>&1; then + fail "reject invalid interpolation boolean" + fi + pass + # Recovery must remain available even if the new curve option is malformed. + validate_config restore >/dev/null 2>&1 || fail "invalid interpolation must not block restore" + pass + FAN_SPEED_INTERPOLATION=true + validate_config auto >/dev/null 2>&1 || fail "accept enabled interpolation" + assert_contains 'piecewise linear' "$(print_effective_config)" "effective config identifies interpolation" + assert_contains 'inactive' "$(print_effective_config)" "effective config explains unused hysteresis" + FAN_SPEED_INTERPOLATION=false + validate_config auto >/dev/null 2>&1 || fail "accept disabled interpolation" + assert_contains 'stepwise' "$(print_effective_config)" "effective config identifies legacy mode" + printf '%s\n' "$PASS_COUNT" +) + +test_control_cycle_interpolation_and_recovery() ( + interpolation_test_curve + FAN_SPEED_INTERPOLATION=true FAILSAFE_ON_ERROR=true FAILSAFE_FAN_SPEED=70 + LAST_LEVEL=high LAST_SPEED=60 + local reading=74 broken=false + local -a applied=() + get_decision_temperature() { + [[ "$broken" == false ]] || return 1 + printf '%s\ttest sensor\n' "$reading" + } + set_fan_speed() { applied+=("$1"); } + control_cycle >/dev/null 2>&1 + assert_eq 57 "$LAST_SPEED" "control cycle cools down without level hysteresis plateau" + assert_eq medium "$LAST_LEVEL" "level remains the raw temperature band" + reading=73 + control_cycle >/dev/null 2>&1 + assert_eq 54 "$LAST_SPEED" "updates speed within the same level" + reading=74 + control_cycle >/dev/null 2>&1 + assert_eq 57 "$LAST_SPEED" "upward interpolation is immediate within a level" + control_cycle >/dev/null 2>&1 + assert_eq '57 54 57' "${applied[*]}" "only changed duty sends a command" + broken=true + control_cycle >/dev/null 2>&1 + assert_eq 70 "$LAST_SPEED" "all-source failure enters failsafe" + assert_eq failsafe "$LAST_LEVEL" "records failsafe state" + broken=false reading=72 + control_cycle >/dev/null 2>&1 + assert_eq 51 "$LAST_SPEED" "recovers from failsafe to interpolated demand" + assert_eq medium "$LAST_LEVEL" "clears failsafe level after recovery" + set_fan_speed() { return 1; } + reading=75 + if control_cycle >/dev/null 2>&1; then fail "failed duty command must fail cycle"; fi + assert_eq 51 "$LAST_SPEED" "failed command does not overwrite last successful speed" + assert_eq medium "$LAST_LEVEL" "failed command does not overwrite last successful level" + set_fan_speed() { applied+=("$1"); } + FAN_SPEED_INTERPOLATION=false LAST_LEVEL=high LAST_SPEED=60 reading=74 + control_cycle >/dev/null 2>&1 + assert_eq 60 "$LAST_SPEED" "legacy control cycle retains downshift hysteresis" + reading=73 + control_cycle >/dev/null 2>&1 + assert_eq 45 "$LAST_SPEED" "legacy control cycle releases hysteresis exactly" + broken=true + control_cycle >/dev/null 2>&1 + broken=false reading=72 + control_cycle >/dev/null 2>&1 + assert_eq 45 "$LAST_SPEED" "legacy recovers from failsafe without an unknown-level hold" + printf '%s\n' "$PASS_COUNT" +) + +test_diagnose_interpolation_modes() ( + interpolation_test_curve + OPERATION_MODE=auto TEMPERATURE_SOURCES=idrac WITH_GPU_TEMP=false DRY_RUN=false + LOG_DIR="" + local output + run_ipmitool() { + [[ "$*" == 'mc info' ]] || fail "diagnose must not send a fan command" + printf 'Firmware Revision : test\n' + } + get_idrac_temperatures() { printf 'idrac\tTest sensor\t72\n'; } + FAN_SPEED_INTERPOLATION=true + output="$(diagnose_mode)" + assert_contains '72C -> medium (51%)' "$output" "diagnose previews interpolated duty" + assert_contains 'piecewise linear' "$output" "diagnose identifies interpolation mode" + assert_contains 'inactive' "$output" "diagnose explains inactive hysteresis" + FAN_SPEED_INTERPOLATION=false + output="$(diagnose_mode)" + assert_contains '72C -> medium (45%)' "$output" "diagnose previews legacy duty" + assert_contains 'stepwise' "$output" "diagnose identifies legacy mode" + printf '%s\n' "$PASS_COUNT" +) + +run_isolated_test test_diagnose_interpolation_modes +test_interpolation_enabled_by_default +run_isolated_test test_interpolation_anchors_rounding_and_bounds +run_isolated_test test_legacy_mode_exact_hysteresis +run_isolated_test test_interpolation_validation_and_config +run_isolated_test test_control_cycle_interpolation_and_recovery test_normalize_sources test_temperature_sources_share_complete_interface test_hex_formatting diff --git a/tests/tui.test.sh b/tests/tui.test.sh index 0d2b47d..4967117 100644 --- a/tests/tui.test.sh +++ b/tests/tui.test.sh @@ -101,6 +101,35 @@ test_new_source_configuration_round_trips() { assert_eq 'set (13 characters)' "$(masked_state "$(config_get REMOTE_GPU_PASSWORD)")" "masks the remote GPU credential" } +test_interpolation_round_trip() { + assert_eq true "$(config_get FAN_SPEED_INTERPOLATION)" "new configurations enable interpolation" + configure_curve <<< $'no\n\n\n\n\n\n\n\n\n' >/dev/null + assert_eq false "$(config_get FAN_SPEED_INTERPOLATION)" "TUI disables interpolation" + load_config_environment + assert_eq false "$FAN_SPEED_INTERPOLATION" "controller environment preserves false" + prompt_boolean FAN_SPEED_INTERPOLATION 'Interpolation' <<< yes >/dev/null + assert_eq true "$(config_get FAN_SPEED_INTERPOLATION)" "TUI enables interpolation" + load_config_environment + assert_eq true "$FAN_SPEED_INTERPOLATION" "controller environment preserves true" +} + +test_legacy_config_defaults_to_interpolation() { + local CONFIG_FILE="${TMP_DIR}/legacy.env" + local output + sed '/^FAN_SPEED_INTERPOLATION=/d' "$FAN_CONTROL_CONFIG" > "$CONFIG_FILE" + output="$(show_config <<< '')" + assert_eq true "$(awk '/Fan speed interpolation/ {print $NF}' <<< "$output")" "review defaults existing env files to interpolation" + config_set FAN_SPEED_INTERPOLATION '' + output="$(show_config <<< '')" + assert_eq true "$(awk '/Fan speed interpolation/ {print $NF}' <<< "$output")" "review treats an empty switch like the controller default" + configure_curve <<< $'\n\n\n\n\n\n\n\n\n' >/dev/null + assert_eq true "$(config_get FAN_SPEED_INTERPOLATION)" "blank TUI input applies default to existing env files" +} + +test_legacy_config_defaults_to_interpolation + +test_interpolation_round_trip + test_config_round_trip_and_preserves_comments test_config_set_does_not_duplicate_keys test_config_rejects_unknown_key_and_newline