diff options
author | Giampaolo Rodola' <g.rodola@gmail.com> | 2013-04-27 18:35:10 +0200 |
---|---|---|
committer | Giampaolo Rodola' <g.rodola@gmail.com> | 2013-04-27 18:35:10 +0200 |
commit | 7e31b67956dacb0e6b704d6e4c4b0fe37faebbed (patch) | |
tree | 9dc4632689ff6100b36374bf53fefe936e0da5b8 | |
parent | ad7fe470bce703165a233495a3daee1330489e3e (diff) | |
download | psutil-cpu-temp.tar.gz |
Issue 371: implement CPU temperature on Linux by parsing files in /sys/class/hwmon/hwmon0/temp1_*. It is probably incomplete compared to what we can get via lm-sensors though so care should be taken on whether adding this or not.cpu-temp
-rw-r--r-- | psutil/__init__.py | 16 | ||||
-rw-r--r-- | psutil/_pslinux.py | 35 | ||||
-rw-r--r-- | test/test_psutil.py | 11 |
3 files changed, 62 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 diff --git a/test/test_psutil.py b/test/test_psutil.py index 3650e333..860de920 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -823,6 +823,17 @@ class TestCase(unittest.TestCase): if (max([kernel_time, ktime]) - min([kernel_time, ktime])) > 0.1: self.fail("expected: %s, found: %s" %(ktime, kernel_time)) + if LINUX: + def test_cpu_temp(self): + ls = psutil.get_cpu_temp() + if not ls: + self.fail('psutil.get_cpu_temp() returns an empty list') + for item in ls: + assert item.name, item + assert item.temp >= 0, item + assert item.max >= 0, item + assert item.critical >= 0, item + def test_create_time(self): sproc = get_test_subprocess(wait=True) now = time.time() |