debugged and reworked all sensors code

This commit is contained in:
inxi-svn 2009-07-29 20:20:41 +00:00
parent 9b532f4aba
commit fcf71c9f55

345
inxi
View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
######################################################################## ########################################################################
#### Script Name: inxi #### Script Name: inxi
#### version: 1.0.108-b1 #### version: 1.1.1-b1
#### Date: 28 July 2009 #### Date: 29 July 2009
######################################################################## ########################################################################
#### SPECIAL THANKS #### SPECIAL THANKS
######################################################################## ########################################################################
@ -3012,27 +3012,32 @@ get_sensors_data()
{ {
eval $LOGFS eval $LOGFS
IFS="," IFS=$'\n'
if [[ $B_SENSORS == 'true' ]];then if [[ $B_SENSORS == 'true' ]];then
# note: non-configured sensors gives error message, which we need to redirect to stdout # note: non-configured sensors gives error message, which we need to redirect to stdout
# also, -F ':' no space, since some cases have the data starting right after :
A_SENSORS_DATA=( $( A_SENSORS_DATA=( $(
sensors | gawk -v userCpuNo="$SENSORS_CPU_NO" ' sensors | gawk -F ': ' -v userCpuNo="$SENSORS_CPU_NO" '
BEGIN { BEGIN {
IGNORECASE=1 IGNORECASE=1
moboTemp=""
cpuTemp=""
core0Temp="" # only if all else fails... core0Temp="" # only if all else fails...
moboTempReal="" cpuTemp=""
cpuTempReal="" cpuTempReal=""
tempUnit="" fanWorking=""
temp1="" indexCountaFanMain=0
temp2="" indexCountaFanDefault=0
indexCountaFanReal=0
indexCountaFanWorking=0
i="" i=""
j="" j=""
moboTemp=""
moboTempReal=""
psuTemp=""
sysFanString="" sysFanString=""
temp1=""
temp2=""
tempFanType="" # set to 1 or 2 tempFanType="" # set to 1 or 2
tempUnit=""
tempWorking=""
tempWorkingUnit=""
} }
# dumping the extra + signs, nobody has negative temps # dumping the extra + signs, nobody has negative temps
# also, note gawk treats ° as a space, so we have to get the C/F data # also, note gawk treats ° as a space, so we have to get the C/F data
@ -3040,108 +3045,91 @@ get_sensors_data()
# different systems before this can be trusted much. Note that there # different systems before this can be trusted much. Note that there
# is a hack here to handle cases where search term has 1 or 2 words with space # is a hack here to handle cases where search term has 1 or 2 words with space
# note: using arrays starting at 1 for all fan arrays to make it easier overall # note: using arrays starting at 1 for all fan arrays to make it easier overall
/^(M\/B|MB|SYS) Temp/ { /^(M\/B|MB|SYS)(.*)\+([0-9]+)(.*)[ \t°](C|F)/ && $2 ~ /^[ \t]*\+([0-9]+)/ {
moboTemp=gensub( /\+(.+)/, "\\1", 1, $3 ) moboTemp=gensub( /[ \t]+\+([0-9\.]*)(.*)/, "\\1", 1, $2 )
if ( $4 ~ /C|F/ && tempUnit == "" ){ tempWorkingUnit=gensub( /[ \t]+\+([0-9\.]+)[ \t°]+([CF])(.*)/, "\\2", 1, $2 )
tempUnit="°" $4 if ( tempWorkingUnit ~ /^C|F$/ && tempUnit == "" ){
tempUnit="°" tempWorkingUnit
} }
} }
/^CPU Temp/ { /^CPU(.*)\+([0-9]+)(.*)[ \t°](C|F)/ && $2 ~ /^[ \t]*\+([0-9]+)/ {
cpuTemp=gensub( /\+(.+)/, "\\1", 1, $3 ) cpuTemp=gensub( /[ \t]+\+([0-9\.]+)(.*)/, "\\1", 1, $2 )
if ( $4 ~ /C|F/ && tempUnit == "" ){ tempWorkingUnit=gensub( /[ \t]+\+([0-9\.]+)[ \t°]+([CF])(.*)/, "\\2", 1, $2 )
tempUnit="°" $4 if ( tempWorkingUnit ~ /^C|F$/ && tempUnit == "" ){
tempUnit="°" tempWorkingUnit
} }
} }
/^temp1:/ { /^(P\/S|Power)(.*)\+([0-9]+)(.*)[ \t°](C|F)/ && $2 ~ /^[ \t]*\+([0-9]+)/ {
tempWorking=gensub( /\+(.+)/, "\\1", 1, $2 ) psuTemp=gensub( /[ \t]+\+([0-9\.]+)(.*)/, "\\1", 1, $2 )
tempWorkingUnit=gensub( /[ \t]+\+([0-9\.]+)[ \t°]+([CF])(.*)/, "\\2", 1, $2 )
if ( tempWorkingUnit ~ /^C|F$/ && tempUnit == "" ){
tempUnit="°" tempWorkingUnit
}
}
$1 ~ /^temp1$/ && $2 ~ /^[ \t]*\+([0-9]+)/ {
tempWorking=gensub( /[ \t]+\+([0-9\.]+)(.*)/, "\\1", 1, $2 )
if ( temp1 == "" || tempWorking > 0 ) { if ( temp1 == "" || tempWorking > 0 ) {
temp1=tempWorking temp1=tempWorking
} }
if ( $3 ~ /C|F/ && tempUnit == "" ){ tempWorkingUnit=gensub( /[ \t]+\+([0-9\.]+)[ \t°]+([CF])(.*)/, "\\2", 1, $2 )
tempUnit="°" $3 if ( tempWorkingUnit ~ /^C|F$/ && tempUnit == "" ){
tempUnit="°" tempWorkingUnit
} }
} }
/^temp2:/ { $1 ~ /^temp2$/ && $2 ~ /^[ \t]*\+([0-9]+)/ {
# add to array if array index does not exist OR if number is > 0 tempWorking=gensub( /[ \t]+\+([0-9\.]+)(.*)/, "\\1", 1, $2 )
tempWorking=gensub( /\+(.+)/, "\\1", 1, $2 )
if ( temp2 == "" || tempWorking > 0 ) { if ( temp2 == "" || tempWorking > 0 ) {
temp2=tempWorking temp2=tempWorking
} }
if ( $3 ~ /C|F/ && tempUnit == "" ){ tempWorkingUnit=gensub( /[ \t]+\+([0-9\.]+)[ \t°]+([CF])(.*)/, "\\2", 1, $2 )
tempUnit="°" $3 if ( tempWorkingUnit ~ /^C|F$/ && tempUnit == "" ){
} tempUnit="°" tempWorkingUnit
}
# final fallback if all else fails
/^core0:|core 0:|core0 Temp/ {
if ( $2 ~ /(.+):/ ){
tempWorking=$3
}
else {
tempWorking=$2
}
tempWorking=gensub( /\+(.+)/, "\\1", 1, tempWorking )
if ( core0Temp == "" || tempWorking > 0 ) {
core0Temp=tempWorking
}
if ( $3 ~ /C|F/ && tempUnit == "" ){
tempUnit="°" $3
}
else if ( $4 ~ /C|F/ && tempUnit == "" ){
tempUnit="°" $4
} }
} }
# note: can be cpu fan:, cpu fan speed:, etc # final fallback if all else fails, funtoo user showed sensors putting
/^CPU Fan(.*):/ { # temp on wrapped second line, not handled
if ( $3 ~ /(.+):/ ){ /^(core0|core 0)(.*)\+([0-9]+)(.*)[ \t°](C|F)/ {
aFanReal[1]=$4 tempWorking=gensub( /[ \t]+\+([0-9\.]+)(.*)/, "\\1", 1, $2 )
if ( core0Temp == "" || tempWorking > 0 ) {
core0Temp=tempWorking
} }
else { tempWorkingUnit=gensub( /[ \t]+\+([0-9\.]+)[ \t°]+([CF])(.*)/, "\\2", 1, $2 )
aFanReal[1]=$3 if ( tempWorkingUnit ~ /^C|F$/ && tempUnit == "" ){
tempUnit="°" tempWorkingUnit
} }
} }
/^(M\/B|MB|SYS) Fan(.*):/ {
if ( $3 ~ /(.+):/ ){ # note: can be cpu fan:, cpu fan speed:, etc. Some cases have no space before
aFanReal[2]=$4 # $2 starts, so skip that space test in regex
} /^CPU(.*)[ \t]*([0-9]+)[ \t]RPM/ {
else { aFanMain[1]=gensub( /[ \t]*([0-9]+)[ \t]+(.*)/, "\\1", 1, $2 )
aFanReal[2]=$3
}
} }
/Power_FAN Speed:|P\/S Fan:/ { /^(M\/B|MB|SYS)(.*)[ \t]*([0-9]+)[ \t]RPM/ {
aFanReal[3]=$3 aFanMain[2]=gensub( /[ \t]*([0-9]+)[ \t]+(.*)/, "\\1", 1, $2 )
} }
/POWER FAN Speed:/ { /(Power|P\/S|POWER)(.*)[ \t]*([0-9]+)[ \t]RPM/ {
aFanReal[3]=$4 aFanMain[3]=gensub( /[ \t]*([0-9]+)[ \t]+(.*)/, "\\1", 1, $2 )
} }
/^FAN1 Speed:|CHASSIS(1)? FAN Speed:/ {
if ( $2 == "Speed:" ) { /^(FAN(1)? |CHASSIS(1)?)[ \t](.*)[ \t]*([0-9]+)[ \t]RPM/ {
aFanReal[4]=$3 aFanMain[4]=gensub( /[ \t]*([0-9]+)[ \t]+(.*)/, "\\1", 1, $2 )
}
else if ( $3 == "Speed:" ) {
aFanReal[4]=$4
}
} }
/^FAN([2-9]) Speed:|CHASSIS([2-9]) FAN Speed:/ { /^FAN([2-9]) |CHASSIS([2-9] )(.*)[ \t]*([0-9]+)[ \t]RPM/ {
sysFanNu=gensub( /(FAN|CHASSIS)([2-9])( FAN)? Speed:/, "\\2", 1, $0 ) sysFanNu=gensub( /^(FAN|CHASSIS)([2-9])[ \t]+(.*)/, "\\2", 1, $1 )
if ( sysFanNu ~ /([2-9])/ ) { if ( sysFanNu ~ /^([2-9])$/ ) {
# note: cpu/mobo/ps/fan1 above are 1/2/3/4 # note: cpu/mobo/ps/fan1 above are 1/2/3/4
sysFanNu = sysFanNu + 4 sysFanNu = sysFanNu + 4
if ( $2 == "Speed:" ) { aFanMain[sysFanNu]=gensub( /[ \t]*([0-9]+)[ \t]+(.*)/, "\\1", 1, $2 )
aFanReal[sysFanNu]=$3
}
else if ( $3 == "Speed:" ) {
aFanReal[sysFanNu]=$4
}
} }
} }
/^fan([0-9]+):/ { /^fan([0-9]+)(.*)[ \t]*([0-9]+)[ \t]RPM/ {
sysFanNu=gensub( /fan([0-9]+):/, "\\1", 1, $1 ) fanWorking=gensub( /[ \t]*([0-9]+)[ \t]+(.*)/, "\\1", 1, $2 )
if ( sysFanNu ~ /([0-9]+)/ ) { sysFanNu=gensub( /fan([0-9]+)/, "\\1", 1, $1 )
if ( sysFanNu ~ /^([0-9]+)$/ ) {
# add to array if array index does not exist OR if number is > existing number # add to array if array index does not exist OR if number is > existing number
if ( ! sysFanNu in aFanWorking || $2 >= aFanWorking[sysFanNu] ) { if ( ! sysFanNu in aFanDefault || fanWorking >= aFanDefault[sysFanNu] ) {
aFanWorking[sysFanNu]=$2 aFanDefault[sysFanNu]=fanWorking
} }
} }
} }
@ -3203,51 +3191,61 @@ get_sensors_data()
moboTempReal=temp2 moboTempReal=temp2
} }
# then set the cpu fan speed # then set the cpu fan speed
if ( aFanReal[1] == "" ) { if ( aFanMain[1] == "" ) {
# note, you cannot test for aFanWorking[1] or [2] != "" # note, you cannot test for aFanDefault[1] or [2] != ""
# because that creates an array item in gawk just by the test itself # because that creates an array item in gawk just by the test itself
if ( tempFanType == 1 && 1 in aFanWorking ) { if ( tempFanType == 1 && 1 in aFanDefault ) {
aFanReal[1]=aFanWorking[1] aFanMain[1]=aFanDefault[1]
} }
else if ( tempFanType == 2 && 2 in aFanWorking ) { else if ( tempFanType == 2 && 2 in aFanDefault ) {
aFanReal[1]=aFanWorking[2] aFanMain[1]=aFanDefault[2]
} }
} }
# then set mobo fan if missing. Note, not trying to guess on either of these # then set mobo fan if missing. Note, not trying to guess on either of these
if ( ! 2 in aFanReal ) { # if ( ! 2 in aFanMain ) {
aFanReal[2] = "" # aFanMain[2] = ""
} # }
# then set psu fan if missing # # then set psu fan if missing
if ( ! 3 in aFanReal ) { # if ( ! 3 in aFanMain ) {
aFanReal[3] = "" # aFanMain[3] = ""
} # }
# then we need to get the actual numeric max array count for both fan arrays # then we need to get the actual numeric max array count for both fan arrays
for (i = 0; i <= 14; i++) { for (i = 0; i <= 14; i++) {
if ( i in aFanReal && i > indexCountaFanReal ) { if ( i in aFanMain && i > indexCountaFanMain ) {
indexCountaFanReal=i indexCountaFanMain=i
} }
} }
for (i = 0; i <= 14; i++) { for (i = 0; i <= 14; i++) {
if ( i in aFanWorking && i > indexCountaFanWorking ) { if ( i in aFanDefault && i > indexCountaFanDefault ) {
indexCountaFanWorking=i indexCountaFanDefault=i
} }
} }
# clear out any duplicates. Primary fan real trumps fan working always if same speed # clear out any duplicates. Primary fan real trumps fan working always if same speed
for (i = 1; i <= indexCountaFanReal; i++) { for (i = 1; i <= indexCountaFanMain; i++) {
if ( i in aFanReal && aFanReal[i] != "" && aFanReal[i] != 0 ) { if ( i in aFanMain && aFanMain[i] != "" && aFanMain[i] != 0 ) {
for (j = 1; j <= indexCountaFanWorking; j++) { for (j = 1; j <= indexCountaFanDefault; j++) {
if ( j in aFanWorking && aFanReal[i] == aFanWorking[j] ) { if ( j in aFanDefault && aFanMain[i] == aFanDefault[j] ) {
aFanWorking[j] = "" aFanDefault[j] = ""
} }
} }
} }
} }
# then construct the sys_fan string for echo # then construct the sys_fan string for echo
for (j = 1; j <= indexCountaFanWorking; j++) { for (j = 1; j <= indexCountaFanDefault; j++) {
sysFanString = sysFanString "," aFanWorking[j] fanDefaultString = fanDefaultString separator aFanDefault[j]
separator=","
}
separator="" # reset to null for next loop
# then construct the sys_fan string for echo
for (j = 1; j <= indexCountaFanMain; j++) {
fanMainString = fanMainString separator aFanMain[j]
separator=","
} }
# and then build the temps: # and then build the temps:
@ -3259,12 +3257,14 @@ get_sensors_data()
} }
# if they are ALL null, print error message. psFan is not used in output currently # if they are ALL null, print error message. psFan is not used in output currently
if ( cpuTempReal == "" && moboTempReal == "" && aFanReal[1] == "" && aFanReal[2] == "" && aFanReal[3] == "" && sysFanString == "" ) { if ( cpuTempReal == "" && moboTempReal == "" && aFanMain[1] == "" && aFanMain[2] == "" && aFanMain[3] == "" && fanDefaultString == "" ) {
print "No active sensors found. Have you configured your sensors yet?" print "No active sensors found. Have you configured your sensors yet?"
} }
else { else {
# then build it: afanreals: 1 - cpu; 2 - mobo; 3 - psu # then build array arrays:
print cpuTempReal "," moboTempReal "," aFanReal[1] "," aFanReal[2] "," aFanReal[3] sysFanString print cpuTempReal "," moboTempReal "," psuTemp
print fanMainString
print fanDefaultString
} }
} }
' '
@ -4134,8 +4134,9 @@ print_partition_data()
print_sensors_data() print_sensors_data()
{ {
eval $LOGFS eval $LOGFS
local mobo_temp='' cpu_temp='' cpu_fan='' mobo_fan='' ps_fan='' sys_fans='' sys_fans2='' local mobo_temp='' cpu_temp='' psu_temp='' cpu_fan='' mobo_fan='' ps_fan='' sys_fans='' sys_fans2=''
local temp_data='' fan_data='' fan_data2='' b_is_error='false' fan_count=0 local temp_data='' fan_data='' fan_data2='' b_is_error='false' fan_count=0
local a_sensors_working=''
get_sensors_data get_sensors_data
# initial error cases, for missing app or unconfigured sensors # initial error cases, for missing app or unconfigured sensors
@ -4145,58 +4146,92 @@ print_sensors_data()
else else
for (( i=0; i < ${#A_SENSORS_DATA[@]}; i++ )) for (( i=0; i < ${#A_SENSORS_DATA[@]}; i++ ))
do do
IFS=","
a_sensors_working=( ${A_SENSORS_DATA[i]} )
IFS="$ORIGINAL_IFS"
case $i in case $i in
# first the temp data
0) 0)
if [[ -n ${A_SENSORS_DATA[0]} ]];then if [[ -n ${a_sensors_working[0]} ]];then
cpu_temp=${A_SENSORS_DATA[0]} cpu_temp=${a_sensors_working[0]}
else else
cpu_temp='N/A' cpu_temp='N/A'
fi fi
cpu_temp="${C1}System Temperatures: cpu:${C2} $cpu_temp " cpu_temp="${C1}System Temperatures: cpu:${C2} $cpu_temp "
;;
1) if [[ -n ${a_sensors_working[1]} ]];then
if [[ -n ${A_SENSORS_DATA[1]} ]];then mobo_temp=${a_sensors_working[1]}
mobo_temp=${A_SENSORS_DATA[1]}
else else
mobo_temp='N/A' mobo_temp='N/A'
fi fi
mobo_temp="${C1}mobo:${C2} $mobo_temp " mobo_temp="${C1}mobo:${C2} $mobo_temp "
if [[ -n ${a_sensors_working[2]} ]];then
psu_temp="${C1}psu:${C2} ${a_sensors_working[2]} "
fi
;;
# then the fan data from main fan array
1)
for (( j=0; j < ${#a_sensors_working[@]}; j++ ))
do
case $j in
0)
# we need to make sure it's either cpu fan OR cpu fan and sys fan 1
if [[ -n ${a_sensors_working[0]} ]];then
cpu_fan="${a_sensors_working[0]}"
elif [[ -z ${a_sensors_working[0]} && -n ${a_sensors_working[1]} ]];then
cpu_fan="${a_sensors_working[1]}"
else
cpu_fan='N/A'
fi
cpu_fan="${C1}Fan Speeds (in rpm): cpu:${C2} $cpu_fan "
(( fan_count++ ))
;;
1)
if [[ -n ${a_sensors_working[1]} ]];then
mobo_fan="${C1}mobo:${C2} ${a_sensors_working[1]} "
(( fan_count++ ))
fi
;;
2)
if [[ -n ${a_sensors_working[2]} ]];then
ps_fan="${C1}ps:${C2} ${a_sensors_working[2]} "
(( fan_count++ ))
fi
;;
[3-9])
if [[ -n ${a_sensors_working[$j]} ]];then
fan_number=$(( $j - 2 )) # sys fans start on array key 5
# wrap after fan 6 total
if [[ $fan_count -lt 7 ]];then
sys_fans="$sys_fans${C1}sys-$fan_number:${C2} ${a_sensors_working[$j]} "
else
sys_fans2="$sys_fans2${C1}sys-$fan_number:${C2} ${a_sensors_working[$j]} "
fi
(( fan_count++ ))
fi
;;
esac
done
;; ;;
2) 2)
# we need to make sure it's either cpu fan OR cpu fan and sys fan 1 for (( j=0; j < ${#a_sensors_working[@]}; j++ ))
if [[ -n ${A_SENSORS_DATA[2]} ]];then do
cpu_fan="${A_SENSORS_DATA[2]}" case $j in
elif [[ -z ${A_SENSORS_DATA[2]} && -n ${A_SENSORS_DATA[3]} ]];then [0-9]|10|11|12|13|14)
cpu_fan="${A_SENSORS_DATA[3]}" if [[ -n ${a_sensors_working[$j]} ]];then
else fan_number=$(( $j + 1 )) # sys fans start on array key 5
cpu_fan='N/A' # wrap after fan 6 total
fi if [[ $fan_count -lt 7 ]];then
cpu_fan="${C1}Fan Speeds (in rpm): cpu:${C2} $cpu_fan " sys_fans="$sys_fans${C1}fan-$fan_number:${C2} ${a_sensors_working[$j]} "
(( fan_count++ )) else
;; sys_fans2="$sys_fans2${C1}fan-$fan_number:${C2} ${a_sensors_working[$j]} "
3) fi
if [[ -n ${A_SENSORS_DATA[3]} ]];then (( fan_count++ ))
mobo_fan="${C1}mobo:${C2} ${A_SENSORS_DATA[3]} " fi
(( fan_count++ )) ;;
fi esac
;; done
4)
if [[ -n ${A_SENSORS_DATA[4]} ]];then
ps_fan="${C1}ps:${C2} ${A_SENSORS_DATA[4]} "
(( fan_count++ ))
fi
;;
[5-9]|10|11|12|13|14)
if [[ -n ${A_SENSORS_DATA[$i]} ]];then
fan_number=$(( $i - 4 )) # sys fans start on array key 5
# wrap after fan 6 total
if [[ $fan_count -lt 7 ]];then
sys_fans="$sys_fans${C1}sys-$fan_number:${C2} ${A_SENSORS_DATA[$i]} "
else
sys_fans2="$sys_fans2${C1}sys-$fan_number:${C2} ${A_SENSORS_DATA[$i]} "
fi
(( fan_count++ ))
fi
;; ;;
esac esac
done done