forked from Spearfoot/FreeNAS-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_hdd_temp.sh
142 lines (119 loc) · 4.59 KB
/
get_hdd_temp.sh
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
#!/bin/sh
# Display current temperature of CPU(s) and all SMART-enabled drives
# Optionally uses IPMI to report temperatures of the system CPU(s)
#
# If IPMI is disabled (see 'use_ipmi' below) then the script uses
# sysctl to report the CPU temperatures. To use IPMI, you must
# provide the IPMI host, user name, and user password file.
# Full path to 'smartctl' program:
smartctl=/usr/local/sbin/smartctl
# IPMI support: set to a postive value to use IPMI for CPU temp
# reporting, set to zero to disable IPMI and use 'sysctl' instead:
use_ipmi=0
# IP address or DNS-resolvable hostname of IPMI server:
ipmihost=192.168.1.x
# IPMI username:
ipmiuser=root
# IPMI password file. This is a file containing the IPMI user's password
# on a single line and should have 0600 permissions:
ipmipwfile=/root/ipmi_password
# Full path to 'ipmitool' program:
ipmitool=/usr/local/bin/ipmitool
# We need a list of the SMART-enabled drives on the system. Choose one of these
# three methods to provide the list. Comment out the two unused sections of code.
# 1. A string constant; just key in the devices you want to report on here:
#drives="da1 da2 da3 da4 da5 da6 da7 da8 ada0"
# 2. A systcl-based technique suggested on the FreeNAS forum:
#drives=$(for drive in $(sysctl -n kern.disks); do \
#if [ "$(/usr/local/sbin/smartctl -i /dev/${drive} | grep "SMART support is: Enabled" | awk '{print $3}')" ]
#then printf ${drive}" "; fi done | awk '{for (i=NF; i!=0 ; i--) print $i }')
# 3. A smartctl-based function:
get_smart_drives()
{
gs_smartdrives=""
gs_drives=$("$smartctl" --scan | awk '{print $1}')
for gs_drive in $gs_drives; do
gs_smart_flag=$("$smartctl" -i "$gs_drive" | egrep "SMART support is:[[:blank:]]+Enabled" | awk '{print $4}')
if [ "$gs_smart_flag" = "Enabled" ]; then
gs_smartdrives="$gs_smartdrives $gs_drive"
fi
done
echo "$gs_smartdrives"
}
drives=$(get_smart_drives)
# end of method 3.
#############################
# CPU temperatures:
#############################
if [ "$use_ipmi" -eq 0 ]; then
cpucores=$(sysctl -n hw.ncpu)
printf '=== CPU (%s) ===\n' "$cpucores"
cpucores=$((cpucores - 1))
for core in $(seq 0 $cpucores); do
temp=$(sysctl -n dev.cpu."$core".temperature|sed 's/\..*$//g')
if [ "$temp" -lt 0 ]; then
temp="--n/a--"
else
temp="${temp}C"
fi
printf 'CPU %2.2s: %5s\n' "$core" "$temp"
done
echo ""
else
cpucores=$("$ipmitool" -I lanplus -H "$ipmihost" -U "$ipmiuser" -f "$ipmipwfile" sdr elist all | grep -c -i "cpu.*temp")
printf '=== CPU (%s) ===\n' "$cpucores"
if [ "$cpucores" -eq 1 ]; then
temp=$("$ipmitool" -I lanplus -H "$ipmihost" -U "$ipmiuser" -f "$ipmipwfile" sdr elist all | grep "CPU Temp" | awk '{print $10}')
if [ "$temp" -lt 0 ]; then
temp="-n/a-"
else
temp="${temp}C"
fi
printf 'CPU %2s: %5s\n' "$core" "$temp"
else
for core in $(seq 1 "$cpucores"); do
temp=$("$ipmitool" -I lanplus -H "$ipmihost" -U "$ipmiuser" -f "$ipmipwfile" sdr elist all | grep "CPU${core} Temp" | awk '{print $10}')
if [ "$temp" -lt 0 ]; then
temp="-n/a-"
else
temp="${temp}C"
fi
printf 'CPU %2s: [%s]\n' "$core" "$temp"
done
fi
echo ""
fi
#############################
# Drive temperatures:
#############################
echo "=== DRIVES ==="
for drive in $drives; do
serial=$("$smartctl" -i "$drive" | grep -i "serial number" | awk '{print $NF}')
capacity=$("$smartctl" -i "$drive" | grep "User Capacity" | awk '{print $5 $6}')
temp=$("$smartctl" -A "$drive" | grep "194 Temperature" | awk '{print $10}')
if [ -z "$temp" ]; then
temp=$("$smartctl" -A "$drive" | grep "190 Airflow_Temperature" | awk '{print $10}')
fi
if [ -z "$temp" ]; then
temp=$("$smartctl" -A "$drive" | grep "Current Drive Temperature" | awk '{print $4}')
fi
if [ -z "$temp" ]; then
temp="-n/a-"
else
temp="${temp}C"
fi
dfamily=$("$smartctl" -i "$drive" | grep "Model Family" | awk '{print $3, $4, $5, $6, $7}' | sed -e 's/[[:space:]]*$//')
dmodel=$("$smartctl" -i "$drive" | grep "Device Model" | awk '{print $3, $4, $5, $6, $7}' | sed -e 's/[[:space:]]*$//')
if [ -z "$dfamily" ]; then
dinfo="$dmodel"
else
dinfo="$dfamily ($dmodel)"
fi
if [ -z "$dfamily" ]; then
vendor=$("$smartctl" -i "$drive" | grep "Vendor:" | awk '{print $NF}')
product=$("$smartctl" -i "$drive" | grep "Product:" | awk '{print $NF}')
revision=$("$smartctl" -i "$drive" | grep "Revision:" | awk '{print $NF}')
dinfo="$vendor $product $revision"
fi
printf '%6.6s: %5s %-8s %-20.20s %s\n' "$(basename "$drive")" "$temp" "$capacity" "$serial" "$dinfo"
done