brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: smartctl --json turns disk health into data a script can read

Disk
~/tips/tip-smart-smartctl-json-jq

sudo smartctl -a --json /dev/sda | jq '.smart_status.passed'

Prints SMART data as JSON, so scripts can check pass or fail, temperature and hours with jq.

Parsing the plain text of smartctl with grep and awk breaks the day a line changes. The tool can print JSON instead, and then jq does the picking.

sudo smartctl -a --json /dev/sda | jq '.smart_status.passed'

The --json option switches smartctl to JSON output. In the smartmontools source the same structure carries a few fields that are handy for a quick check:

sudo smartctl -a --json /dev/sda | jq '{passed: .smart_status.passed, temp: .temperature.current, hours: .power_on_time.hours, exit: .smartctl.exit_status}'

That gives you the overall health verdict, the current temperature, the power-on hours and smartctl’s own exit status in one small object. Which keys exist depends on the drive and its protocol, so a SATA disk and an NVMe drive don’t have exactly the same fields. I’d check before building a script on one.

The man page documents extra modes as letters after an equals sign. g prints every value as a single assignment with its full path, which you can grep, and the syntax is compatible with gron:

sudo smartctl -a --json=g /dev/sda | grep -i temperature

That is the easiest way to find the path of a value you want. c gives compact output, s sorts the keys, and o adds the complete original plain-text output as an array, which is useful when you want both forms. You can combine letters, for example --json=co.

The caveat is that JSON mode only changes the format. A failing disk still needs a person to read the attributes and the self-test log. The pass or fail flag is the drive’s own overall verdict, so I’d still read the attributes and the self-test log.

Further reading: smartctl(8) man page, smartmontools

More Disk tips sudo e2freefrag /dev/sdb1 lsscsi -s udisksctl power-off -b /dev/sdX du -sh — .[!.]* * 2>/dev/null | sort -h ls -l /dev/disk/by-id/ All Disk tips (44) All topics

Link