-
Notifications
You must be signed in to change notification settings - Fork 8
/
FanControl.tool
executable file
·897 lines (734 loc) · 22.9 KB
/
FanControl.tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
#!/bin/bash
#
# Config
#
# shellcheck disable=SC2236
#
# Write out a default config file
function fcConfig {
tee > "${configFile}" <<"EOF"
# FanControl config file
# Set this to 0 to enable
defaultFile="1"
# Fan settings
autoFanDuty="0" # Value that sets fan to auto based on CPU temp.
minFanDuty="30" # Minimum effective value to set duty level to.
maxFanDuty="100" # Maxim value to set duty level to.
difFanDuty="10" # The difference maintained between intake and exhaust fans
mixedFans="0" # Set to one if the same fans are responsible for both hdd and ssd
# Temperatures in Celsius
targetHDriveTemp="31" # The temperature that we try to maintain for HDs.
targetSSDriveTemp="36" # The temperature that we try to maintain for SSDs.
maxDriveTemp="39" # Do not let drives get hotter than this.
ambTempVariance="1" # How many degrees the ambient temperature may effect the target
# CPU settings
# Currently unused
# targetCPUTemp="35" # The temperature that we try to maintain.
# maxCPUTemp="55" # Do not let the CPU get hotter than this.
# cpuCheckTempInterval="2" # In seconds.
# HBA Settings
targetHBATemp="41" # The temperature that we try to maintain.
maxHBATemp="55" # Do not let the HBA get hotter than this.
# PID Controls
Kp="4" # Proportional tunable constant
Ki="0.040" # Integral tunable constant
Kd="16" # Derivative tunable constant
# Time interval to check disk temps in mins
diskCheckTempInterval="2"
# List of HDs
hdName=(
da0
da1
da2
da3
da4
da5
da6
da7
)
# List of SSDs
ssdName=(
ada0
ada1
ada2
ada3
ada4
ada5
)
# List of HBAs
# See https://gist.github.com/dak180/cd44e9957e1c4180e7eb6eb000716ee2
lsi_temp="/mnt/jails/scripts/lsi_temp" # path to compiled program ^
# sesutil map -u /dev/sesX will tell you if there are temp sensors in your
# backplane or expander that you can read.
hbaName=(
mpr0
ses0
)
# Everything below this line is MB specific.
# These examples are for an ASRockRack E3C236D4U.
#
# Currently unused
#
# Fan sensor name prefixes; numbers will be added
# senNamePrefixCPU_FAN="CPU_FAN"
# senNamePrefixFRNT_FAN="FRNT_FAN"
# senNamePrefixREAR_FAN="REAR_FAN"
# Temp sensors
# The names used by ipmi.
# cpuTempSens[0]="CPU1 Temp" # CPU temp Currently unused
hbaTempSens[0]="TR1 Temp" # HBA temp
ambTempSens[0]="MB Temp" # Ambient temp
ambTempSens[1]="Card Side Temp" # Ambient temp
if [ ! "${#hbaTempSens[@]}" = "0" ]; then
hbaName+=("${hbaTempSens[@]}")
fi
# IPMI Fan Commands
#
# The script curently tracks five different types of fan values:
# CPU_FAN is used for fans that directly cools the cpu.
# HBA_FAN is used for fans that directly cools a HBA card.
# FRNT_FAN is used for intake fans.
# REAR_FAN is used for exhaust fans.
# NIL_FAN is used for space holder values that are not fans.
#
# Make sure that you set values here corectly for your board.
# The following is a template for the ASRockRack E3C236D4U board other ASRockRack
# boards may use a similar call.
# ipmitool raw 0x3a 0x01 ${CPU_FAN1} ${Reserved} ${REAR_FAN1} ${REAR_FAN2} ${FRNT_FAN1} ${FRNT_FAN2} ${FRNT_FAN3} ${Reserved}
# The command to set the desired fan duty levels.
function ipmiWrite {
if ! ipmitool raw 0x3a 0x01 "${CPU_FAN[0]}" "${NIL_FAN[0]}" "${REAR_FAN[0]}" "${NIL_FAN[1]}" "${FRNT_FAN[0]}" "${SSD_FAN[0]}" "${HBA_FAN[0]}" "${NIL_FAN[2]}"; then
return ${?}
fi
}
# A function to read the current fan duty levels.
# It converts hex values to decimal and seperates them by type.
function ipmiRead {
local rawFan
local rawFanArray
# Command to get current fan duty levels as a space separated list.
rawFan="$(ipmitool raw 0x3a 0x02 | sed -e 's:^ *::')"
read -ra rawFanArray <<< "${rawFan}"
# Assign named values from the raw array.
CPU_FAN[0]="$(hexConv "${rawFanArray[0]}")"
NIL_FAN[0]="$(hexConv "${rawFanArray[1]}")"
REAR_FAN[0]="$(hexConv "${rawFanArray[2]}")"
NIL_FAN[1]="$(hexConv "${rawFanArray[3]}")"
FRNT_FAN[0]="$(hexConv "${rawFanArray[4]}")"
FRNT_FAN[1]="$(hexConv "${rawFanArray[5]}")"
FRNT_FAN[2]="$(hexConv "${rawFanArray[6]}")"
NIL_FAN[2]="$(hexConv "${rawFanArray[7]}")"
# Remap from MB supplied names.
HBA_FAN[0]="${FRNT_FAN[2]}"
SSD_FAN[0]="${FRNT_FAN[1]}"
}
EOF
exit 0
}
#
# Functions Start Here
#
# ipmi sensor command
function ipmiSens {
local sensName="${1}"
ipmitool -c sdr get "${sensName}" | cut -d ',' -f 2
}
# Convert Hex to decimal
function hexConv {
local hexIn="${1}"
echo "$((0x${hexIn}))"
}
# Round to nearest whole number
function roundR {
bc <<< "scale=0;(${1} + 0.5) / 1"
}
# Get average or high CPU temperature.
function cpuTemp {
local numberCPU="${1}"
local numberCPUArray
local coreCPU
local cpuTempCur
local cpuTempAv="0"
local cpuTempMx="0"
read -ra numberCPUArray <<< "$(seq "0 ${numberCPU}")"
for coreCPU in "${numberCPUArray[@]}"; do
cpuTempCur="$(sysctl -n "dev.cpu.${coreCPU}.temperature" | sed -e 's:C::')"
# Start adding temps for an average.
cpuTempAv="$(bc <<< "scale=3;${cpuTempCur} + ${cpuTempAv}")"
# Keep track of the highest current temp
if [ "$(roundR "${cpuTempMx}")" -gt "$(roundR "${cpuTempCur}")" ]; then
true
else
cpuTempMx="${cpuTempCur}"
fi
done
# Divide by number of CPUs for average.
hdTempAv="$(bc <<< "scale=3;${cpuTempAv} / ${#numberCPUArray[@]}")"
# If the hottest CPU matches/exceeds the max temp use that
# instead of the average.
if [ "${cpuTempMx}" -gt "${cpuTempCur}" ]; then
true
else
cpuTempMx="${cpuTempCur}"
fi
}
# Get set point temp
function targetTemp {
local ambTemIn
local ambTemOut="0"
local ambTemCur
local targetDiskTemp="${1}"
for ambTemIn in "${ambTempSens[@]}"; do
# Get the current ambent temp readings.
ambTemCur="$(ipmiSens "${ambTemIn}")"
# Start adding temps for an average.
ambTemOut="$(bc <<< "scale=0;${ambTemOut} + ${ambTemCur}")"
done
# Divide by number of sensors for average.
ambTemOut="$(bc <<< "scale=3;${ambTemOut} / ${#ambTempSens[@]}")"
ambTemRoun="$(roundR "${ambTemOut}")"
ambTemComp="$(bc <<< "scale=0;${ambTemOut} / 1")"
# Alow the target temp to vary by $ambTempVariance degrees based on
# a difference between ambent internal temp and $targetDiskTemp.
if [ "${ambTemComp}" = "${targetDiskTemp}" ]; then
echo "${ambTemOut}"
else
if [ "${ambTemRoun}" -gt "${targetDiskTemp}" ]; then
if [ "${ambTemRoun}" -gt "$(bc <<< "scale=0;${targetDiskTemp} + ${ambTempVariance}")" ]; then
bc <<< "scale=3;${targetDiskTemp} + ${ambTempVariance}"
return 0
fi
elif [ "${targetDiskTemp}" -gt "${ambTemComp}" ]; then
if [ "$(bc <<< "scale=0;${targetDiskTemp} - ${ambTempVariance}")" -gt "${ambTemComp}" ]; then
bc <<< "scale=3;${targetDiskTemp} - ${ambTempVariance}"
return 0
fi
fi
echo "${ambTemOut}"
fi
}
# Get average or high HD temperature.
function hdTemp {
local hdNum
local hdTempCur
local hdTempAv="0"
local hdTempMx="0"
local hdComp="0"
for hdNum in "${hdName[@]}"; do
# Get the temp for the current drive.
hdTempCur="$(smartctl -aj "/dev/${hdNum}" | jq -Mre '.temperature.current | values')"
# Start adding temps for an average.
hdTempAv="$(( hdTempAv + hdTempCur ))"
# Track non responsive drives
if [ -z "${hdTempCur}" ]; then
hdComp="$(( hdComp + 1 ))"
fi
# Keep track of the highest current temp
if [ "${hdTempMx}" -gt "${hdTempCur}" ]; then
true
else
hdTempMx="${hdTempCur}"
fi
done
# Divide by number of drives for average.
hdTempAv="$(bc <<< "scale=3;${hdTempAv} / (${#hdName[@]} - ${hdComp})")"
# If the hottest drive matches/exceeds the max temp use that instead
# of the average.
if [ "${hdTempMx}" -ge "${maxDriveTemp}" ]; then
echo "${hdTempMx}"
else
echo "${hdTempAv}"
fi
}
# Get average or high ssd temperature.
function ssdTemp {
local ssdNum
local ssdTempCur
local ssdTempAv="0"
local ssdTempMx="0"
local ssdTempMn
local ssdComp="0"
for ssdNum in "${ssdName[@]}"; do
# Get the temp for the current drive.
ssdTempCur="$(smartctl -aj "/dev/${ssdNum}" | jq -Mre '.temperature.current | values')"
# Start adding temps for an average.
ssdTempAv="$(( ssdTempAv + ssdTempCur ))"
# Track non responsive drives
if [ -z "${ssdTempCur}" ]; then
ssdComp="$(( ssdComp + 1 ))"
fi
# Keep track of the lowest current temp
if [ "${ssdTempMn:="${ssdTempCur}"}" -lt "${ssdTempCur}" ]; then
true
else
ssdTempMn="${ssdTempCur}"
fi
# Keep track of the highest current temp
if [ "${ssdTempMx}" -gt "${ssdTempCur}" ]; then
true
else
ssdTempMx="${ssdTempCur}"
fi
done
if [ "$((ssdTempMx - ssdTempMn))" -ge "10" ]; then
ssdComp="$(( ssdComp + 1 ))"
fi
# Divide by number of drives for average.
ssdTempAv="$(bc <<< "scale=3;${ssdTempAv} / (${#ssdName[@]} - ${ssdComp})")"
# If the hottest drive matches/exceeds the max temp use that instead
# of the average.
if [ "${ssdTempMx}" -ge "${maxDriveTemp}" ]; then
echo "${ssdTempMx}"
else
echo "${ssdTempAv}"
fi
}
# Get average or high hba temperature.
function hbaTemp {
local hbaNum
local hbaTempCur
local hbaTempAv="0"
local hbaTempMx="0"
for hbaNum in "${hbaName[@]}"; do
# Get the temp for the current hba.
if [ -c "/dev/${hbaNum}" ]; then
if echo "${hbaNum}" | grep -q "mpr"; then
# See https://gist.github.com/dak180/cd44e9957e1c4180e7eb6eb000716ee2
hbaTempCur="$(${lsi_temp} "/dev/${hbaNum}" | grep 'IOC' | cut -d ' ' -f 3)"
elif echo "${hbaNum}" | grep -q "ses"; then
hbaTempCur="$(sesutil map -u "/dev/${hbaNum}" --libxo:J | jq --arg hbaNum "${hbaNum}" -Mre '.sesutil.enclosures[] | select(.enc == $hbaNum) | .elements[] | select((.type == "Temperature Sensor") and (.status == "OK")) | .extra_status.temperature | values')"
fi
else
hbaTempCur="$(ipmiSens "${hbaNum}")"
fi
# Start adding temps for an average.
hbaTempAv="$(( hbaTempAv + hbaTempCur ))"
# Keep track of the highest current temp
if [ "${hbaTempMx}" -gt "${hbaTempCur}" ]; then
true
else
hbaTempMx="${hbaTempCur}"
fi
done
# Divide by number of hba for average.
hbaTempAv="$(bc <<< "scale=3;${hbaTempAv} / ${#hbaName[@]}")"
# If the hottest drive matches/exceeds the max temp use that instead
# of the average.
if [ "${hbaTempMx}" -ge "${maxHBATemp}" ]; then
echo "${hbaTempMx}"
else
echo "${hbaTempAv}"
fi
}
# Print temp info
function infoTemps {
local hdNum
local hdTempCur
local hdTempAv="0"
local hdComp="0"
local ssdNum
local ssdTempCur
local ssdTempAv="0"
local ssdTempMn
local ssdTempMx="0"
local ssdComp="0"
local hbaNum
local hbaTempCur
local hbaTempAv="0"
if [ ! "${#ssdName[@]}" = "0" ]; then
echo -e "Current SSD setpoint temp:\t$(targetTemp "${targetSSDriveTemp}")°C"
fi
echo -e "Current HD setpoint temp:\t$(targetTemp "${targetHDriveTemp}")°C\n"
if [ ! "${#hbaName[@]}" = "0" ]; then
for hbaNum in "${hbaName[@]}"; do
# Get the temp for the current hba.
if [ -c "/dev/${hbaNum}" ]; then
if echo "${hbaNum}" | grep -q "mpr"; then
# See https://gist.github.com/dak180/cd44e9957e1c4180e7eb6eb000716ee2
hbaTempCur="$(${lsi_temp} "/dev/${hbaNum}" | grep 'IOC' | cut -d ' ' -f 3)"
elif echo "${hbaNum}" | grep -q "ses"; then
hbaTempCur="$(sesutil map -u "/dev/${hbaNum}" --libxo:J | jq --arg hbaNum "${hbaNum}" -Mre '.sesutil.enclosures[] | select(.enc == $hbaNum) | .elements[] | select((.type == "Temperature Sensor") and (.status == "OK")) | .extra_status.temperature | values')"
fi
else
hbaTempCur="$(ipmiSens "${hbaNum}")"
fi
# Start adding temps for an average.
hbaTempAv="$(( hbaTempAv + hbaTempCur ))"
# Echo HBA's current temp
if [ -c "/dev/${hbaNum}" ]; then
echo -e "${hbaNum} Temp:\t${hbaTempCur}°C"
else
echo -e "HBA Temp:\t${hbaTempCur}°C"
fi
done
echo -e " "
# Divide by number of drives for average.
hbaTempAv="$(bc <<< "scale=3;${hbaTempAv} / ${#hbaName[@]}")"
fi
for hdNum in "${hdName[@]}"; do
# Get the temp for the current drive.
hdTempCur="$(smartctl -aj "/dev/${hdNum}" | jq -Mre '.temperature.current | values')"
# Start adding temps for an average.
hdTempAv="$(( hdTempAv + hdTempCur ))"
# Track non responsive drives
if [ -z "${hdTempCur}" ]; then
hdComp="$(( hdComp + 1 ))"
echo -e "${hdNum} Temp:\tN/A"
else
# Echo HD's current temp
echo -e "${hdNum} Temp:\t${hdTempCur}°C"
fi
done
# Divide by number of drives for average.
hdTempAv="$(bc <<< "scale=3;${hdTempAv} / (${#hdName[@]} - ${hdComp})")"
if [ ! "${#ssdName[@]}" = "0" ]; then
for ssdNum in "${ssdName[@]}"; do
# Get the temp for the current drive.
ssdTempCur="$(smartctl -aj "/dev/${ssdNum}" | jq -Mre '.temperature.current | values')"
# Start adding temps for an average.
ssdTempAv="$(( ssdTempAv + ssdTempCur ))"
# Track non responsive drives
if [ -z "${ssdTempCur}" ]; then
ssdComp="$(( ssdComp + 1 ))"
echo -e "${ssdNum} Temp:\tN/A"
else
# Echo SSD's current temp
echo -e "${ssdNum} Temp:\t${ssdTempCur}°C"
fi
# Keep track of the lowest current temp
if [ "${ssdTempMn:="${ssdTempCur}"}" -lt "${ssdTempCur}" ]; then
true
else
ssdTempMn="${ssdTempCur}"
fi
# Keep track of the highest current temp
if [ "${ssdTempMx}" -gt "${ssdTempCur}" ]; then
true
else
ssdTempMx="${ssdTempCur}"
fi
done
if [ "$((ssdTempMx - ssdTempMn))" -ge "10" ]; then
ssdComp="$(( ssdComp + 1 ))"
fi
# Divide by number of drives for average.
ssdTempAv="$(bc <<< "scale=3;${ssdTempAv} / (${#ssdName[@]} - ${ssdComp})")"
fi
echo -e "\nAverage HD Temp: ${hdTempAv}°C"
if [ ! "${#ssdName[@]}" = "0" ]; then
echo -e "Average SSD Temp: ${ssdTempAv}°C"
fi
if [ ! "${#hbaName[@]}" = "0" ]; then
echo -e "HBA Temp Average: ${hbaTempAv}°C"
fi
}
# Print fan info
# shellcheck disable=SC2086
function infoFans {
ipmiRead
local cpuFan
local intakeFan
local outputFan
local countd
for cpuFan in "${!CPU_FAN[@]}"; do
countd=$((cpuFan+1))
if [ "${CPU_FAN[${cpuFan}]}" = "0" ]; then
CPU_FAN[${cpuFan}]="auto"
else
CPU_FAN[${cpuFan}]="${CPU_FAN[${cpuFan}]}%"
fi
echo -e "CPU_FAN${countd}\t${CPU_FAN[${cpuFan}]}\t$(ipmiSens "CPU_FAN${countd}") rpm"
done
for intakeFan in "${!FRNT_FAN[@]}"; do
countd=$((intakeFan+1))
if [ "${FRNT_FAN[${intakeFan}]}" = "0" ]; then
FRNT_FAN[${intakeFan}]="auto"
else
FRNT_FAN[${intakeFan}]="${FRNT_FAN[${intakeFan}]}%"
fi
echo -e "FRNT_FAN${countd}\t${FRNT_FAN[${intakeFan}]}\t$(ipmiSens "FRNT_FAN${countd}") rpm"
done
for outputFan in "${!REAR_FAN[@]}"; do
countd=$((outputFan+1))
if [ "${REAR_FAN[${outputFan}]}" = "0" ]; then
REAR_FAN[${outputFan}]="auto"
else
REAR_FAN[${outputFan}]="${REAR_FAN[${outputFan}]}%"
fi
echo -e "REAR_FAN${countd}\t${REAR_FAN[${outputFan}]}\t$(ipmiSens "REAR_FAN${countd}") rpm"
done
}
# Set fan duty levels
function setFanDuty {
local cpuFan
local hbaFan
local intakeFan
local outputFan
local cpuFanSet
local hbaFanSet
local intakeFanSet
local outputFanSet
cpuFanSet="$(roundR "${1}")"
hbaFanSet="$(roundR "${2}")"
intakeFanSet="$(roundR "${3}")"
ssdFanSet="$(roundR "${4}")"
outputFanSet="$(bc <<< "scale=0;${intakeFanSet} - ${difFanDuty}")"
for cpuFan in "${!CPU_FAN[@]}"; do
CPU_FAN[${cpuFan}]="${cpuFanSet}"
done
for intakeFan in "${!FRNT_FAN[@]}"; do
FRNT_FAN[${intakeFan}]="${intakeFanSet}"
done
for outputFan in "${!REAR_FAN[@]}"; do
REAR_FAN[${outputFan}]="${outputFanSet}"
done
for hbaFan in "${!HBA_FAN[@]}"; do
HBA_FAN[${hbaFan}]="${hbaFanSet}"
done
for ssdFan in "${!SSD_FAN[@]}"; do
SSD_FAN[${ssdFan}]="${ssdFanSet}"
done
}
# The proportional calculation
function proportionalK {
local errorK="${1}"
local controlOuput
controlOuput="$(bc <<< "scale=3;${errorK} * ${Kp}")"
echo "${controlOuput}"
}
# The integral calculation
function integralK {
local errorK="${1}"
local prevIntegralVal="${2}"
local controlStep
local controlOuput
controlStep="$(bc <<< "scale=3;(${errorK} * ${diskCheckTempInterval}) + ${prevIntegralVal}")"
controlOuput="$(bc <<< "scale=3;(${Ki} * ${controlStep}) / 1")"
echo "${controlOuput}"
}
# The derivative calculation
function derivativeK {
local errorK="${1}"
local prevErrorK="${2}"
local controlOuput
controlOuput="$(bc <<< "scale=3;${Kd} * ((${errorK} - ${prevErrorK}) / ${diskCheckTempInterval})")"
echo "${controlOuput}"
}
#
# Main Script Starts Here
#
while getopts ":c:tfd" OPTION; do
case "${OPTION}" in
c)
configFile="${OPTARG}"
;;
t)
scAction="temps"
;;
f)
scAction="fans"
;;
d)
scAction="daemon"
;;
?)
# If an unknown flag is used (or -?):
echo "${0} {-c configFile} {-t|-f|-d}" >&2
exit 1
;;
esac
done
if [ -z "${configFile}" ]; then
echo "Please specify a config file location." >&2
exit 1
elif [ ! -f "${configFile}" ]; then
fcConfig
fi
# Source external config file
# shellcheck source=./FanConfig
. "${configFile}"
# Check if needed software is installed.
PATH="${PATH}:/usr/local/sbin:/usr/local/bin"
commands=(
grep
sed
cut
sleep
bc
smartctl
jq
ipmitool
sysctl
sesutil
)
for command in "${commands[@]}"; do
if ! type "${command}" &> /dev/null; then
echo "${command} is missing, please install" >&2
exit 100
fi
done
# Do not run if the config file has not been edited.
if [ ! "${defaultFile}" = "0" ]; then
echo "Please edit the config file for your setup" >&2
exit 1
fi
# Must be run as root
if [ ! "$(whoami)" = "root" ]; then
echo "Must be run as root." >&2
exit 1
fi
#
# Alternate modes
#
if [ -z "${scAction}" ]; then
echo "Please specify an action." >&2
exit 1
elif [ "${scAction}" = "temps" ]; then
infoTemps
exit 0
elif [ "${scAction}" = "fans" ]; then
infoFans
exit 0
elif [ "${scAction}" = "daemon" ]; then
: # nothing currently needed here
fi
# Set fans to auto on exit
function scExit {
setFanDuty "${autoFanDuty}" "${autoFanDuty}" "${autoFanDuty}" "${autoFanDuty}"
ipmiWrite
exit 0
}
trap "scExit" EXIT
#
# Setup for main loop.
#
# Get current duty levels.
ipmiRead
# Start fans at max
setFanDuty "100" "100" "100" "100"
if ! ipmiWrite; then
exit 1
fi
# Get number of CPUs
numberCPU="$(bc <<< "$(sysctl -n hw.ncpu) - 1")"
# Initialize previous run vars.
: "${prevHDErrorK:="0"}"
: "${prevHDProportionalVal:="0"}"
: "${prevHDIntegralVal:="0"}"
: "${prevHDDerivativeVal:="0"}"
: "${prevHDControlOutput:="100"}"
: "${prevSSDErrorK:="0"}"
: "${prevSSDProportionalVal:="0"}"
: "${prevSSDIntegralVal:="0"}"
: "${prevSSDDerivativeVal:="0"}"
: "${prevSSDControlOutput:="100"}"
: "${prevHBAErrorK:="0"}"
: "${prevHBAProportionalVal:="0"}"
: "${prevHBAIntegralVal:="0"}"
: "${prevHBADerivativeVal:="0"}"
: "${prevHBAControlOutput:="100"}"
#
# Main Loop.
#
while true; do
# Calculate HD fan settings
#
HDsetPoint="$(targetTemp "${targetHDriveTemp}")"
HDprocessVar="$(hdTemp)"
# Get the error.
HDerrorK="$(bc <<< "scale=3;${HDprocessVar} - ${HDsetPoint}")"
# Compute an unqualified control output (P+I+D).
HDproportionalVal="$(proportionalK "${HDerrorK}")"
HDintegralVal="$(integralK "${HDerrorK}" "${prevHDIntegralVal}")"
HDderivativeVal="$(derivativeK "${HDerrorK}" "${prevHDErrorK}")"
unQualHDControlOutput="$(bc <<< "scale=3;${prevHDControlOutput} + ${HDproportionalVal} + ${HDintegralVal} + ${HDderivativeVal}")"
# Qualify the output to ensure we are inside the constraints.
qualHDMinFanDuty="$(bc <<< "${minFanDuty} + ${difFanDuty}")"
qualHDMinFanDuty="$(roundR "${qualHDMinFanDuty}")"
qualHDControlOutput="$(roundR "${unQualHDControlOutput}")"
if [ "${qualHDControlOutput}" -lt "${qualHDMinFanDuty}" ]; then
qualHDControlOutput="${qualHDMinFanDuty}"
elif [ "${qualHDControlOutput}" -gt "${maxFanDuty}" ]; then
qualHDControlOutput="${maxFanDuty}"
fi
if [ ! "${#ssdName[@]}" = "0" ]; then
# Calculate SSD fan settings
#
SSDsetPoint="$(targetTemp "${targetSSDriveTemp}")"
SSDprocessVar="$(ssdTemp)"
# Get the error.
SSDerrorK="$(bc <<< "scale=3;${SSDprocessVar} - ${SSDsetPoint}")"
# Compute an unqualified control output (P+I+D).
SSDproportionalVal="$(proportionalK "${SSDerrorK}")"
SSDintegralVal="$(integralK "${SSDerrorK}" "${prevSSDIntegralVal}")"
SSDderivativeVal="$(derivativeK "${SSDerrorK}" "${prevSSDErrorK}")"
unQualSSDControlOutput="$(bc <<< "scale=3;${prevSSDControlOutput} + ${SSDproportionalVal} + ${SSDintegralVal} + ${SSDderivativeVal}")"
# Qualify the output to ensure we are inside the constraints.
qualSSDMinFanDuty="$(bc <<< "${minFanDuty} + ${difFanDuty}")"
qualSSDMinFanDuty="$(roundR "${qualSSDMinFanDuty}")"
qualSSDControlOutput="$(roundR "${unQualSSDControlOutput}")"
if [ "${qualSSDControlOutput}" -lt "${qualSSDMinFanDuty}" ]; then
qualSSDControlOutput="${qualSSDMinFanDuty}"
elif [ "${qualSSDControlOutput}" -gt "${maxFanDuty}" ]; then
qualSSDControlOutput="${maxFanDuty}"
fi
fi
# Calculate HBA fan settings
#
HBAsetPoint="${targetHBATemp}"
HBAprocessVar="$(hbaTemp)"
# Get the error.
HBAerrorK="$(bc <<< "scale=3;${HBAprocessVar} - ${HBAsetPoint}")"
# Compute an unqualified control output (P+I+D).
HBAproportionalVal="$(proportionalK "${HBAerrorK}")"
HBAintegralVal="$(integralK "${HBAerrorK}" "${prevHBAIntegralVal}")"
HBAderivativeVal="$(derivativeK "${HBAerrorK}" "${prevHBAErrorK}")"
unQualHBAControlOutput="$(bc <<< "scale=3;${prevHBAControlOutput} + ${HBAproportionalVal} + ${HBAintegralVal} + ${HBAderivativeVal}")"
# Qualify the output to ensure we are inside the constraints.
qualHBAMinFanDuty="$(roundR "${minFanDuty}")"
qualHBAControlOutput="$(roundR "${unQualHBAControlOutput}")"
if [ "${qualHBAControlOutput}" -lt "${qualHBAMinFanDuty}" ]; then
qualHBAControlOutput="${qualHBAMinFanDuty}"
elif [ "${qualHBAControlOutput}" -gt "${maxFanDuty}" ]; then
qualHBAControlOutput="${maxFanDuty}"
fi
# We only need to set the fans if something changes.
if [ ! "${prevHDControlOutput}" = "${qualHDControlOutput}" ] || [ ! "${prevHBAControlOutput}" = "${qualHBAControlOutput}" ] || [ ! "${prevSSDControlOutput}" = "${qualSSDControlOutput}" ]; then
# For the case where the same fans are used for SSDs and HDDs
if [ "${mixedFans}" = "1" ]; then
if [ "${qualSSDControlOutput}" -gt "${qualHDControlOutput}" ]; then
qualDiskControlOutput="${qualSSDControlOutput}"
else
qualDiskControlOutput="${qualHDControlOutput}"
fi
fi
# Set the duty levels for each fan type.
if [ "${mixedFans}" = "1" ]; then
setFanDuty "${autoFanDuty}" "${qualHBAControlOutput}" "${qualDiskControlOutput}" "${qualDiskControlOutput}"
else
setFanDuty "${autoFanDuty}" "${qualHBAControlOutput}" "${qualHDControlOutput}" "${qualSSDControlOutput}"
fi
# Write out the new duty levels to ipmi.
# shellcheck disable=SC2086
if ! ipmiWrite; then
exit 1
fi
fi
# Set vars for next run
prevHDErrorK="${HDerrorK}"
prevHDProportionalVal="${HDproportionalVal}"
prevHDIntegralVal="${HDintegralVal}"
prevHDDerivativeVal="${HDderivativeVal}"
prevHDControlOutput="${qualHDControlOutput}"
prevSSDErrorK="${SSDerrorK}"
prevSSDProportionalVal="${SSDproportionalVal}"
prevSSDIntegralVal="${SSDintegralVal}"
prevSSDDerivativeVal="${SSDderivativeVal}"
prevSSDControlOutput="${qualSSDControlOutput}"
prevHBAErrorK="${HBAerrorK}"
prevHBAProportionalVal="${HBAproportionalVal}"
prevHBAIntegralVal="${HBAintegralVal}"
prevHBADerivativeVal="${HBAderivativeVal}"
prevHBAControlOutput="${qualHBAControlOutput}"
sleep "$(bc <<< "scale=0;60 * ${diskCheckTempInterval}" | sed -e 's:.0$::')"
done