diff options
Diffstat (limited to 'psutil')
-rw-r--r-- | psutil/__init__.py | 16 | ||||
-rw-r--r-- | psutil/_pslinux.py | 35 |
2 files changed, 51 insertions, 0 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index 1d03d8af..23a33c85 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -1273,6 +1273,22 @@ def get_users(): """ return _psplatform.get_system_users() +if sys.platform.startswith("linux"): + + def get_cpu_temp(fahrenheit=False): + """Return temperatures for each physical CPU installed on the + system as a list of namedtuple including the following fields: + + - name: CPU name + - temp: current temperature + - max: maximum temperature + - critical: temperature considered critical + + If fahrenheit == False values are expressed in Celsius. + Linux only and still experimental. + """ + return _psplatform.get_cpu_temp(fahrenheit) + # ===================================================================== # --- deprecated functions # ===================================================================== diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index d3f30348..825bed72 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -268,6 +268,41 @@ def get_system_per_cpu_times(): finally: f.close() +def _cat(file, int_=False): + f = open(file, 'r') + try: + content = f.read().strip() + finally: + f.close() + if int_: + content = int(content) + return content + +nt_cpu_temp = namedtuple('cputemp', 'name temp max critical') + +def get_cpu_temp(fahrenheit=False): + """Return CPU temperatures expressed in Celsius.""" + # References: + # http://www.mjmwired.net/kernel/Documentation/hwmon/sysfs-interface + # /sys/class/hwmon/hwmon0/temp1_* + base = '/sys/class/hwmon/' + ret = [] + ls = sorted(os.listdir(base)) + if not ls: + raise RuntimeError('no files in ' + base) + for hwmon in ls: + hwmon = os.path.join(base, hwmon) + label = _cat(os.path.join(hwmon, 'temp1_label')) + assert 'cpu temp' in label.lower(), label + name = _cat(os.path.join(hwmon, 'name')) + temp = _cat(os.path.join(hwmon, 'temp1_input'), int_=True) / 1000 + max_ = _cat(os.path.join(hwmon, 'temp1_max'), int_=True) / 1000 + crit = _cat(os.path.join(hwmon, 'temp1_crit'), int_=True) / 1000 + digits = (temp, max_, crit) + if fahrenheit: + digits = [(x * 1.8) + 32 for x in digits] + ret.append(nt_cpu_temp(name, *digits)) + return ret # --- system disk functions |