Language: EN FR PT ES NL DE SV RU
#!/bin/bash
 
############# CONFIG START ################
 
TEMP_RAW_LOC[0]="/sys/class/hwmon/hwmon0/temp1_input"
TEMP_RAW_DIV[0]="1000"
TEMP_COOL[0]="40"
TEMP_WARM[0]="45"
TEMP_HOT[0]="55"
TEMP_DOWN[0]="74"
TEMP_UP[0]="68"
TEMP_LABEL[0]="Core"
 
TEMP_RAW_LOC[1]="/sys/class/hwmon/hwmon1/device/temp2_input"
TEMP_RAW_DIV[1]="1000"
TEMP_COOL[1]="40"
TEMP_WARM[1]="45"
TEMP_HOT[1]="55"
TEMP_DOWN[1]="58"
TEMP_UP[1]="55"
TEMP_LABEL[1]="CPU"
 
TEMP_RAW_LOC[2]="/sys/class/thermal/thermal_zone0/temp"
TEMP_RAW_DIV[2]="1000"
TEMP_COOL[2]="40"
TEMP_WARM[2]="45"
TEMP_HOT[2]="55"
TEMP_DOWN[2]="50"
TEMP_UP[2]="45"
TEMP_LABEL[2]="System"
 
#FAN_RAW_LOC[0]="/sys/class/hwmon/hwmon1/device/fan1_input"
#FAN_LOW[0]="5000"
#FAN_HIGH[0]="6800"
#FAN_LABEL[0]="CPU"
 
#FAN_RAW_LOC[1]="/sys/class/hwmon/hwmon1/device/fan2_input"
#FAN_LOW[1]="1000"
#FAN_HIGH[1]="2400"
#FAN_LABEL[1]="Case"
 
SLEEP_TIME=5
 
FREQ_LOW="1000"
FREQ_MID="1200"
FREQ_HIGH="1400"
 
FREQ_RAW_DIV="1000"
 
############### CONFIG END ################
 
core="0"
 
# Get the raw temp, then divide it by value set in config, then colorize it.
function GetRealTemp() {
	TEMP_RAW[$i]=`cat ${TEMP_RAW_LOC[$i]}`
	TEMP_REAL[$i]=$((${TEMP_RAW[$i]}/${TEMP_RAW_DIV[$i]}))
	ColorizeTemp
}
 
# Get the raw freq, then divide it by value set in config, then colorize it.
function GetRealFreq() {
	FREQ_RAW[$c]=`cat ${FREQ_RAW_LOC[$c]}`
	FREQ_REAL[$c]=$((${FREQ_RAW[$c]}/${FREQ_RAW_DIV}))
	ColorizeFreq
}
 
# Get the governor and then colorize it.
function GetGovernor() {
	FREQ_GOV[$c]=`cat /sys/devices/system/cpu/cpu${c}/cpufreq/scaling_governor`
	ColorizeGov
}
 
# Get the fan speed and then colorize it.
function GetFanSpeed() {
	FAN_SPEED[$f]=`cat ${FAN_RAW_LOC[$f]}`
	ColorizeFan
}
 
# Get the current cooling state.
function GetCoolingState() {
	THERM_STATE[$c]=`cat /sys/devices/virtual/thermal/cooling_device${c}/cur_state`
	ColorizeState
}
 
function GetSpeed() {
	if [ ${TEMP_REAL[$i]} -le ${TEMP_UP[$i]} ] ;
	then
		SPEED=0
		return ;
	fi
	if [ ${TEMP_REAL[$i]} -le ${TEMP_DOWN[$i]} -a ${TEMP_REAL[$i]} -gt ${TEMP_UP[$i]} ] ;
	then
		SPEED=1
		return ;
	fi
	if [ ${TEMP_REAL[$i]} -gt ${TEMP_DOWN[$i]} ] ;
	then
		SPEED=2
		return ;
	fi
}
 
