forked from Spearfoot/FreeNAS-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-system-temps.pl
329 lines (281 loc) · 7.54 KB
/
get-system-temps.pl
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/local/bin/perl
###############################################################################
#
# get-system-temps.pl
#
# Displays CPU and drive temperatures
#
# Drive information reported includes the device ID, temperature, capacity, type
# (SDD or HDD), serial number, model, and, if available, the model family.
#
# Optionally uses IPMI to report CPU temperatures. Otherwise, these are pulled
# from sysctl. IPMI is more accurate in that it reports the temperature of each
# socketed CPU in the system, even on virtualized instances, whereas the CPU
# temperatures typically aren't available from sysctl in this case.
#
# Requires the smartmontools, available at: https://www.smartmontools.org/
#
# Keith Nash, July 2017
#
###############################################################################
use strict;
use warnings;
# Get system's hostname:
my $hostname = qx(hostname);
chomp($hostname);
# Full path to the smartctl program:
my $smartctl = "/usr/local/sbin/smartctl";
# IPMI setup:
# Toggle IPMI support on or off:
# 1 = on: use IPMI
# 0 = off: use sysctl instead of IPMI
my $useipmi = 0;
# IPMI username and password file. The password file is a text file with the
# IPMI user's password on the first line. Be sure to set permissions to 0600
# on the password file.
#
# You may not need credentials on some systems. In this case, ignore these
# variables and modify the ipmitool variable below to suit your environment,
# removing the '-I lanplus' and user credential options (-U and -f) as needed.
my $ipmiuser = "root";
my $ipmipwfile = "/root/ipmi_password";
# The IPMI host must be either an IP address or a DNS-resolvable hostname. If you
# have multiple systems, leave the variable blank and edit the conditional below
# to specify the IPMI host according to the host on which you are running the script:
my $ipmihost = "";
if ($useipmi && $ipmihost eq "")
{
if ($hostname =~ /bandit/)
{
$ipmihost="falcon.ipmi.spearfoot.net"
}
elsif ($hostname =~ /boomer/)
{
$ipmihost="felix.ipmi.spearfoot.net"
}
elsif ($hostname =~ /bacon/)
{
$ipmihost="fritz.ipmi.spearfoot.net"
}
else
{
die "No IPMI host specified!\n"
}
}
# Full path to ipmitool program, including options and credentials:
my $ipmitool = "/usr/local/bin/ipmitool -I lanplus -H $ipmihost -U $ipmiuser -f $ipmipwfile";
main();
###############################################################################
#
# main
#
###############################################################################
sub main
{
printf("==========\n\n");
if ($useipmi)
{
printf("%s (IPMI host: %s)\n\n",$hostname,$ipmihost);
}
else
{
printf("%s\n\n",$hostname);
}
display_cpu_temps();
display_drive_info();
}
###############################################################################
#
# display_cpu_temps
#
###############################################################################
sub display_cpu_temps
{
my $temp;
my $cpucores=0;
if ($useipmi)
{
$cpucores = qx($ipmitool sdr | grep -c -i "cpu.*temp");
}
else
{
$cpucores = qx(sysctl -n hw.ncpu);
}
printf("=== CPU (%d) ===\n",$cpucores);
if ($useipmi)
{
if ($cpucores > 1)
{
for (my $core=1; $core <= $cpucores; $core++)
{
$temp=qx($ipmitool sdr | grep -i "CPU$core Temp" | awk '{print \$4}');
chomp($temp);
printf("CPU %2u: %3sC\n",$core,$temp);
}
}
else
{
$temp=qx($ipmitool sdr | grep -i "CPU Temp" | awk '{print \$4}');
chomp($temp);
printf("CPU %2u: %3sC\n",1,$temp);
}
}
else
{
for (my $core=0; $core < $cpucores; $core++)
{
$temp = qx(sysctl -n dev.cpu.$core.temperature);
chomp($temp);
if ($temp <= 0)
{
printf("CPU %2u: -N/A-\n",$core);
}
else
{
printf("CPU %2u: %3sC\n",$core,$temp);
}
}
}
}
###############################################################################
#
# display_drive_info
#
###############################################################################
sub display_drive_info
{
my $drive_id;
my $drive_model;
my $drive_family;
my $drive_serial;
my $drive_capacity;
my $drive_temp;
my $drive_is_ssd;
my $drive_family_display;
printf("\n=== Drives ===\n");
my @smart_drive_list = get_smart_drives();
foreach my $drive (@smart_drive_list)
{
($drive_model, $drive_family, $drive_serial, $drive_capacity, $drive_temp, $drive_is_ssd) = get_drive_info($drive);
if ($drive =~ /\/dev\/(.*)/)
{
$drive_id = $1;
}
else
{
$drive_id = $drive;
}
if ($drive_family eq "")
{
$drive_family_display = "";
}
else
{
$drive_family_display = "(" . $drive_family . ")";
}
printf("%6.6s: %3uC [%8.8s %s] %-20.20s %s %s\n",
$drive_id,
$drive_temp,
$drive_capacity,
$drive_is_ssd ? "SSD" : "HDD",
$drive_serial,
$drive_model,
$drive_family_display);
}
}
###############################################################################
#
# get_smart_drives
#
###############################################################################
sub get_smart_drives
{
my @retval = ();
my @drive_list = split(" ", qx($smartctl --scan | awk '{print \$1}'));
foreach my $drive (@drive_list)
{
my $smart_enabled = qx($smartctl -i $drive | grep "SMART support is: Enabled" | awk '{print \$4}');
chomp($smart_enabled);
if ($smart_enabled eq "Enabled")
{
push @retval, $drive;
}
}
return @retval;
}
###############################################################################
#
# get_drive_info
#
###############################################################################
sub get_drive_info
{
my $drive = shift;
my $smart_data = qx($smartctl -a $drive);
my $drive_model = "";
my $drive_family = "";
my $drive_serial = "";
my $drive_capacity = "";
my $drive_temp = 0;
my $drive_is_ssd = 0;
$drive_temp = get_drive_temp($drive);
# Serial number
if ($smart_data =~ /^Serial Number:\s*(.*)\s/m)
{
$drive_serial = $1;
}
# Device model
if ($smart_data =~ /^Device Model:\s*(.*)\s/m)
{
$drive_model = $1;
}
# Model family
if ($smart_data =~ /^Model Family:\s*(.*)\s/m)
{
$drive_family = $1;
}
# User capacity
if ($smart_data =~ /^User Capacity:.*\[(.*)\]\s/m)
{
$drive_capacity = $1;
}
# Determine if drive is a SSD
if ($smart_data =~ /^Rotation Rate:[ ]*Solid State Device/m)
{
$drive_is_ssd = 1;
}
elsif ($smart_data =~ /^[ 0-9]{3} Unknown_SSD_Attribute/m)
{
$drive_is_ssd = 1;
}
elsif ($smart_data =~ /^[ 0-9]{3} Wear_Leveling_Count/m)
{
$drive_is_ssd = 1;
}
elsif ($smart_data =~ /^[ 0-9]{3} Media_Wearout_Indicator/m)
{
$drive_is_ssd = 1;
}
elsif ($drive_family =~ /SSD/)
{
# Model family indicates SSD
$drive_is_ssd = 1;
}
return ($drive_model, $drive_family, $drive_serial, $drive_capacity, $drive_temp, $drive_is_ssd);
}
###############################################################################
#
# get_drive_temp
#
###############################################################################
sub get_drive_temp
{
my $drive = shift;
my $retval = 0;
$retval = qx($smartctl -A $drive | grep "194 Temperature" | awk '{print \$10}');
if (!$retval)
{
$retval = qx($smartctl -A $drive | grep "190 Airflow_Temperature" | awk '{print \$10}');
}
return $retval;
}