Skip to content

Commit

Permalink
#371: implement hardware temperatures on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 24, 2016
1 parent 6c3e3e1 commit 856b3bf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
33 changes: 33 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,39 @@ def users():
return _psplatform.users()


if hasattr(_psplatform, "temperatures"):

def temperatures(fahrenheit=False):
"""Return hardware temperatures as a list of named tuples.
Each entry represents a "sensor" monitoring a certain hardware
resource.
The hardware resource may be a CPU, an hard disk or something
else, depending on the OS and its configuration.
All temperatures are expressed in celsius unless 'fahrenheit'
parameter is specified.
This function may raise NotImplementedError in case the OS
is not configured in order to provide these metrics.
"""
def to_fahrenheit(n):
return (n * 9 / 5) + 32

ret = []
for rawtuple in _psplatform.temperatures():
name, label, current, high, critical = rawtuple
if fahrenheit:
current = to_fahrenheit(current)
if high is not None:
high = to_fahrenheit(high)
if critical is not None:
critical = to_fahrenheit(critical)
if high and not critical:
critical = high
elif critical and not high:
high = critical
ret.append(_common.shwtemp(name, label, current, high, critical))
return ret


# =====================================================================
# --- Windows services
# =====================================================================
Expand Down
2 changes: 2 additions & 0 deletions psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class NicDuplex(enum.IntEnum):
# psutil.cpu_stats()
scpustats = namedtuple(
'scpustats', ['ctx_switches', 'interrupts', 'soft_interrupts', 'syscalls'])
shwtemp = namedtuple(
'shwtemp', ['name', 'label', 'current', 'high', 'critical'])

# --- for Process methods

Expand Down
52 changes: 52 additions & 0 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import base64
import errno
import functools
import glob
import os
import re
import socket
Expand Down Expand Up @@ -63,6 +64,7 @@

HAS_SMAPS = os.path.exists('/proc/%s/smaps' % os.getpid())
HAS_PRLIMIT = hasattr(cext, "linux_prlimit")
_DEFAULT = object()

# RLIMIT_* constants, not guaranteed to be present on all kernels
if HAS_PRLIMIT:
Expand Down Expand Up @@ -1059,6 +1061,56 @@ def boot_time():
"line 'btime' not found in %s/stat" % get_procfs_path())


def temperatures():
"""Return hardware (CPU and others) temperatures as a list
of named tuples including name, label, current, max and
critical temperatures.
Implementation notes:
- /sys/class/hwmon looks like the most recent interface to
retrieve this info, and this implementation relies on it
only (old distros will probably use something else)
- lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon
- /sys/class/thermal/thermal_zone* is another one but it's more
difficult to parse
"""
def cat(fname, replace=_DEFAULT):
try:
f = open(fname)
except IOError:
if replace != _DEFAULT:
return replace
else:
raise
else:
with f:
return f.read().strip()

path = '/sys/class/hwmon'
if not os.path.exists(path):
raise NotImplementedError(
"%s hwmon fs does not exist on this platform" % path)

ret = []
basenames = sorted(set(
[x.split('_')[0] for x in
glob.glob('/sys/class/hwmon/hwmon*/temp*_*')]))
for base in basenames:
name = cat(os.path.join(os.path.dirname(base), 'name'))
label = cat(base + '_label', replace='')
current = int(cat(base + '_input')) / 1000.0
high = cat(base + '_max', replace=None)
if high is not None:
high = int(high) / 1000.0
critical = cat(base + '_crit', replace=None)
if critical is not None:
critical = int(critical) / 1000.0

ret.append((name, label, current, high, critical))

return ret


# =====================================================================
# --- processes
# =====================================================================
Expand Down

0 comments on commit 856b3bf

Please sign in to comment.