Skip to content

Instantly share code, notes, and snippets.

@KCCat
Last active July 31, 2020 17:26
Show Gist options
  • Save KCCat/e55d05f60a6f7b5dcf0019f5301fd268 to your computer and use it in GitHub Desktop.
Save KCCat/e55d05f60a6f7b5dcf0019f5301fd268 to your computer and use it in GitHub Desktop.
spectrwm_status_ber.py python3
#!/usr/bin/env python3
from time import sleep, strftime, localtime
from sys import stdout
from os import statvfs
#import time, sys
from pathlib import Path
def hwmon():
_hwmon = list(Path('/sys/class/hwmon/').iterdir())
_hwmon.sort()
return "|".join("|".join([reader(x) for x in _hwmon if x.is_dir()]).split('||'))
def reader(hwmon_path):
hwmon_name = hwmon_path / 'name'
if hwmon_name.exists():
with hwmon_name.open() as r:
_name = r.read().split('\n')[0]
if _name == 'amdgpu':
return amdgpu(hwmon_path)
if _name == 'zenpower':
return zenpower(hwmon_path)
if _name == 'nct6797':
return nct6797(hwmon_path)
if _name == 'nvme':
return nvme(hwmon_path)
return ''
def amdgpu(hwmon_path):
#/sys/class/drm/card0/device/hwmon/hwmon0/power1_average
#/sys/class/drm/card0/device/hwmon/hwmon0/fan1_input
#/sys/class/drm/card0/device/hwmon/hwmon0/temp1_input
#/sys/class/drm/card0/device/hwmon/hwmon0/in0_input
#/sys/class/drm/card0/device/hwmon/hwmon0/freq1_input
#/sys/class/drm/card0/device/pp_dpm_sclk
#/sys/class/drm/card0/device/gpu_busy_percent
with open(hwmon_path / 'device' / 'gpu_busy_percent') as r:
use = r.read().split('\n')[0]
with open(hwmon_path / 'freq1_input') as r:
sclk = r.read()[:-7]
with open(hwmon_path / 'freq2_input') as r:
mclk = r.read()[:-7]
with open(hwmon_path / 'temp1_input') as r:
temp = r.read()[:-4]
with open(hwmon_path / 'temp2_input') as r:
temp_j = r.read()[:-4]
with open(hwmon_path / 'temp3_input') as r:
temp_mem = r.read()[:-4]
with open(hwmon_path / 'fan1_input') as r:
fan = r.read()[:-1]
with open(hwmon_path / 'power1_average') as r:
power = r.read()[:-7]
with open(hwmon_path / 'in0_input') as r:
vu = r.read()[:-1]
return 'GPU%3s%%%4sMhz%4sMhz%3s°c%3s°c%3s°c %4sRPM%4smV%3sW' % (use, sclk, mclk, temp, temp_j, temp_mem, fan, vu, power)
def zenpower(hwmon_path):
with open(hwmon_path / 'temp1_input') as r: #Tdie
die = r.read()[:-4]
# with open(hwmon_path / 'temp2_input') as r: #Tctl
# ctl = r.read()[:-4]
with open(hwmon_path / 'temp3_input') as r: #Tccd1
ccd1 = r.read()[:-4]
with open(hwmon_path / 'temp4_input') as r: #Tccd2
ccd2 = r.read()[:-4]
with open(hwmon_path / 'power1_input') as r: #core_power
core = r.read()[:-7]
with open(hwmon_path / 'power2_input') as r: #soc_power
soc = r.read()[:-7]
return 'CPU DIE%3s°c CCD%3s°c%3s°c%3sW%3sW' % (die, ccd1, ccd2, core, soc)
def nct6797(hwmon_path):
with open(hwmon_path / 'temp3_input') as r:
mosin = r.read()[:-4]
with open(hwmon_path / 'temp5_input') as r:
pchin = r.read()[:-4]
with open(hwmon_path / 'fan2_input') as r:
cpufan = r.read()[:-1]
with open(hwmon_path / 'fan1_input') as r:
fan1 = r.read()[:-1]
return 'MOS%3s°c PCH%3s°c%4s/%4sRPM' % (mosin, pchin, cpufan, fan1,)
def nvme(hwmon_path):
with open(hwmon_path / 'temp1_input') as r:
nvme_in = r.read()[:-4]
return 'NVME%3s°c' % (nvme_in, )
'''
def net():
#/sys/class/net/enp24s0/statistics/rx_bytes
#/sys/class/net/enp24s0/statistics/rx_bytes
b = ['B/s', 'KB/s', 'MB/s', 'GB/s']
card = '/sys/class/net/enp24s0/statistics/'
o_rx = [0]*5
o_tx = [0]*5
while 1:
with open(card + 'rx_bytes') as r:
rx = int(r.read())
with open(card + 'tx_bytes') as r:
tx = int(r.read())
l = [(rx - o_rx.pop())//5, (tx - o_tx.pop())//5]
p = [len(str(x))//3 for x in l]
n = zip(l, p)
h = [x[0]/pow(1024, x[1]) for x in n]
yield '[↓%5.2f%4s ↑%5.2f%4s]' % (h[0], b[p[0]], h[1], b[p[1]])
o_rx.insert(0, rx)
o_tx.insert(0, tx)
'''
def net():
#/sys/class/net/enp24s0/statistics/rx_bytes
#/sys/class/net/enp24s0/statistics/rx_bytes
b = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'NaN']
card = '/sys/class/net/enp39s0/statistics/'
o_rx = o_tx = 0
while 1:
with open(card + 'rx_bytes') as r:
rx = int(r.read())
with open(card + 'tx_bytes') as r:
tx = int(r.read())
l = [rx - o_rx, tx - o_tx]
p = [min(len(str(x))//3, 4) for x in l]
n = zip(l, p)
h = [x[0]/pow(1024, x[1]) for x in n]
yield '↓%5.2f%4s ↑%5.2f%4s' % (h[0], b[p[0]], h[1], b[p[1]])
o_rx = rx; o_tx = tx
#def disk(path):
# b = ['B', 'K', 'M', 'G', 'T']
# root = statvfs(path)
# root_f = root.f_bavail * root.f_bsize
# root_n = len(str(root_f))//3
# root_h = root_f/pow(1024, root_n)
# return '%s:%5.2f%s' % ('/'+path.split('/')[-1], root_h, b[root_n])
def loop():
r_net = net()
while True:
stdout.write('|%s|%s|%s \n' % (next(r_net), hwmon(), strftime("%Y-%m-%d %H:%M", localtime()) ))
stdout.flush()
sleep(1)
loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment