Skip to content

Instantly share code, notes, and snippets.

@jhoblitt
Last active February 17, 2025 21:28
Show Gist options
  • Save jhoblitt/04f7eb48b9a11a1743ee903c91af1ee6 to your computer and use it in GitHub Desktop.
Save jhoblitt/04f7eb48b9a11a1743ee903c91af1ee6 to your computer and use it in GitHub Desktop.
chatgpt 01 generated script to look for failed nvme devices. The prompt was: `Write a bash script which loops over all nvme devices installed in a system and runs the smart-log subcommand and reports any device which has a critical_warning other than 0.`
#!/usr/bin/env bash
#
# nvme-smart-check.sh
#
# This script lists all NVMe devices on the system, runs 'nvme smart-log'
# on each, and reports any device whose 'critical_warning' is non-zero.
#
# Exit on any error (optional; remove 'set -e' if you prefer to continue on errors)
set -e
ret=0
# Iterate over all NVMe devices found by 'nvme list'
for dev in $(nvme list | awk '/^\/dev\/nvme[0-9]/ { print $1 }'); do
# Extract the critical_warning value from 'nvme smart-log' output.
# The line should look like: "critical_warning : 0x00" or similar.
# We capture that and store in 'cw'.
cw=$(nvme smart-log "$dev" 2>/dev/null \
| awk '/critical_warning/ {print $3}' \
| tr -d '[:space:]')
# Convert hex (e.g. 0x01) or decimal to an integer for comparison:
# This $(( )) shell expansion can handle 0xNN syntax correctly.
cw_val=$(( cw ))
# If critical_warning is non-zero, report the device.
if [[ $cw_val -ne 0 ]]; then
echo "WARNING: Device $dev has a critical warning (critical_warning=$cw)."
((ret++))
fi
done
exit $ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment