How to Monitor Total SSD Writes in Linux Using SMART Attributes and smartctl


19 views

For developers and sysadmins working with SSDs in Linux environments, monitoring write endurance is crucial for both performance tuning and hardware longevity. The SMART (Self-Monitoring, Analysis and Reporting Technology) system provides the most reliable way to access these metrics.

The primary toolchain consists of:

sudo apt-get install smartmontools gsmartcontrol  # Debian/Ubuntu
sudo yum install smartmontools gsmartcontrol      # RHEL/CentOS

For a quick overview of your SSD's health and write statistics:

sudo smartctl -a /dev/sdX | grep -E "Media_Wearout_Indicator|Total_LBAs_Written|Data_Units_Written|Host_Write_Commands"

Different SSD manufacturers implement SMART attributes differently. Here are the key attributes to check:

Intel SSDs

sudo smartctl -A /dev/sdX | grep -i "Host_Writes_32MiB"
# Attribute 225 shows total writes in 32MiB units

Samsung SSDs

sudo smartctl -A /dev/sdX | grep -i "Total_LBAs_Written"
# Attribute 241 shows writes in 512-byte sectors

Crucial/Micron SSDs

sudo smartctl -A /dev/sdX | grep -i "NAND_Writes"
# Shows total GB written to NAND

For Samsung drives (assuming 512-byte sectors):

sudo smartctl -A /dev/sda | awk '/Total_LBAs_Written/ {print $10*512/1024/1024/1024 " GB"}'

Create a bash script for regular monitoring:

#!/bin/bash
DEVICE="/dev/sda"
OUTPUT=$(sudo smartctl -A $DEVICE)

# Intel calculation
intel_writes=$(echo "$OUTPUT" | awk '/Host_Writes_32MiB/ {print $10*33.55}')
echo "Intel Total Writes: $intel_writes GB"

# Samsung calculation
samsung_writes=$(echo "$OUTPUT" | awk '/Total_LBAs_Written/ {print $10*0.0000005}')
echo "Samsung Total Writes: $samsung_writes GB"

When analyzing SSD write metrics, consider:

  • Manufacturer's TBW (Terabytes Written) rating
  • Current write amplification factor
  • Percentage of rated endurance used
  • Comparison with power-on hours

For production systems, consider setting up node_exporter with textfile collector:

#!/bin/bash
DEVICE="/dev/nvme0n1"
METRICS_FILE="/var/lib/node_exporter/ssd_metrics.prom"

# Get SMART data
DATA=$(sudo smartctl -A $DEVICE)

# Extract and format metrics
echo "# HELP ssd_total_bytes_written Total bytes written to SSD" > $METRICS_FILE
echo "# TYPE ssd_total_bytes_written gauge" >> $METRICS_FILE
echo "ssd_total_bytes_written $(echo "$DATA" | awk '/Data_Units_Written/ {print $10*1000*512}')" >> $METRICS_FILE

Linux provides several methods to check SSD health metrics including total writes. The most reliable approach uses SMART (Self-Monitoring, Analysis and Reporting Technology) data through smartctl:

# Install smartmontools
sudo apt install smartmontools  # Debian/Ubuntu
sudo yum install smartmontools  # RHEL/CentOS

# Check basic health
sudo smartctl -a /dev/sda

# Filter for write-related attributes
sudo smartctl -A /dev/sda | grep -i "written\|write"

Different SSD manufacturers report write statistics through distinct SMART IDs:

# Intel SSDs (usually Attribute 225)
sudo smartctl -A /dev/sda | grep "Host_Writes_32MiB"

# Samsung SSDs (usually Attribute 241)
sudo smartctl -A /dev/sda | grep "Total_LBAs_Written"

# Crucial/Micron SSDs
sudo smartctl -A /dev/sda | grep "Total_LBAs_Written"

Convert raw SMART values to meaningful storage units:

# For Intel SSDs (32MiB units):
total_writes=$(sudo smartctl -A /dev/sda | awk '/Host_Writes_32MiB/{print $10}')
echo "Total writes: $(($total_writes * 32)) MiB"

# For Samsung SSDs (512B LBA units):
total_lbas=$(sudo smartctl -A /dev/sda | awk '/Total_LBAs_Written/{print $10}')
echo "Total writes: $(($total_lbas * 512 / 1024 / 1024 / 1024)) GB"

For persistent tracking, consider these approaches:

# Systemd service unit example (/etc/systemd/system/ssd-write-logger.service)
[Unit]
Description=SSD Write Logger

[Service]
ExecStart=/usr/local/bin/ssd-write-logger.sh
Restart=always

[Install]
WantedBy=multi-user.target

# Corresponding shell script (/usr/local/bin/ssd-write-logger.sh)
#!/bin/bash
while true; do
  timestamp=$(date +"%Y-%m-%d %T")
  writes=$(smartctl -A /dev/sda | awk '/Total_LBAs_Written/{print $10}')
  echo "$timestamp,$writes" >> /var/log/ssd_writes.csv
  sleep 3600
done

Several GUI and CLI alternatives exist:

  • gsmartcontrol: Graphical SMART tool
  • nvme-cli: For NVMe-specific SSDs (sudo nvme smart-log /dev/nvme0)
  • diskspd: Advanced performance monitoring

Key parameters to monitor:

Metric SMART ID Importance
Media Wearout Indicator 233 SSD lifespan percentage
Power On Hours 9 Total operational time
Unsafe Shutdowns 192 Power failure events