#!/bin/sh
# http://www.berklix.com/~jhs/bin/.sh/temp
# To be run when I'm worrying about eg:
#	how powerd is regulating cpu temp
#	or if cpu fan is intermittently jamming.
#	if host will crash
#	why host fire was stinking
#	Tue Sep  6 10:03:41 CEST 2022
#	        dev.cpu.0.temperature: 128.0C
#	        dev.amdtemp.0.sensor0.core0: 128.0C
#	        dev.amdtemp.0.sensor1.core0: 128.0C
# See also /usr/local/etc/rc.d/mbmon
# See also man  mbmon
# See also man xmbmon

# Idle:
#  Fire	2022-09-06T14:41:50Z: Sysctl 47.0C 48.0C 48.0C Mbmon 34.0, 25.0; Fans 3183, 2163
#  Fire halted in BIOS : ................................... 38 ............. CPU   Case

#  Lapr 2022-09-06T14:42:27Z: 64.0C 64.0C 65.0C 64.0C Freq 933 Bat 100
#
# I halted host=Fire to look in BIOS to see if it has an auto off at eg 90 C,
# it does not, it has the 2 options: CPU & Case turned on for "Warning if fan
# stops", but that is presumably just some warning PC beep not a hard Off.
#
# host=fire 9.2-RELEASE shows nothing for sysctl -a | grep -i temp | grep -i max
# but lapr 12.3-RELEASE shows dev.cpu.[0-3].coretemp.tjmax: 90.0C
# At least BSD there might itself decide to turn itself off if laptop overheats.
#
# Amazing host=fire did not die at 127 C.  OTOH 127 sounds like a
# fake report limit But CPU heat sink was too hot to touch for long
# after I turned off.

# 2022-12-04 fire: I turned on, cpu fan did not start, didnt notice, hadnt turned on
# external fan either, smell emitted, fan on, started temp, it started dropping
# from high, reporting:
#  CPU 102.0C, Sens0 102.0C, Sens1 103.0C, Board Chip 79.0, 25.0; Fans CPU, Case    0, 2191
#  CPU 101.0C, Sens0 101.0C, Sens1 102.0C, Board Chip 79.0, 25.0; Fans CPU, Case    0, 2163
#  CPU 101.0C, Sens0 100.0C, Sens1 101.0C, Board Chip 79.0, 25.0; Fans CPU, Case    0, 2163

# Locked up. Cold reboot. Both fans on:
#  CPU 44.0C, Sens0 44.0C, Sens1 44.0C, Board Chip 25.0, 25.0; Fans CPU, Case 3125, 2163
#  CPU 45.0C, Sens0 45.0C, Sens1 44.0C, Board Chip 26.0, 25.0; Fans CPU, Case 3245, 2163
#  CPU 42.0C, Sens0 43.0C, Sens1 43.0C, Board Chip 26.0, 25.0; Fans CPU, Case 3245, 2136
#  CPU 43.0C, Sens0 42.0C, Sens1 43.0C, Board Chip 26.0, 25.0; Fans CPU, Case 3245, 2136
#  CPU 44.0C, Sens0 43.0C, Sens1 43.0C, Board Chip 26.0, 25.0; Fans CPU, Case 3183, 2163

# 2022-12-05 I turned on, cpu fan did not start, some smell
# CPU 64.0C, Sens0 64.0C, Sens1 63.0C, Board Chip 48.0, 25.0; Fans CPU, Case    0, 2191
# flicked the fan to start, then alteranted between 0 & 10546,
# cold reboot

# 2022-12-08 ~11:00 machine froze, cold reboot, after:
# CPU 39.0C, Sens0 39.0C, Sens1 39.0C, Board Chip 25.0, 25.0; Fans CPU, Case 1028, 2220

# --------------------------------------------

while true
do
case `hostname -s` in
	fire)	# ( bracketc.c
		# `date -u +%Y-%m-%dT%H:%M:%SZ`:
		echo "CPU\
 `sysctl -n dev.cpu.0.temperature`,\
 Sens0\
 `sysctl -n dev.amdtemp.0.sensor0.core0`,\
 Sens1\
 `sysctl -n dev.amdtemp.0.sensor1.core0`,\
 `mbmon -I -c 1\
 |grep =\
 |grep Temp\
 |sed -e 's/Temp.= /Board Chip /'\
 |sed -e 's/Rot.= /Fans CPU, Case /'\
 |sed -e 's/127\.0, //'\
 |sed -e 's/124\.0, //'\
 |sed -e 's/121\.0, //'\
 |sed -e 's/, \ \ \ 0\$//'\
`"
	# |sed -e 's/127\.0, //'\
		# I used to omit the 127.0 which I had previously
		# guessed might have been a dummy unused sensor, but.
		# 2022-09-08 (when also CPU Fan started reportedy
		# eraticly,  instead of 3000 RPM now 0 to 10,546) the
		# 127.0 occasionaly changes to 124.0
		# A cold reboot restored sanity to CPU fan, now back to 2812-3068
		# I till see periodic 124.0
		# xmbmon -tmin 30 -tmax 55
		#	MB   38.0
		#	CPU 127.0
		#	Chip 25.0
		#	Vc    1.26
		# PS the Case fan (with PC lid off) reports a steady 2136 or 2163
	# |sed -e 's/, \ \ \ 0\$//'
		# Omit trailing 0 which may be an unused 2nd case fan speed,
		# Check mboard or manual & see if there a 2nd set of stakes
		# I could plug a 2nd chassis fan on ? JJLATER
		#----
		# sysctl -n kern.timecounter.tc.TSC.frequency`
		#	| sed -e s/000000//
		# sysctl -n machdep.tsc_freq`" | \
		#	sed -e 's/000000/ MHz/'
		# One of the 2 frequencies above is also shown by powerd -v
		#----
		# echo "cpu \ \ \ \ \ `sysctl -n dev.cpu.0.freq levels`"
		# echo "powernow `sysctl -n dev.powernow.0.freq_settings`"
		#----
		;;
	lapr)	# ( bracketc.c
		echo "`date -u +%Y-%m-%dT%H:%M:%SZ`:\
 `sysctl -n dev.cpu.0.temperature`\
 `sysctl -n dev.cpu.1.temperature`\
 `sysctl -n dev.cpu.2.temperature`\
 `sysctl -n dev.cpu.3.temperature`\
 Freq `sysctl -n dev.cpu.0.freq`\
 Bat `sysctl -n hw.acpi.battery.life`\
"
		;;

	*)	# ( bracketc.c
		echo "Host `hostname -s` not known."
		sysctl -a | grep temp
		sysctl -a | grep freq
		exit 1
		;;
	esac
# echo "-------------------------------------------------------------"
sleep 2
done
