硬盘 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 磨损程度

二、安装 smartmontools

1
2
3
4
5
6
7
8
# CentOS/RHEL
yum install -y smartmontools

# Ubuntu/Debian
apt-get install -y smartmontools

# 验证安装
smartctl --version

三、查看硬盘 SMART 信息

3.1 查看硬盘列表

1
2
3
4
5
# 列出所有硬盘
lsblk

# 或使用 smartctl 扫描
smartctl --scan

3.2 查看 SMART 健康状态

1
2
3
4
5
6
7
8
# 快速健康检查
smartctl -H /dev/sda

# 查看完整 SMART 信息
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
# 编辑 smartd 配置,指定邮件参数
MAILFROM="smartd-alert@server.local"
SMARTD_ARGS="-m admin@example.com"

# 确保系统能发送邮件(配置 postfix 或使用外部 SMTP)

4.3 启动服务

1
2
3
4
5
6
7
8
9
# 启动 smartd
systemctl enable smartd
systemctl start smartd

# 查看状态
systemctl status smartd

# 查看日志
journalctl -u smartd -f

五、手动执行硬盘自检

1
2
3
4
5
6
7
8
# 执行短自检(约 2 分钟)
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
# /usr/local/bin/smart_check.sh

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
# 每天凌晨 2 点执行检查
crontab -e

# 添加
0 2 * * * /usr/local/bin/smart_check.sh

七、SSD 特殊注意事项

SSD 除了常规 SMART 指标外,还需关注:

1
2
# 查看 SSD 特有指标
smartctl -a /dev/sda | grep -E "Wear|Percentage|TBW"

关键指标:

  • Wear_Leveling_Count:磨损均衡计数(越低越危险)
  • Used_Rsvd_Blk_Cnt_Tot:已用保留块总数
  • Total_LBAs_Written:总写入量(换算 TBW)

SSD 寿命估算:

1
2
# 假设 SSD 标称 TBW=300,已写入量可从 SMART 获取
# 剩余寿命 ≈ (TBW - 已写入 TB) / TBW × 100%

八、故障处理流程

1
2
3
4
5
6
7
8
SMART 告警
├── 立即备份数据(最高优先级)
├── 查看具体故障指标
│ ├── Reallocated_Sector > 0 → 有坏道,计划更换
│ ├── Pending_Sector > 0 → 有坏道风险,密切监控
│ └── SMART health FAILED → 立即更换
├── 执行长自检确认
└── 联系供应商保修(如在保)

九、最佳实践

  1. 定期检查:生产环境至少每周检查一次 SMART 状态
  2. 监控告警:配置 smartd 自动监控 + 邮件/短信告警
  3. 提前更换:发现重映射扇区 > 0 即计划更换,不要等到完全故障
  4. 保留日志:定期归档 SMART 日志,便于趋势分析
  5. 备件管理:关键业务服务器应备有硬盘备件

十、总结

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 # 服务状态