前言:在 TrueNAS SCALE 上面监控磁盘温度挺简单,但是批量查看磁盘温度操作颇为繁琐,本文提供一个相对简单的脚本,方便用户使用。

1. 确定磁盘入口·

在 Linux 系统中一切都是文件,因此独立的磁盘也被认为是文件。通过查看 /dev/disk/by-id 目录(下称 by-id 目录)可以知道目前接入了哪些物理磁盘。by-id 目录里的文件,大多以如下前缀命名:

  • nvme-,表明这是 NVMe 固态硬盘
  • ata-,表明这是 SATA 硬盘
  • scsi-,表明这是 SAS 硬盘
  • wwn-,World Wide Naming,一般用来给 SATA 和 SAS 硬盘取独立串号的时候使用。

/dev/disk/by-id 目录下存放的均是软链接,所有设备均指向 /dev 目录下的块设备描述文件。

注意,使用 CrystakDiskInfo 无法识别 SAS 硬盘。

2. 生成 Python3 脚本·

使用 Python 进行运维管理是非常方便的,因为 Python 的数据结构比较高级,因此编程时更符合人的逻辑思维。Bash/Zsh 的数据结构太过简陋,要实现较为复杂的逻辑只能依赖 sedawk 等字符串编辑器进行操作,非常不便。

将系统接入的磁盘在 by-id 目录中的名字记录下来,写到下述脚本对应的数组里,即可通过执行脚本获取温度信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3

import subprocess
import colorama
from colorama import Fore, Back, Style

colorama.init()

def run_smartctl(disk_id, filter_keyword=None, exclude_keyword=None, is_nvme=False):
if is_nvme is True:
result = subprocess.check_output(f'sudo nvme smart-log /dev/disk/by-id/{disk_id}', shell=True).decode('utf-8')
else:
result = subprocess.check_output(f'sudo smartctl -a /dev/disk/by-id/{disk_id}', shell=True).decode('utf-8')

lines = result.splitlines()
if filter_keyword:
lines = [line for line in lines if filter_keyword.lower() in line.lower()]
if exclude_keyword:
lines = [line for line in lines if exclude_keyword.lower() not in line.lower()]
return '\n'.join(lines)

def print_group_temp(title, disks, keyword="Temperature", exclude=None, is_nvme=False):
print(Fore.BLACK + Back.YELLOW + f"💡 {title} Group Temperature:" + Style.RESET_ALL)
for disk_id in disks:
print(f"{disk_id}:")
output = run_smartctl(disk_id, filter_keyword=keyword, exclude_keyword=exclude, is_nvme=is_nvme)
print(output)
print('-' * max([len(line) for line in output.splitlines()]))




DapuStor_R5100_NVMes = [
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23300B3G9",
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23400BZ0J",
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23500C6CA",
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23500C6MY",
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23500C737",
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23A00GALG",
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51B236000213",
"nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51B236000220"
]
print_group_temp("DapuStor R5100 NVMe", DapuStor_R5100_NVMes, is_nvme=True)
print("\n" * 2)

intel_Optane_900P_NVMes = ["nvme-INTEL_SSDPE21D280GA_PHM28105018M280AGN"]
print_group_temp(title="Intel Optane 900P", disks=intel_Optane_900P_NVMes, is_nvme=True)
print("\n" * 2)

