S.M.A.R.T. (Self-Monitoring, Analysis and Reporting Technology) 是一种硬盘自我监控、分析和报告技术,可以监控硬盘的各种参数,预测可能的故障。主要监控指标包括:
smartctl [选项] 设备
| 选项 | 说明 |
|---|---|
| -a | 显示所有SMART信息(等价于 -H -i -c -A) |
| -H | 检查硬盘健康状态(PASSED/FAILED) |
| -i | 显示设备基本信息 |
| -c | 显示SMART功能支持情况 |
| -A | 显示所有SMART属性 |
| -l error | 显示SMART错误日志 |
| -l selftest | 显示自检日志 |
| -t short | 运行短时间自检(通常1-2分钟) |
| -t long | 运行长时间自检(可能数小时) |
| -t conveyance | 运行运输自检(5-10分钟) |
| -X | 中断正在进行的自检 |
| -d TYPE | 指定设备类型(ata, scsi, sat, nvme等) |
| -q TYPE | 指定输出静默模式(errorsonly, noserial) |
| -g all | 显示ATA寄存器值 |
| -r ioctl,N | 调试I/O控制调用 |
| -v 9,hex48 | 设置详细模式 |
| -V | 显示版本信息 |
| -h | 显示帮助信息 |
| 设备类型 | 说明 | 示例 |
|---|---|---|
| ata | ATA/SATA硬盘(默认) | /dev/sda |
| scsi | SCSI/SAS硬盘 | /dev/sg0 |
| sat | SATA硬盘通过SCSI层访问 | /dev/sda |
| nvme | NVMe固态硬盘 | /dev/nvme0n1 |
| usbprolific | USB转接芯片(Prolific) | /dev/sdb |
| usbjmicron | USB转接芯片(JMicron) | /dev/sdb |
# 显示设备基本信息
sudo smartctl -i /dev/sda
# 检查硬盘健康状态
sudo smartctl -H /dev/sda
# 显示所有SMART信息(最常用)
sudo smartctl -a /dev/sda
# 显示NVMe硬盘信息
sudo smartctl -a /dev/nvme0n1
# 显示所有SMART属性
sudo smartctl -A /dev/sda
# 显示错误日志
sudo smartctl -l error /dev/sda
# 显示自检日志
sudo smartctl -l selftest /dev/sda
# 显示ATA寄存器值
sudo smartctl -g all /dev/sda
# 运行短时间自检(约2分钟)
sudo smartctl -t short /dev/sda
# 运行长时间自检(可能数小时)
sudo smartctl -t long /dev/sda
# 运行运输自检(约5-10分钟)
sudo smartctl -t conveyance /dev/sda
# 检查自检进度
sudo smartctl -c /dev/sda | grep -A 5 "Self-test"
# 中断正在进行的自检
sudo smartctl -X /dev/sda
# 明确指定ATA设备
sudo smartctl -d ata -a /dev/sda
# SCSI设备
sudo smartctl -d scsi -a /dev/sg0
# SATA设备通过SCSI层
sudo smartctl -d sat -a /dev/sda
# 使用USB转接芯片的设备
sudo smartctl -d usbjmicron -a /dev/sdb
# RAID阵列中的硬盘
sudo smartctl -d megaraid,0 -a /dev/sda
$ sudo smartctl -A /dev/sda
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
1 Raw_Read_Error_Rate 0x000b 100 100 016 Pre-fail Always - 0
3 Spin_Up_Time 0x0007 092 092 025 Pre-fail Always - 2360
4 Start_Stop_Count 0x0012 100 100 000 Old_age Always - 245
5 Reallocated_Sector_Ct 0x0033 100 100 005 Pre-fail Always - 0
7 Seek_Error_Rate 0x000b 100 100 067 Pre-fail Always - 0
9 Power_On_Hours 0x0012 094 094 000 Old_age Always - 4645
10 Spin_Retry_Count 0x0013 100 100 060 Pre-fail Always - 0
12 Power_Cycle_Count 0x0032 100 100 000 Old_age Always - 245
...
| 属性ID | 属性名 | 正常范围 | 危险信号 |
|---|---|---|---|
| 5 | Reallocated_Sector_Ct | 0 | 任何非零值都表示有坏扇区 |
| 187 | Reported_Uncorrectable_Errors | 0 | 不可纠正的错误增加 |
| 188 | Command_Timeout | 0 | 命令超时次数增加 |
| 197 | Current_Pending_Sector | 0 | 待处理扇区增加 |
| 198 | Offline_Uncorrectable | 0 | 脱机不可纠正错误 |
| 194 | Temperature_Celsius | <50℃ | 持续高温可能缩短寿命 |
| 9 | Power_On_Hours | - | 通电时间过长增加故障风险 |
#!/bin/bash
# smart_check.sh - 自动检查所有硬盘的SMART状态
LOG_FILE="/var/log/smart_check.log"
echo "=== SMART检查报告 $(date) ===" >> $LOG_FILE
# 获取所有硬盘设备
for drive in /dev/sd[a-z] /dev/nvme[0-9]n[0-9]; do
if [ -b "$drive" ]; then
echo "检查设备: $drive" >> $LOG_FILE
# 检查健康状态
health=$(sudo smartctl -H "$drive" | grep "SMART overall-health")
echo "健康状态: $health" >> $LOG_FILE
# 检查关键属性
sudo smartctl -A "$drive" | grep -E "(Reallocated_Sector_Ct|Current_Pending_Sector|Offline_Uncorrectable)" >> $LOG_FILE
# 检查温度
temp=$(sudo smartctl -A "$drive" | grep "Temperature_Celsius" | awk '{print $10}')
echo "温度: ${temp}℃" >> $LOG_FILE
echo "---" >> $LOG_FILE
fi
done
# 发送邮件通知(如果有问题)
if grep -q "FAILED\|Reallocated_Sector_Ct.*[1-9]\|Current_Pending_Sector.*[1-9]" "$LOG_FILE"; then
mail -s "硬盘SMART警报" admin@example.com < "$LOG_FILE"
fi
echo "检查完成" >> $LOG_FILE
#!/bin/bash
# smart_schedule.sh - 安排定期自检
DRIVE="/dev/sda"
# 每周日晚上运行长时间自检
if [ $(date +%u) -eq 7 ]; then
echo "开始长时间自检: $(date)" >> /var/log/smart_long_test.log
sudo smartctl -t long "$DRIVE" >> /var/log/smart_long_test.log
fi
# 每天运行短时间自检
echo "开始短时间自检: $(date)" >> /var/log/smart_short_test.log
sudo smartctl -t short "$DRIVE" >> /var/log/smart_short_test.log
# 记录SMART属性变化
echo "=== SMART属性记录 $(date) ===" >> /var/log/smart_history.log
sudo smartctl -A "$DRIVE" | grep -E "(Power_On_Hours|Reallocated_Sector_Ct|Temperature_Celsius)" >> /var/log/smart_history.log
# 每天凌晨2点检查
0 2 * * * /usr/local/bin/smart_check.sh
# 每周日凌晨3点运行长时间自检
0 3 * * 0 /usr/local/bin/smart_schedule.sh
SMART overall-health: PASSEDSMART overall-health: FAILED# Ubuntu/Debian
sudo apt install smartmontools
# RHEL/CentOS
sudo yum install smartmontools
# Fedora
sudo dnf install smartmontools
# macOS (Homebrew)
brew install smartmontools
# 从源码编译
wget https://downloads.sourceforge.net/project/smartmontools/smartmontools/7.3/smartmontools-7.3.tar.gz
tar xzf smartmontools-7.3.tar.gz
cd smartmontools-7.3
./configure
make
sudo make install
# 列出所有硬盘设备
lsblk -d -o NAME,SIZE,MODEL,TYPE
# 检查设备是否支持SMART
for drive in /dev/sd[a-z]; do
if sudo smartctl -i "$drive" | grep -q "SMART support"; then
echo "$drive 支持SMART"
fi
done
A: 立即采取以下步骤:
smartctl -t long进行深度检查smartctl -l error错误日志A: NVMe使用不同的SMART标准,包含以下重要指标:
| 工具 | 主要功能 | 适用场景 |
|---|---|---|
| smartctl | 详细的SMART信息查询和自检 | 深入诊断和自动化监控 |
gsmartcontrol |
图形化界面,更易使用 | 桌面环境,初级用户 |
smartd |
后台守护进程,自动监控 | 服务器长期监控 |
badblocks |
物理坏块检测 | 详细的坏扇区检测 |
hdparm |
硬盘参数调整和测试 | 性能测试和参数调整 |