硬盘 SMART 检测与预警配置指南
一、SMART 简介
SMART (Self-Monitoring, Analysis and Reporting Technology) 是硬盘内置的健康监测技术,可提前预警硬盘故障。
关键指标:
Reallocated_Sector_Ct:重映射扇区数(>0 需警惕)
Current_Pending_Sector:待映射扇区数(>0 表示有坏道风险)
Offline_Uncorrectable:无法校正的错误数
Power_On_Hours:通电时间
Temperature_Celsius:工作温度
Wear_Leveling_Count:SSD 磨损程度
1 2 3 4 5 6 7 8
| yum install -y smartmontools
apt-get install -y smartmontools
smartctl --version
|
三、查看硬盘 SMART 信息
3.1 查看硬盘列表
1 2 3 4 5
| lsblk
smartctl --scan
|
3.2 查看 SMART 健康状态
1 2 3 4 5 6 7 8
| smartctl -H /dev/sda
smartctl -a /dev/sda
smartctl -a /dev/sda > /var/log/smart/sda_$(date +%Y%m%d).log
|
3.3 关键输出解读
1 2 3 4 5 6
| SMART overall-health self-assessment test result: PASSED
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE 5 Reallocated_Sector_Ct 0x0033 100 100 010 Pre-fail Always - 0 197 Current_Pending_Sector 0x0012 100 100 000 Old_age Always - 0 198 Offline_Uncorrectable 0x0010 100 100 000 Old_age Offline - 0
|
重点关注:
WHEN_FAILED 列显示 - 表示正常,显示 FAILING_NOW 表示即将故障
RAW_VALUE 为 0 表示正常,非 0 需关注
VALUE 低于 THRESH 表示硬盘已故障
四、配置自动检测
4.1 启用 smartd 服务
1 2 3 4 5 6 7 8
| vim /etc/smartmontools/smartd.conf
DEVICESCAN -H -m admin@example.com -M exec /usr/share/doc/smartmontools/DEVICESCAN
/dev/sda -H -l error -l selftest -s (S/../.././02|L/../../6/03) -m admin@example.com
|
参数说明:
-H:监控健康状态
-l error:监控错误日志
-l selftest:监控自检结果
-s:定期执行自检(S=短检,L=长检)
-m:告警邮件接收者
4.2 配置邮件通知(可选)
1 2 3 4 5
| MAILFROM="smartd-alert@server.local" SMARTD_ARGS="-m admin@example.com"
|
4.3 启动服务
1 2 3 4 5 6 7 8 9
| systemctl enable smartd systemctl start smartd
systemctl status smartd
journalctl -u smartd -f
|
五、手动执行硬盘自检
1 2 3 4 5 6 7 8
| smartctl -t short /dev/sda
smartctl -t long /dev/sda
smartctl -l selftest /dev/sda
|
自检结果示例:
1 2 3 4
| SMART Self-test log structure revision number 1 Num Test_Description Status Remaining LifeTime(hours) LBA_of_first_error # 1 Short offline Completed without error 00% 1234 - # 2 Extended offline Completed: read failure 90% 5678 12345678
|
六、监控脚本示例
6.1 简易检查脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #!/bin/bash
DISKS=$(lsblk -d -n -o NAME | grep sd) LOG_FILE="/var/log/smart_check.log"
for disk in $DISKS; do echo "=== Checking /dev/$disk at $(date) ===" >> $LOG_FILE HEALTH=$(smartctl -H /dev/$disk 2>/dev/null | grep "result:" | awk '{print $NF}') if [ "$HEALTH" != "PASSED" ]; then echo "WARNING: /dev/$disk SMART check FAILED: $HEALTH" >> $LOG_FILE fi REALLOCATED=$(smartctl -A /dev/$disk 2>/dev/null | grep "Reallocated_Sector_Ct" | awk '{print $NF}') if [ "$REALLOCATED" -gt 0 ] 2>/dev/null; then echo "WARNING: /dev/$disk has $REALLOCATED reallocated sectors" >> $LOG_FILE fi done
|
6.2 配置定时任务
1 2 3 4 5
| crontab -e
0 2 * * * /usr/local/bin/smart_check.sh
|
七、SSD 特殊注意事项
SSD 除了常规 SMART 指标外,还需关注:
1 2
| smartctl -a /dev/sda | grep -E "Wear|Percentage|TBW"
|
关键指标:
Wear_Leveling_Count:磨损均衡计数(越低越危险)
Used_Rsvd_Blk_Cnt_Tot:已用保留块总数
Total_LBAs_Written:总写入量(换算 TBW)
SSD 寿命估算:
八、故障处理流程
1 2 3 4 5 6 7 8
| SMART 告警 ├── 立即备份数据(最高优先级) ├── 查看具体故障指标 │ ├── Reallocated_Sector > 0 → 有坏道,计划更换 │ ├── Pending_Sector > 0 → 有坏道风险,密切监控 │ └── SMART health FAILED → 立即更换 ├── 执行长自检确认 └── 联系供应商保修(如在保)
|
九、最佳实践
- 定期检查:生产环境至少每周检查一次 SMART 状态
- 监控告警:配置 smartd 自动监控 + 邮件/短信告警
- 提前更换:发现重映射扇区 > 0 即计划更换,不要等到完全故障
- 保留日志:定期归档 SMART 日志,便于趋势分析
- 备件管理:关键业务服务器应备有硬盘备件
十、总结
SMART 是硬盘故障预警的第一道防线。正确配置 smartd 并定期分析 SMART 数据,可以在硬盘完全故障前提前发现隐患,避免数据丢失和业务中断。
参考命令速查:
1 2 3 4 5
| smartctl -H /dev/sda smartctl -a /dev/sda smartctl -t short /dev/sda smartctl -l selftest /dev/sda systemctl status smartd
|