WD_HC550_SATAs = [
"ata-WDC_WUH721818ALE6L4_2JJRHM8B",
"ata-WDC_WUH721818ALE6L4_2JJV06YL",
"ata-WDC_WUH721818ALE6L4_2JJWHLPL",
"ata-WDC_WUH721818ALE6L4_2VGJV5DA",
"ata-WDC_WUH721818ALE6L4_2VGKMM0L",
"ata-WDC_WUH721818ALE6L4_2VGL7RYA",
"ata-WDC_WUH721818ALE6L4_2VH9S91A",
"ata-WDC_WUH721818ALE6L4_3FHDMUTT",
"ata-WDC_WUH721818ALE6L4_3WGRYZJJ",
"ata-WDC_WUH721818ALE6L4_3WGS00DJ",
"ata-WDC_WUH721818ALE6L4_3WJDSE1K",
"ata-WDC_WUH721818ALE6L4_4BKMN0LY",
"ata-WDC_WUH721818ALE6L4_4BKNLESH",
"ata-WDC_WUH721818ALE6L4_4BKY72JZ",
"ata-WDC_WUH721818ALE6L4_4EG3BVPY",
"ata-WDC_WUH721818ALE6L4_5DGHEM6J",
"ata-WDC_WUH721818ALE6L4_5DGPBXWJ",
"ata-WDC_WUH721818ALE6L4_5DGPHYHJ",
"ata-WDC_WUH721818ALE6L4_6PGEE13U",
"ata-WDC_WUH721818ALE6L4_6TGXA6XF",
"ata-WDC_WUH721818ALE6L4_6TH0H4RF",
"ata-WDC_WUH721818ALE6L4_6TH0J73F",
"ata-WDC_WUH721818ALE6L4_6TH0M0MF",
"ata-WDC_WUH721818ALE6L4_6TH0M3KF"
]
print_group_temp("WD HC550", WD_HC550_SATAs, keyword="Celsius")
print("\n" * 2)

Toshiba_MG06S_SASs = [
"scsi-3500003992822e315",
"scsi-3500003992822e33d",
"scsi-3500003992823697d",
"scsi-350000399282392e5"
]
print_group_temp("Toshiba MG06S", Toshiba_MG06S_SASs, keyword="Current Drive Temperature:")

3. 查看温度·

将上述文件保存到 ~/bin/get-temp3.py,并将 ~/bin 添加到 PATH 环境变量,即可在命令行进行调用。

