I'm trying to make a shell script to use as an HDD temperature alert job with cron. The cron job must make these things:
- Verify the existing ATA devices (I'm using camcontrol);
- Verify the temperature of the devices identified (I'm using smartctl);
- Compare the temperatures of the ATA devices with a constant variable;
- If one of the devices temperature is highter than the constant variable, the script sends an email with the temperature of all ATA devices.
My problem is how to use the results of item 2 to accomplish the item 3...
My actual code is:
# Defines alert temperature
TEMP_MAX=30 #Graus celcius
# Create an array with devices
arrdiscs=($(camcontrol devlist | awk '{print substr($NF, 8, length($NF)-8)}'))
# Get the temperature of the devices
for i in "${arrdiscs[@]}"
do
# Get temperatures
TEMP="$(smartctl -A /dev/$i | egrep ^194 | awk '{print $10}')"
done
I can echo the devices names and the temperatures if I use:
for i in "${arrdiscs[@]}"
do
TEMP="$(smartctl -A /dev/$i | egrep ^194 | awk '{print $10}')"
echo "[$i]: $TEMP"
done
I tried (and many others ways):
for i in "${arrdiscs[@]}"
do
TEMP="$(smartctl -A /dev/$i | egrep ^194 | awk '{print $10}')"
echo "[$i]: $TEMP"
done
echo "ARRAY DISCOS: ${arrdiscs[*]}"
echo "TEMPERATURAS: ${TEMP[*]}"
The above code print the "arrdiscs" correctly but the "TEMP" prints null.
So... How can I create an array based on the TEMP variable so I can verify wich temperature is highter then TEMP_MAX?
Made some changes as suggested by Etan Reisner:
# Defines alert temperature
temp_max=20 #Graus celcius
# Monta array de discos do sistema
arrdiscs=($(camcontrol devlist | awk '{print substr($NF, 8, length($NF)-8)}'))
echo "${arrdiscs[@]}"
alertdiscs=()
for i in "${arrdiscs[@]}"
do
temp=("$(smartctl -A "/dev/$i" | egrep ^194 | awk '{print $10}')")
if [ -z "$temp" ]; then
echo "\$temp is empty"
else
if [ $temp -gt $temp_max ]; then
alertdiscs+=("[$i]: $temp")
echo ${temp[@]}
fi
fi
done
if [ ${#alertdics[@]} -gt 0 ]; then
echo "The following discs were too hot:"
printf '%s\n' "${alertdiscs[@]}"
else
echo "${alertdiscs[@]}"
fi
But I got the result as follow:
ada0 ada1 ada2 ada3 da0
33
31
32
30
$temp is empty
[ada0]: 33 [ada1]: 31 [ada2]: 32 [ada3]: 30
The variable temp_max was set to 20, but no "The following discs were too hot:" message displayed... :(