disktemp — disk temperature, or an honest 'unavailable'

disktemp — disk temperature, or an honest 'unavailable'

disktemp is a LightGUI (Python/XFrame) port of the disktemp utility from util_box: disk temperature from SMART if smartctl is present, else /sys sensors, else a clear message. Press Refresh and it probes in the CLI's order — smartctl -A per block device first, then /sys/class/hwmon temp inputs, then /sys/class/thermal zones — and shows what it found. It is one of the 285 apps in util_boxgui, the project recreating util_box utilities as LightGUI apps, each with a screenshot and a walkthrough on the matching LightGUI doc site. The full source is apps/python/disktemp/disktemp.py in that repo.

The probe is already run when the app starts, so a plain launch + screenshot shows a real result. On this box the honest result is "unavailable": the build machine is a VM with no smartctl and no /sys sensors, so there is no temperature to show — and the app says so plainly instead of faking a reading or crashing. The screenshot below is the default state, not a staged one, and the "unavailable" message is the correct output for this machine.

The GUI layout

disktemp running under Xvfb
disktemp running — the honest "unavailable" message on a VM with no sensors, captured under Xvfb.

The window is 800×480 and holds three widgets, created with create() + set_prop() and drawn on a single Gtk.DrawingArea — the simplest layout in this batch, because there is nothing to type:

  • Refresh: a GtkButton that re-runs the temperature probe.
  • Hint line: a GtkLabel explaining the probe order: SMART first, then /sys sensors, else a clear message.
  • Result area: a GtkTextView filling the rest of the window, showing the temperatures found — or the clear "unavailable" message when the box has no sensors. It is read-only — clicking it just redraws.

One honest limitation, inherited from how LightGUI draws its widgets with Cairo on one drawing area: the result area does not really scroll — the text-view scroll bars are drawn, not functional, so a machine with more sensors than fit the window clips at the bottom. This app has no entry, so there is no typing and no focus to wire — the one app in this batch that needs no keyboard handling at all.

When sensors do exist, the result is a plain list with a <-- HOT flag on anything at or above 60 C (SMART) or 80 C (/sys) — the CLI's own thresholds. On this VM the result is the message, reproduced here exactly as the app shows it:

disk temperature: unavailable on this system.
  - smartctl not installed
  - no temperature sensors found under /sys/class/hwmon or /sys/class/thermal
This is normal on a VM or container with no physical disks attached.
On a physical machine, install smartmontools for SMART readings:
  apt install smartmontools   # then: smartctl -A /dev/sda

The source, in sections

disktemp.py is about 317 lines. It is modelled on the LightGUI Python examples gtkbutton.py and gtktextview.py (and the datecalc app in this repo): widgets are created with create() + set_prop(), drawn on a Gtk.DrawingArea by widget.draw(cr), hit-tested on click, and dispatched through a _HANDLERS table. The probe half, though, is not modelled on anything — it is vendored verbatim from the CLI, so the GUI can never drift from it.

The probes. smartctl_temps() and sys_temps() are copied byte-for-byte from the CLI script into disktemp.py. The SMART probe runs smartctl -A on every /dev/sd? and /dev/nvme?n? device and pulls the temperature out with the CLI's two regexes — Temperature_Celsius first, then the Temperature: N C fallback. The /sys probe reads hwmon temp*_input files (millidegrees, divided by 1000) with their labels, and falls back to thermal zones when there are no hwmon inputs at all:

def smartctl_temps():
    out = {}
    if not shutil.which("smartctl"):
        return out, "smartctl not installed"
    for dev in sorted(glob.glob("/dev/sd?") + glob.glob("/dev/nvme?n?")):
        try:
            r = subprocess.run(["smartctl", "-A", dev], capture_output=True,
                               text=True, timeout=10)
        except (OSError, subprocess.TimeoutExpired):
            continue
        txt = r.stdout
        m = re.search(r"Temperature_Celsius.*?(\d+)\s*$", txt, re.M)
        if not m:
            m = re.search(r"Temperature:\s*(\d+)\s*C", txt)
        if m:
            out[dev] = int(m.group(1))
    return out, None

compute_text() is the GUI's own half: it runs the probes in the CLI's order and formats the result — SMART temperatures first if any were found, /sys temperatures next, and the "unavailable" message (with the reason and the smartmontools hint) when nothing is available. It never crashes when nothing is available; that is the CLI's behaviour too, and the point of the app. A reading at or above the HOT threshold gets the flag, exactly as the CLI does.

The widgets. _build_widgets() creates the three widgets with their design-time geometry, and an _ANCHORS table records each widget's anchor flags so _apply_anchors() can reposition them when the window is resized — recomputed from the design geometry every time, so a resize is idempotent and never drifts. The output area is created already holding the probe result — that is why the window opens populated.

Events. There is almost nothing to wire: clicking Refresh re-probes, and clicking the output area just redraws. The dispatch is the same small hit-test as the other apps — the topmost visible widget containing the click gets the event through the _HANDLERS table, and a button release always goes to the widget that got the press, even if the pointer moved off it. main() is the standard LightGUI Python harness: a Gtk.Window titled disktemp - util_boxgui holding a Gtk.DrawingArea, with the draw, button-press, button-release and resize signals connected, and Gtk.main() running the loop.

Building and running it

There is nothing to build — it is pure Python. Run it with the LightGUI Python package on PYTHONPATH, under a real X server or under Xvfb (display :97 is the one used for this app). It needs python3-gi / gir1.2-gtk-3.0 and the LightGUI Python checkout:

cd apps/python/disktemp
PYTHONPATH=/root/graphfix-wt/lightgui_python python3 disktemp.py

# Under Xvfb:
Xvfb :97 -screen 0 1024x768x24 &
DISPLAY=:97 PYTHONPATH=/root/graphfix-wt/lightgui_python python3 disktemp.py

The PYTHONPATH above points at the clean graphfix-wt checkout on the build machine — the usual /root/lg-review/lightgui_python checkout is mid-merge as of this writing, so batch 5 was built against the clean one. On your own machine, point it at wherever you cloned LightGUI Python. The window title is disktemp - util_boxgui. On a physical machine, install smartmontools first (apt install smartmontools) — without it, and without /sys sensors, you will see the same "unavailable" message this VM shows, which is the correct result.

What the CLI version does differently

Nothing in behaviour — the GUI shows the CLI's exact output in the result area instead of stdout. The CLI is disktemp [--selftest]; the GUI has no --selftest flag and no options at all, because there is nothing to configure: probe, show, refresh. A reading at or above 60 C (SMART) or 80 C (/sys) is flagged <-- HOT in both. Verified against the CLI on this machine: the result text is exactly what the CLI prints for the same run — on this VM, the "unavailable" message, which is the honest result when a box has no sensors.

See also

Source code: git.lightgui.com/thinkmelt/lightgui_python