输出结果大概如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23300B3G9:
temperature : 42°C (315 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 54°C (327 Kelvin)
Temperature Sensor 2 : 39°C (312 Kelvin)
Temperature Sensor 3 : 39°C (312 Kelvin)
Temperature Sensor 4 : 38°C (311 Kelvin)
Temperature Sensor 5 : 39°C (312 Kelvin)
--------------------------------------------------
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23400BZ0J:
temperature : 36°C (309 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 46°C (319 Kelvin)
Temperature Sensor 2 : 34°C (307 Kelvin)
Temperature Sensor 3 : 33°C (306 Kelvin)
Temperature Sensor 4 : 34°C (307 Kelvin)
Temperature Sensor 5 : 34°C (307 Kelvin)
--------------------------------------------------
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23500C6CA:
temperature : 41°C (314 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 52°C (325 Kelvin)
Temperature Sensor 2 : 39°C (312 Kelvin)
Temperature Sensor 3 : 38°C (311 Kelvin)
Temperature Sensor 4 : 37°C (310 Kelvin)
Temperature Sensor 5 : 40°C (313 Kelvin)
--------------------------------------------------
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23500C6MY:
temperature : 39°C (312 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 50°C (323 Kelvin)
Temperature Sensor 2 : 37°C (310 Kelvin)
Temperature Sensor 3 : 38°C (311 Kelvin)
Temperature Sensor 4 : 36°C (309 Kelvin)
Temperature Sensor 5 : 39°C (312 Kelvin)
--------------------------------------------------
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23500C737:
temperature : 34°C (307 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 45°C (318 Kelvin)
Temperature Sensor 2 : 32°C (305 Kelvin)
Temperature Sensor 3 : 32°C (305 Kelvin)
Temperature Sensor 4 : 31°C (304 Kelvin)
Temperature Sensor 5 : 32°C (305 Kelvin)
--------------------------------------------------
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51A23A00GALG:
temperature : 39°C (312 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 50°C (323 Kelvin)
Temperature Sensor 2 : 35°C (308 Kelvin)
Temperature Sensor 3 : 36°C (309 Kelvin)
Temperature Sensor 4 : 36°C (309 Kelvin)
Temperature Sensor 5 : 36°C (309 Kelvin)
--------------------------------------------------
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51B236000213:
temperature : 42°C (315 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 54°C (327 Kelvin)
Temperature Sensor 2 : 40°C (313 Kelvin)
Temperature Sensor 3 : 40°C (313 Kelvin)
Temperature Sensor 4 : 39°C (312 Kelvin)
Temperature Sensor 5 : 40°C (313 Kelvin)
--------------------------------------------------
nvme-DAPUSTOR_DPRD3108T0T507T6000_RS5U51B236000220:
temperature : 38°C (311 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
Temperature Sensor 1 : 51°C (324 Kelvin)
Temperature Sensor 2 : 36°C (309 Kelvin)
Temperature Sensor 3 : 36°C (309 Kelvin)
Temperature Sensor 4 : 36°C (309 Kelvin)
Temperature Sensor 5 : 37°C (310 Kelvin)
--------------------------------------------------



💡 Intel Optane 900P Group Temperature:
nvme-INTEL_SSDPE21D280GA_PHM28105018M280AGN:
temperature : 35°C (308 Kelvin)
Warning Temperature Time : 0
Critical Composite Temperature Time : 0
---------------------------------------



💡 WD HC550 Group Temperature:
ata-WDC_WUH721818ALE6L4_2JJRHM8B:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 20/37)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_2JJV06YL:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 21/37)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_2JJWHLPL:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 19/36)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_2VGJV5DA:
194 Temperature_Celsius 0x0002 062 062 000 Old_age Always - 33 (Min/Max 22/37)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_2VGKMM0L:
194 Temperature_Celsius 0x0002 060 060 000 Old_age Always - 34 (Min/Max 22/38)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_2VGL7RYA:
194 Temperature_Celsius 0x0002 062 062 000 Old_age Always - 33 (Min/Max 21/38)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_2VH9S91A:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 18/36)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_3FHDMUTT:
194 Temperature_Celsius 0x0002 064 064 000 Old_age Always - 31 (Min/Max 16/55)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_3WGRYZJJ:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 16/55)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_3WGS00DJ:
194 Temperature_Celsius 0x0002 064 064 000 Old_age Always - 31 (Min/Max 16/54)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_3WJDSE1K:
194 Temperature_Celsius 0x0002 064 064 000 Old_age Always - 31 (Min/Max 17/53)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_4BKMN0LY:
194 Temperature_Celsius 0x0002 065 065 000 Old_age Always - 30 (Min/Max 23/34)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_4BKNLESH:
194 Temperature_Celsius 0x0002 066 066 000 Old_age Always - 29 (Min/Max 22/35)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_4BKY72JZ:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 19/35)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_4EG3BVPY:
194 Temperature_Celsius 0x0002 062 062 000 Old_age Always - 33 (Min/Max 22/52)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_5DGHEM6J:
194 Temperature_Celsius 0x0002 062 062 000 Old_age Always - 33 (Min/Max 22/53)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_5DGPBXWJ:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 22/52)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_5DGPHYHJ:
194 Temperature_Celsius 0x0002 060 060 000 Old_age Always - 34 (Min/Max 22/53)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_6PGEE13U:
194 Temperature_Celsius 0x0002 060 060 000 Old_age Always - 34 (Min/Max 22/37)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_6TGXA6XF:
194 Temperature_Celsius 0x0002 065 065 000 Old_age Always - 30 (Min/Max 22/35)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_6TH0H4RF:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 19/36)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_6TH0J73F:
194 Temperature_Celsius 0x0002 062 062 000 Old_age Always - 33 (Min/Max 18/35)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_6TH0M0MF:
194 Temperature_Celsius 0x0002 063 063 000 Old_age Always - 32 (Min/Max 18/35)
---------------------------------------------------------------------------------------------------------
ata-WDC_WUH721818ALE6L4_6TH0M3KF:
194 Temperature_Celsius 0x0002 065 065 000 Old_age Always - 30 (Min/Max 22/34)
---------------------------------------------------------------------------------------------------------



💡 Toshiba MG06S Group Temperature:
scsi-3500003992822e315:
Current Drive Temperature: 31 C
-----------------------------------
scsi-3500003992822e33d:
Current Drive Temperature: 31 C
-----------------------------------
scsi-3500003992823697d:
Current Drive Temperature: 30 C
-----------------------------------
scsi-350000399282392e5:
Current Drive Temperature: 29 C
-----------------------------------

总结·

使用 grep 命令对 smartctl 的输出结果进行过滤算是比较简陋的方式,但是它确实行之有效。

PyPI 上面也有一个 pySMART 库,能将 S.M.A.R.T. 信息进行结构化处理,毕竟运维领域用 Python 替代 shell 处理复杂流程已经是大趋势,因此也推荐本文的读者使用 Python 进行监控。