debian/ubuntu配置smartmontools监控硬盘健康状态
smartmontools的软件包程序,它通过使用自我监控(Self-Monitoring)、分析(Analysis)和报告(Reporting)三种技术(缩写为S.M.A.R.T或SMART)来管理和监控存储硬件。如今大部分的ATA/SATA、SCSI/SAS和固态硬盘都搭载内置的SMART系统。SMART的目的是监控硬盘的可靠性、预测磁盘故障和执行各种类型的磁盘自检。smartmontools由smartctl和smartd两部分工具程序组成,它们一起为Linux平台提供对磁盘退化和故障的高级警告。
这个文章,debian操作系统,使用smartmontools,监控系统硬盘状态,遇到问题发邮件报出来。
一,要解决的问题
在服务器管理的实际环境中,硬盘是最容易出现问题及发生故障的硬件,而且硬盘中存储着大量重要的数据,万一出现故障所造成的损失也是无法估计的,轻则需要化费大量的时间与精力去做数据恢复,重则硬盘报废,里面重要的数据也无法100%挽回,所以对硬盘健康状监控十分必要。
普通个人用户,如果使用了Raid或者其他备份技术,遭遇硬盘毁坏,也是需要付出大量代价的。
二, smartmontools
smartmontools的软件包程序,它通过使用自我监控(Self-Monitoring)、分析(Analysis)和报告(Reporting)三种技术(缩写为S.M.A.R.T或SMART)来管理和监控存储硬件。如今大部分的ATA/SATA、SCSI/SAS和固态硬盘都搭载内置的SMART系统。SMART的目的是监控硬盘的可靠性、预测磁盘故障和执行各种类型的磁盘自检。smartmontools由smartctl和smartd两部分工具程序组成,它们一起为Linux平台提供对磁盘退化和故障的高级警告。
三,配合发邮件
当前使用msmtp,配合smartmontools工具,当检测硬盘发现问题,邮件报错。
四,配置方式
A安装msmtp
使用root用户
1.apt install msmtp msmtp-mta
补充:如果系统没有mail 命令,说明没有邮件系统,需要装上,比如可以安装 bsd-mailx
2. 配置发邮件
编辑 /etc/msmtprc (如果非root用户,编辑 ~/.msmtprc)
如果只有一个,使用yaoo的话,参考内容
# Set default values for all following accounts.
# yahoo
defaults
account yahoo
tls on
tls_starttls off
tls_certcheck off
logfile /var/log/msmtp
auth on
host smtp.mail.yahoo.com
port 465
from ****@yahoo.com
user ****@yahoo.com
password *********
如果多个,参考
# Set default values for all following accounts.
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /home/xxx/.msmtp.log
#tls_starttls off
#tls_certcheck off
# Yandex
account yandex
host smtp.yandex.com
port 465
from [email protected]
user xxx
password xxx
# Hotmail
account hotmail
host smtp-mail.outlook.com
port 587
from [email protected]
user [email protected]
password xxxx
# Set a default account
account default : hotmail
补充:如果smtp不支持starttls,可以通过tls_starttls off并设置端口为465解决予以解决,提示证书错可以通过tls_certcheck off,
yandex太过简单的内容会被认为是垃圾邮件而拒绝投递,如果yandex不通最好换个别的。
3. 测试发邮件
echo "This is the mail body" | mail -s "Subject222" ***@mydomain.com
B安装smartmontools
1.安装
debian /ubuntu :apt install martmontools
readhat: yun install martmontools
2. 简单使用
列出硬盘
# ls -l /dev | grep -E 'sd|hd'
brw-rw---- 1 root disk 8, 0 Apr 30 15:41 sda
brw-rw---- 1 root disk 8, 1 Apr 30 15:41 sda1
brw-rw---- 1 root disk 8, 2 Apr 30 15:41 sda2
brw-rw---- 1 root disk 8, 3 May 24 20:59 sda3
brw-rw---- 1 root disk 8, 16 Apr 29 20:33 sdb
brw-rw---- 1 root disk 8, 17 Apr 29 20:33 sdb1
crw------- 1 root root 10, 130 Apr 29 20:33 watchdog
crw------- 1 root root 244, 0 Apr 29 20:33 watchdog0
检查硬盘是否支持SMART
1993年以后出厂的硬盘基本上都支持SMART技术,使用如下命令可以来查看
smartctl -i /dev/sda
smartctl 7.2 2020-12-30 r5155 [x86_64-linux-5.15.102-1-**] (local build)
Copyright (C) 2002-20, Bruce Allen, Christian Franke, www.smartmontools.org
=== START OF INFORMATION SECTION ===
Model Family: Western Digital Blue (SMR)
Device Model: WDC WD20EZAZ-00L9GB0
Serial Number: WD-WXP2A13FDPE1
LU WWN Device Id: 5 0014ee 26af3eb8c
Firmware Version: 80.00A80
User Capacity: 2,000,398,934,016 bytes [2.00 TB]
Sector Sizes: 512 bytes logical, 4096 bytes physical
Rotation Rate: 5400 rpm
Form Factor: 3.5 inches
TRIM Command: Available, deterministic, zeroed
Device is: In smartctl database [for details use: -P show]
ATA Version is: ACS-3 T13/2161-D revision 5
SATA Version is: SATA 3.1, 6.0 Gb/s (current: 6.0 Gb/s)
Local Time is: Thu Jun 1 20:40:05 2023 CST
SMART support is: Available - device has SMART capability.
SMART support is: Enabled
如果运行这个命令返回了类似“Device does not support SMART”的信息就说明目标硬盘不支持SMART,不能使用这套软件包进行测试和监控。
尽管最开始可能不会注意到ATA(译者注:硬盘接口技术)的版本信息,但当需要替换硬盘时它确实是最重要的因素之一。每一代ATA版本都保持向下兼容。例如,老的ATA-1或ATA-2设备可以正常工作在ATA-6和ATA-7接口上,但反过来就不行了。在设备版本和接口版本两者不匹配的情况下,它们会按照两者中版本较小的规范来运行。也就是说,在这种情况下,需要替换硬盘时,ATA-7硬盘是最安全的选择。
可以通过这个命令来检测某个硬盘的健康状况:
在这个命令中,"-s on"标志开启指定设备上的SMART功能。如果/dev/sda上已开启SMART支持,那就省略它。
硬盘的SMART信息包含很多部分。其中,"READ SMART DATA"部分显示出硬盘的整体健康状况。
=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED
这个测试的结果是PASSED或FAILED。后者表示即将出现硬件故障,所以需要开始备份这块磁盘上的重要数据!
还需要关注smart表
SMART Attributes Data Structure revision number: 16
Vendor Specific SMART Attributes with Thresholds:
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
1 Raw_Read_Error_Rate 0x002f 200 200 051 Pre-fail Always - 0
3 Spin_Up_Time 0x0027 175 175 021 Pre-fail Always - 2250
4 Start_Stop_Count 0x0032 100 100 000 Old_age Always - 6
5 Reallocated_Sector_Ct 0x0033 200 200 140 Pre-fail Always - 0
7 Seek_Error_Rate 0x002e 200 200 000 Old_age Always - 0
9 Power_On_Hours 0x0032 099 099 000 Old_age Always - 795
10 Spin_Retry_Count 0x0032 100 253 000 Old_age Always - 0
11 Calibration_Retry_Count 0x0032 100 253 000 Old_age Always - 0
12 Power_Cycle_Count 0x0032 100 100 000 Old_age Always - 5
192 Power-Off_Retract_Count 0x0032 200 200 000 Old_age Always - 0
193 Load_Cycle_Count 0x0032 200 200 000 Old_age Always - 5
194 Temperature_Celsius 0x0022 108 104 000 Old_age Always - 35
196 Reallocated_Event_Count 0x0032 200 200 000 Old_age Always - 0
197 Current_Pending_Sector 0x0032 200 200 000 Old_age Always - 0
198 Offline_Uncorrectable 0x0030 100 253 000 Old_age Offline - 0
199 UDMA_CRC_Error_Count 0x0032 200 200 000 Old_age Always - 0
200 Multi_Zone_Error_Rate 0x0008 100 253 000 Old_age Offline - 0
基本上,SMART属性表列出了制造商在硬盘中定义好的属性值,以及这些属性相关的故障阈值。这个表由驱动固件自动生成和更新。
其他:smartmontool简单使用命令
1.smartctl -a检查该设备是否已经打开SMART技术。
2.smartctl -s on如果没有打开SMART技术,使用该命令打开SMART技术。
3.smartctl -t short后台检测硬盘,消耗时间短;
smartctl -t long后台检测硬盘,消耗时间长;
smartctl -C -t short前台检测硬盘,消耗时间短;
smartctl -C -t long前台检测硬盘,消耗时间长。其实就是利用硬盘SMART的自检程序。
4.smartctl -X中断后台检测硬盘。
5. smartctl -l selftest显示硬盘检测日志。
6.smartctl -l error显示硬盘错误汇总。
3. 让smartmontolls间隔一定时间运行,并邮件通知结果
首先,编辑smartctl的配置文件(/etc/default/smartmontools)以便在系统启动时启动smartd,并以秒为单位指定间隔时间(如7200 = 2小时)。
start_smartd=yes
smartd_opts="--interval=7200"
下一步,编辑smartd的配置文件(/etc/smartd.conf),注释掉DEVICESCAN开头的命令,否则其下的命令都不会执行
然后添加以下行内容。
/dev/sda -H -m [email protected] -M test
其他参数看这个文件中注释的部分。
保存改动并重启smartd。
systemctl restart smartd
邮箱中会收到邮件
最后,可以使用“-s”标志和形如“T/MM/DD/d/HH”的正则表达式按照想要的调度方案执行测试 例如,想要在每个工作日的下午一点为所有的磁盘执行一次长测试,在/etc/smartd.conf中添加如下行内容。确保编辑完重启smartd。
DEVICESCAN -s (L/../../[1-5]/13)
参考
How to check hard disk health on Linux using smartmontools
中文版