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 /psutil/_pslinux.py | |
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
Diffstat (limited to 'psutil/_pslinux.py')
-rw-r--r-- | psutil/_pslinux.py | 35 |
1 files changed, 35 insertions, 0 deletions
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 |