2024-05-19 22:38:21 +08:00
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
from controller.client import FanController
|
|
|
|
|
from controller.logger import logger
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
2026-05-06 09:23:26 +08:00
|
|
|
# 从环境变量读取iDRAC连接信息,开源版本不内置任何真实默认凭据
|
|
|
|
|
host = os.getenv('HOST')
|
|
|
|
|
username = os.getenv('USERNAME')
|
|
|
|
|
password = os.getenv('PASSWORD')
|
2026-05-06 10:04:15 +08:00
|
|
|
# 优先读取新的温控档位变量,并兼容旧别名
|
|
|
|
|
fan_speed_steps = os.getenv('FAN_SPEED_STEPS')
|
|
|
|
|
if fan_speed_steps is None:
|
|
|
|
|
fan_speed_steps = os.getenv('FAN_SPEED_RULES')
|
2025-12-02 17:55:08 +08:00
|
|
|
if not host:
|
2025-12-02 17:47:51 +08:00
|
|
|
raise RuntimeError('未设置 HOST 环境变量')
|
2024-05-19 22:38:21 +08:00
|
|
|
|
2025-12-02 17:55:08 +08:00
|
|
|
if not username:
|
2025-12-02 17:47:51 +08:00
|
|
|
raise RuntimeError('未设置 USERNAME 环境变量')
|
2024-05-19 22:38:21 +08:00
|
|
|
|
2025-12-02 17:55:08 +08:00
|
|
|
if not password:
|
2025-12-02 17:47:51 +08:00
|
|
|
raise RuntimeError('未设置 PASSWORD 环境变量')
|
2024-05-19 22:38:21 +08:00
|
|
|
|
2026-05-06 09:23:26 +08:00
|
|
|
# 复用控制器实例,避免每轮循环丢失上次设置状态
|
2026-05-06 10:04:15 +08:00
|
|
|
client = FanController(
|
|
|
|
|
host=host,
|
|
|
|
|
username=username,
|
|
|
|
|
password=password,
|
|
|
|
|
fan_speed_steps=fan_speed_steps,
|
|
|
|
|
)
|
2026-05-06 09:05:53 +08:00
|
|
|
|
2024-05-19 22:38:21 +08:00
|
|
|
while True:
|
2025-12-02 17:47:51 +08:00
|
|
|
try:
|
2026-05-06 09:23:26 +08:00
|
|
|
# 执行一次温度读取和风扇控制周期
|
2024-05-19 22:38:21 +08:00
|
|
|
client.run()
|
|
|
|
|
time.sleep(60)
|
|
|
|
|
except Exception as err:
|
|
|
|
|
logger.error(
|
2025-06-08 12:50:52 +08:00
|
|
|
f'运行控制器失败 {err}. {traceback.format_exc()}'
|
2026-05-06 09:05:53 +08:00
|
|
|
)
|
2026-05-06 09:23:26 +08:00
|
|
|
# iDRAC会话异常时等待下一轮,避免连续请求压垮IPMI服务
|
2026-05-06 09:05:53 +08:00
|
|
|
time.sleep(60)
|