function CheckSpeed() {
	if [ ${SPEED} == 0 -a ${THERM_STATE[$core]} != 0 ] ;
	then
		NEW_STATE=$((${THERM_STATE[$core]}-1))
		echo ${NEW_STATE} > /sys/devices/virtual/thermal/cooling_device${core}/cur_state
		if [ ${core} != 0 -a ${NEW_STATE} == 0 ] ;
		then
			core=$((${core}-1)) ;
		fi
		return ;
	fi
	if [ ${SPEED} == 2 -a ${THERM_STATE[$core]} != 3 ] ;
	then
		NEW_STATE=$((${THERM_STATE[$core]}+1))
		if [ ${NEW_STATE} == 3 -a ${core} != 3 ] ;
		then
			core=$((${core}+1))
			NEW_STATE="1" ;
		fi
		echo ${NEW_STATE} > /sys/devices/virtual/thermal/cooling_device${core}/cur_state
		return ;
	fi
}
 
# Function for colorizing a string.
function ColorizeStr() {
 
	# Case statement to set ascii color code from named color
	case "$1" in
		"blue" )
			color="\e[1;34m"
			;;
		"red" )
			color="\e[1;31m"
			;;
		"yellow" )
			color="\e[1;33m"
			;;
		"green" )
			color="\e[1;32m"
			;;
	esac
 
	# Do the actual echo, note the -e to use the escape codes
	# and notice the color reset at the end, always use that.
	CL_STR="${color}${2}\e[0m"
 
}
 
# Function to colorize the cooling state value.
function ColorizeState() {
	case ${THERM_STATE[$c]} in 
		"0" )
			ColorizeStr "green" ${THERM_STATE[$c]}
			STATE_STR[$c]=${CL_STR}
		;;
		"1" )
			ColorizeStr "yellow" ${THERM_STATE[$c]}
			STATE_STR[$c]=${CL_STR}
		;;
		"2" )
			ColorizeStr "red" ${THERM_STATE[$c]}
			STATE_STR[$c]=${CL_STR}
		;;
	esac
}
 
# Function to colorize the governor value.
function ColorizeGov() {
	case ${FREQ_GOV[$c]} in 
		"ondemand" )
			ColorizeStr "green" ${FREQ_GOV[$c]}
			GOV_STR[$c]=${CL_STR}
		;;
		"performance" )
			ColorizeStr "red" ${FREQ_GOV[$c]}
			GOV_STR[$c]=${CL_STR}
		;;
		"powersave" )
			ColorizeStr "blue" ${FREQ_GOV[$c]}
			GOV_STR[$c]=${CL_STR}
		;;
		"userspace" )
			ColorizeStr "yellow" ${FREQ_GOV[$c]}
			GOV_STR[$c]=${CL_STR}
		;;
	esac
}
 
# Function to colorize the temp.
function ColorizeTemp() {
	if [ ${TEMP_REAL[$i]} -le ${TEMP_COOL[$i]} ] ;
	then
		ColorizeStr "blue" ${TEMP_REAL[$i]}
		TEMP_STR[$i]=${CL_STR}
		return ;
	fi
	if [ ${TEMP_REAL[$i]} -gt ${TEMP_COOL[$i]} -a ${TEMP_REAL[$i]} -le ${TEMP_WARM[$i]} ] ;
	then
		ColorizeStr "green" ${TEMP_REAL[$i]}
		TEMP_STR[$i]=${CL_STR}
		return ;
	fi
	if [ ${TEMP_REAL[$i]} -gt ${TEMP_WARM[$i]} -a ${TEMP_REAL[$i]} -le ${TEMP_HOT[$i]} ] ;
	then
		ColorizeStr "yellow" ${TEMP_REAL[$i]}
		TEMP_STR[$i]=${CL_STR}
		return ;
	fi
	if [ ${TEMP_REAL[$i]} -gt ${TEMP_HOT[$i]} ] ;
	then
		ColorizeStr "red" ${TEMP_REAL[$i]}
		TEMP_STR[$i]=${CL_STR}
		return ;
	fi
}
 
