Last active
December 21, 2024 02:08
-
-
Save exelban/ec24807e0fd4326b1bca962e8ae5786a to your computer and use it in GitHub Desktop.
Python script that allows monitoring sensors value changes over time. Help to detect sensors on macOS. Requires colorama python package and Stats that contains SMC tool.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Usage: | |
# (while true; do /Applications/Stats.app/Contents/Resources/smc list -t; sleep 1; done) | python3 watch-smc.py | |
from colorama import Fore, Back, Style | |
import time | |
import fileinput | |
import re | |
keys = {} | |
orig = {} | |
recent = {} | |
names = {} | |
pwr_re = re.compile(r'\[([A-Za-z0-9 ]{4})\]\s+([0-9.]+)') | |
start = time.time() | |
def name_fmt(key): | |
return names.get(key, "") | |
def delta_fmt(delta, color_thresh, bright_thresh, prefix): | |
abs_delta = abs(delta) | |
color = "" | |
if abs_delta > color_thresh: | |
color = Fore.RED if delta > 0.0 else Fore.GREEN | |
color += Style.BRIGHT if abs_delta > 10.0 else "" | |
return color + f"{prefix}{delta:+8.3f}" + Style.RESET_ALL | |
for line in fileinput.input(): | |
line = line.strip() | |
# Empty line, which corresponds to the second line for an "smc list" read. | |
# Used to identify the start of a new sensor read cycle. | |
if not line: | |
print(f"{time.time() - start:7.3f}") | |
continue | |
if line[0] != '[': | |
continue | |
m = pwr_re.match(line) | |
if not m: | |
continue | |
key, value = m.groups() | |
value = float(value) | |
if key not in keys: | |
# First read, store values. | |
keys[key] = value | |
orig[key] = value | |
continue | |
# Delta since start of run. | |
basedelta = value - orig[key] | |
# Delta since last read. | |
delta = value - keys[key] | |
# Store new value. | |
keys[key] = value | |
abs_delta = abs(delta) | |
abs_basedelta = abs(basedelta) | |
color = Style.RESET_ALL | |
if abs_basedelta > 7.0: | |
recent[key] = 1 | |
if abs_delta > 3.0: | |
recent[key] = 2 | |
print(f"{key}: {value:7.3f} {delta_fmt(delta, 3.0, 10.0, 'L')} {delta_fmt(basedelta, 3.0, 10.0, 'B')} {name_fmt(key)}" + Style.RESET_ALL) | |
elif key in recent: | |
if recent[key] > 0: | |
recent[key] -= 1 | |
print(f"{key}: {value:7.3f} {delta_fmt(delta, 3.0, 10.0, 'L')} {delta_fmt(basedelta, 3.0, 10.0, 'B')} {name_fmt(key)}" + Style.RESET_ALL) | |
else: | |
del recent[key] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment