diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-27 18:55:51 +0000 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-27 18:55:51 +0000 |
commit | 0af7193f6931081514ea1ac684bb2a344a4ace1d (patch) | |
tree | 6c725b176173b87d9be5e7eb78f14fa05661a345 /src/app.py | |
parent | debian/README: update (diff) | |
parent | add thermal sensor readings from /sys/class/thermal (diff) | |
download | linux-status-0af7193f6931081514ea1ac684bb2a344a4ace1d.tar.gz linux-status-0af7193f6931081514ea1ac684bb2a344a4ace1d.zip |
Merge tag 'v2.4' into debian
Diffstat (limited to 'src/app.py')
-rwxr-xr-x | src/app.py | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -47,6 +47,7 @@ from http import HTTPStatus import json import os import pwd +import re import shlex import socket import subprocess @@ -314,6 +315,7 @@ class DockerStatus(DockerInspect): 'status': info['State']['Status'], } + class Hostname(Task): def run(self): pass @@ -322,6 +324,64 @@ class Hostname(Task): return socket.gethostname() +class ThermalInfo(Task): + ROOT = '/sys/class/thermal' + + @staticmethod + def _collect_dirs(): + root = ThermalInfo.ROOT + dirs = [ + dir for dir in os.listdir(root) + if re.match(r'^thermal_zone\d+$', dir) + ] + dirs = sorted(dirs) + dirs = [os.path.join(root, dir) for dir in dirs] + return dirs + + @staticmethod + def _read_temp(dir): + with open(os.path.join(dir, 'temp')) as fd: + temp = fd.read() + return ThermalInfo._parse_temp(temp) + + @staticmethod + def _parse_temp(temp): + if not temp.endswith('\n'): + raise RuntimeError('invalid temp file contents: ' + temp) + temp = temp.strip('\n') + try: + temp = int(temp) + except ValueError: + raise RuntimeError('invalid temp file contents: ' + temp) + return round(temp / 1000, 2) + + @staticmethod + def _read_type(dir): + with open(os.path.join(dir, 'type')) as fd: + type = fd.read() + return ThermalInfo._parse_type(type) + + @staticmethod + def _parse_type(type): + if not type.endswith('\n'): + raise RuntimeError('invalid type file contents: ' + type) + type = type.strip('\n') + return type + + @staticmethod + def _read_dir(dir): + return { + 'temp': ThermalInfo._read_temp(dir), + 'type': ThermalInfo._read_type(dir), + } + + def run(self): + pass + + def result(self): + return [self._read_dir(dir) for dir in ThermalInfo._collect_dirs()] + + class Top(Command): COMMAND = None @@ -401,6 +461,7 @@ class Status(TaskList): def __init__(self): tasks = { 'hostname': Hostname(), + 'thermal': ThermalInfo(), 'system': SystemStatus(), 'user': UserStatusList(), } |