# Function to colorize the temp.
function ColorizeFan() {
	if [ ${FAN_SPEED[$f]} -le ${FAN_LOW[$f]} ] ;
	then
		ColorizeStr "red" ${FAN_SPEED[$f]}
		FAN_STR[$f]=${CL_STR}
		return ;
	fi
	if [ ${FAN_SPEED[$f]} -le ${FAN_HIGH[$f]} -a ${FAN_SPEED[$f]} -gt ${FAN_LOW[$f]} ] ;
	then
		ColorizeStr "green" ${FAN_SPEED[$f]}
		FAN_STR[$f]=${CL_STR}
		return ;
	fi
	if [ ${FAN_SPEED[$f]} -gt ${FAN_HIGH[$f]} ] ;
	then
		ColorizeStr "red" ${FAN_SPEED[$f]}
		FAN_STR[$f]=${CL_STR}
		return ;
	fi
}
 
# Function to colorize the frequency.
function ColorizeFreq() {
	if [ ${FREQ_REAL[$c]} -le ${FREQ_LOW} ] ;
	then
		ColorizeStr "blue" ${FREQ_REAL[$c]}
		FREQ_STR[$c]=${CL_STR}
		return ;
	fi
	if [ ${FREQ_REAL[$c]} -gt ${FREQ_LOW} -a ${FREQ_REAL[$c]} -le ${FREQ_MID} ] ;
	then
		ColorizeStr "green" ${FREQ_REAL[$c]}
		FREQ_STR[$c]=${CL_STR}
		return ;
	fi
	if [ ${FREQ_REAL[$c]} -gt ${FREQ_MID} -a ${FREQ_REAL[$c]} -le ${FREQ_HIGH} ] ;
	then
		ColorizeStr "yellow" ${FREQ_REAL[$c]}
		FREQ_STR[$c]=${CL_STR}
		return ;
	fi
	if [ ${FREQ_REAL[$c]} -gt ${FREQ_HIGH} ] ;
	then
		ColorizeStr "red" ${FREQ_REAL[$c]}
		FREQ_STR[$c]=${CL_STR}
		return ;
	fi
}
 
# Start of program loop.
while [ TRUE ]
do
 
	# Loop for gathering temp sensor information.
	for i in {0..3} ;
	do 
		if [ -z ${TEMP_RAW_LOC[$i]} ] ;
		then
			break ;
		fi
		GetRealTemp
		if [ ${TEMP_LABEL[$i]} == "Core" ] ;
		then
			GetSpeed ;
		fi
		TEMP_OUT[$i]="\e[1;37m ${TEMP_LABEL[$i]}: ${TEMP_STR[$i]}\e[1;37mc\e[0m"
	done
 
	# Loop for gathering fan information.
	for f in {0..3} ;
	do 
		if [ -z ${FAN_RAW_LOC[$f]} ] ;
		then
			break ;
		fi
		GetFanSpeed
		FAN_OUT[$f]="\e[1;37m ${FAN_LABEL[$f]}: ${FAN_STR[$f]}\e[1;37m RPMS\e[0m"
	done
 
	# Loop for gathering cpufreq information.
	for c in {0..6} ;
	do
		FREQ_RAW_LOC[$c]="/sys/devices/system/cpu/cpu${c}/cpufreq/scaling_cur_freq"
		if [ ! -e ${FREQ_RAW_LOC[$c]} ] ;
		then
			break ;
		fi
		GetGovernor
		GetRealFreq
		GetCoolingState
		FREQ_OUT[$c]="\e[1;37m Core\e[0m: \e[1;32m${c}\e[1;37m\n Governor \e[0m : ${GOV_STR[$c]}\n\e[1;37m Frequency\e[0m : ${FREQ_STR[$c]} \e[1;37mMghz\n CoolState\e[0m : ${STATE_STR[$c]}"
	done
 
	CheckSpeed
 
	### Start output
 
	# Loop for temp output.
	for OUT in "${TEMP_OUT[@]}" ;
	do
		echo -e ${OUT}
	done
	for OUT in "${FAN_OUT[@]}" ;
	do
		echo -e ${OUT}
	done
 
	# Spacer.
	echo "<------------------------->"
 
	# Loop for cpufreq output.
	for OUT in "${FREQ_OUT[@]}" ;
	do
		echo -e ${OUT}
		echo "<------------------------->"
	done
 
	# Sleep for time set in config.
	sleep $SLEEP_TIME
done