dell_t620_fan_speed_controler.sh 5.74 KB
Newer Older
1
2
#!/bin/sh
ipmi_host=""
3
ipmi_user=""
4
5
ipmi_passwd=""

6
7
8
snmp_host=""
snmp_ver="2c"
snmp_com="public"
9

10
# 三个温度值将转速划分为四个区间
11
12
temp_threshold="58,60,65"
fan_speed="30,40,70,100"
13
14

#日志文件路径
15
log_path='/var/log/t620_ipmi_fan_speed_controller.log'
16
17

# 检查是否已安装bc
18
19
20
21
if [ ! "$(rpm -qa | grep -c -E "^bc|net-snmp-utils")" -eq 2 ]; then
  echo "You need to install bc and net-snmp-utils."
  echo "You can try \"yum install bc net-snmp-utils -y\""
  exit 2  
Terence Chuen's avatar
Terence Chuen committed
22
fi
23

24
# 获取当前的CPU温度
25
26
27
28
29
get_cpu1_temp=$(snmpwalk -v $snmp_ver -c $snmp_com $snmp_host \
SNMPv2-SMI::enterprises.674.10892.5.4.700.20.1.6.1.2 | cut -d " " -f 4)

get_cpu2_temp=$(snmpwalk -v $snmp_ver -c $snmp_com $snmp_host \
SNMPv2-SMI::enterprises.674.10892.5.4.700.20.1.6.1.3 | cut -d " " -f 4)
30

31
# 获取当前进气温度,用于写入日志
32
33
inlet_temp=$(snmpwalk -v $snmp_ver -c $snmp_com $snmp_host \
SNMPv2-SMI::enterprises.674.10892.5.4.700.20.1.6.1.1 | cut -d " " -f 4)
34
35

# 计算CPU温度均值
36
calc_avg_temp=$(echo "($get_cpu1_temp + $get_cpu2_temp)/20" | bc)
37
38

# 确定温度区间
39
40
41
42
43
44
if [ "$(echo "$calc_avg_temp > $(echo $temp_threshold | cut -d ',' -f 3)" | bc)" -eq 1 ]; then
  speed_range=4
elif [ "$(echo "$calc_avg_temp > $(echo $temp_threshold | cut -d ',' -f 2)" | bc)" -eq 1 ]; then
  speed_range=3
elif [ "$(echo "$calc_avg_temp > $(echo $temp_threshold | cut -d ',' -f 1)" | bc)" -eq 1 ]; then
  speed_range=2
45
else
46
  speed_range=1
47
48
fi

Terence Chuen's avatar
Terence Chuen committed
49
# 转速进制转换
50
51
speed_hex="0x"$(echo "obase=16; $(echo $fan_speed | cut -d ',' -f $speed_range)" | bc)
fan_speed=$(echo $fan_speed | cut -d ',' -f $speed_range)%
Terence Chuen's avatar
Terence Chuen committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
inlet_temp=$((inlet_temp / 10))

# 判断系统是否重启
sys_boot_time=$(who -b | sed s/[[:space:]]//g)
if [ -f .t620_ipmi_fan_speed_controller.tmp ]; then
  if [ "$(cat .t620_ipmi_fan_speed_controller.tmp)" != "$sys_boot_time" ]; then
    ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x01 0x00
    ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x02 0xff "$speed_hex"
    echo "$(date '+%Y-%m-%d %H:%M:%S') cpu_temp=$calc_avg_temp inlet_temp=$inlet_temp \
fan_speed_range=$speed_range fan_speed=$fan_speed msg=\"System reboot, Cooling fan reduces speed\"" >> $log_path
    echo $sys_boot_time > .t620_ipmi_fan_speed_controller.tmp
    exit 0
  fi
else
  ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x01 0x00
  ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x02 0xff "$speed_hex"
  echo "$(date '+%Y-%m-%d %H:%M:%S') cpu_temp=$calc_avg_temp inlet_temp=$inlet_temp \
fan_speed_range=$speed_range fan_speed=$fan_speed msg=\"System reboot, Cooling fan reduces speed\"" >> $log_path
  echo $sys_boot_time > .t620_ipmi_fan_speed_controller.tmp
  exit 0
fi
73
74
75

# 判断日志文件是否存在
if [ -f $log_path ]; then
76
77
  # 获取最后一条日志中的转速区间值
  fan_speed_now=$(tail -n 1 $log_path | cut -d '=' -f 4 | cut -d ' ' -f 1)
78

79
80
  # 若目标区间值大于最后一条日志中的区间值,则立即上调转速,然后写入日志并退出程序
  if [ "$speed_range" -gt "$fan_speed_now" ]; then
Terence Chuen's avatar
Terence Chuen committed
81
    ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x01 0x00
82
83
84
85
86
87
88
89
90
91
    ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x02 0xff "$speed_hex"
    echo "$(date '+%Y-%m-%d %H:%M:%S') cpu_temp=$calc_avg_temp inlet_temp=$inlet_temp \
fan_speed_range=$speed_range fan_speed=$fan_speed msg=\"Cooling fan increases speed\"" >> $log_path
    exit 0
  elif [ "$speed_range" -eq "$fan_speed_now" ]; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') cpu_temp=$calc_avg_temp inlet_temp=$inlet_temp \
fan_speed_range=$fan_speed_now fan_speed=$fan_speed msg=\"Cooling fan maintains speed\"" >> $log_path
    exit 0
  else
    # 获取最后5条日志中的CPU温度值并计算均值
92
    for i in $(tail -n 10 $log_path);
93
94
95
96
97
98
99
100
101
102
103
    do
      i_tmp=$(echo "$i" | sed -e 's/^cpu_temp=\(.*\)/\1/g;t;d')
      if [ "$i_tmp" ]; then
        if [ "$last_few_min_cpu_temp_avg" ]; then
          last_few_min_cpu_temp_avg=$(echo "($last_few_min_cpu_temp_avg + $i_tmp)/2" | bc)
        else
          last_few_min_cpu_temp_avg=$i_tmp
        fi
      fi
    done
  fi
104
else
105
  # 若日志文件不存在,则立即调整转速并写入日志
Terence Chuen's avatar
Terence Chuen committed
106
  ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x01 0x00
107
108
109
110
  ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x02 0xff "$speed_hex"
  echo "$(date '+%Y-%m-%d %H:%M:%S') cpu_temp=$calc_avg_temp inlet_temp=$inlet_temp \
fan_speed_range=$speed_range fan_speed=$fan_speed msg=\"The log file does not exist, adjust the speed immediately\"" >> $log_path
  exit 0
111
112
113
fi

# 若日志文件存在,但需要降低转速,则需要进行判断温度是否在合适的范围
114
115
116
# 当 “(最近10分钟的温度均值-最新CPU温度均值)/最新CPU温度均值” 大于0且小于0.03时,方可降低转速
#temp_calc=$(echo "scale=2; ($last_few_min_cpu_temp_avg - $calc_avg_temp)/$calc_avg_temp" | bc)
temp_calc=$[$last_few_min_cpu_temp_avg - $calc_avg_temp]
117

118
119
#if [ `echo "0 < $temp_calc" | bc` -eq 1 -a `echo "$temp_calc < 0.03" | bc` -eq 1 ]; then
if [ "$temp_calc" -le "-1" ]; then
Terence Chuen's avatar
Terence Chuen committed
120
  ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x01 0x00
121
122
123
124
  ipmitool -I lanplus -H $ipmi_host -U $ipmi_user -P $ipmi_passwd raw 0x30 0x30 0x02 0xff "$speed_hex"
  echo "$(date '+%Y-%m-%d %H:%M:%S') cpu_temp=$calc_avg_temp inlet_temp=$inlet_temp \
fan_speed_range=$speed_range fan_speed=$fan_speed msg=\"Cooling fan reduces speed, $temp_calc\"" >> $log_path
  exit 0
125
else
126
127
128
  echo "$(date '+%Y-%m-%d %H:%M:%S') cpu_temp=$calc_avg_temp inlet_temp=$inlet_temp \
fan_speed_range=$fan_speed_now fan_speed=$fan_speed msg=\"Waiting for the temperature drop, $temp_calc\"" >> $log_path
  exit 0
Terence Chuen's avatar
Terence Chuen committed
129
fi