-
Notifications
You must be signed in to change notification settings - Fork 15
/
checkTemp.sh
144 lines (112 loc) · 4.57 KB
/
checkTemp.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
142
143
#!/bin/sh
#############################################################################
# Check the CPU and HDD temperatures, and send e-mail
# if desired limits are exceeded
#
# Author: miGi from NAS4Free Forum
#
# Usage: checkTemp.sh thresholdCPU thresholdHDD
#
# thresholdCPU : Warning temperature threshold to the CPU (in deg celsius)
# thresholdHDD : Warning temperature threshold to the HDD (in deg celsius)
#############################################################################
# Initialization of the script name
readonly SCRIPT_NAME=`basename $0` # The name of this file
# set script path as working directory
cd "`dirname $0`"
# Import required scripts
. "config.sh"
. "common/commonLogFcts.sh"
. "common/commonMailFcts.sh"
. "common/commonLockFcts.sh"
# Initialization of the constants
readonly START_TIMESTAMP=`$BIN_DATE +"%s"`
readonly LOGFILE="$CFG_LOG_FOLDER/$SCRIPT_NAME.log"
readonly TMPFILE_ARGS="$CFG_TMP_FOLDER/$SCRIPT_NAME.$$.args.tmp"
# Set variables corresponding to the input parameters
ARGUMENTS="$@"
##################################
# Check script input parameters
#
# Params: all parameters of the shell script
# return : 1 if an error occured, 0 otherwise
##################################
parseInputParams() {
local opt regex_temp
# parse the optional parameters
# (there should be none)
while getopts ":" opt; do
case $opt in
\?)
echo "Invalid option: -$OPTARG"
return 1 ;;
esac
done
# Remove the optional arguments parsed above.
shift $((OPTIND-1))
# Check if the number of mandatory parameters
# provided is as expected
if [ "$#" -ne "2" ]; then
echo "$LOGFILE" "Exactly two mandatory argument shall be provided"
return 1
fi
# Set variables corresponding to the input parameters
I_WARN_THRESHOLD_CPU="$1"
I_WARN_THRESHOLD_HDD="$2"
regex_temp="([0-9]+)"
if ! echo "$I_WARN_THRESHOLD_CPU" | grep -E "^$regex_temp$" >/dev/null; then
echo "$LOGFILE" "Wrong CPU temperature notification threshold definition !"
return 1
fi
if ! echo "$I_WARN_THRESHOLD_HDD" | grep -E "^$regex_temp$" >/dev/null; then
echo "$LOGFILE" "Wrong HDD temperature notification threshold definition !"
return 1
fi
return 0
}
##################################
# Main
##################################
main() {
returnCode=0
log_info "$LOGFILE" "Starting checking temperatures..."
log_info "$LOGFILE" "CPUs (warning threshold: $((I_WARN_THRESHOLD_CPU))C):"
printf '%8s %s\n' "Temp(C)" "CPU" | log_info "$LOGFILE"
for cpu in `sysctl -a | grep -E "cpu\.[0-9]+\.temp" | cut -f1 -d:`; do
cpuTemp=`sysctl -a | grep $cpu | awk '{gsub(/[.][0-9]*C/,"");print $2}'`
printf '%+8d %s\n' "$((cpuTemp))" "$cpu" | log_info "$LOGFILE"
if [ "$((cpuTemp))" -ge "$I_WARN_THRESHOLD_CPU" ] ; then
log_warning "$LOGFILE" "CPU notification threshold reached !"
returnCode=1
fi
done
log_info "$LOGFILE" "HDDs (warning threshold: $((I_WARN_THRESHOLD_HDD))C):"
printf '%8s %-6s %-25s %s\n' "Temp(C)" "dev" "P/N" "S/N" | log_info "$LOGFILE"
for hdd in $(sysctl -n kern.disks); do
devTemp=`$BIN_SMARTCTL -a /dev/$hdd | grep "Temperature_Celsius" | awk '{print $10}'`
devSerNum=`$BIN_SMARTCTL -a /dev/$hdd | grep "^Serial Number:" | sed 's/^Serial Number:[ \t]*\(.*\)[ \t]*$/\1/g'`
devName=`$BIN_SMARTCTL -a /dev/$hdd | grep "^Device Model:" | sed 's/^Device Model:[ \t]*\(.*\)[ \t]*$/\1/g'`
printf '%+8d %-6s %-25s %s\n' "$((devTemp))" "$hdd" "$devName" "$devSerNum" \
| log_info "$LOGFILE"
if [ "$((devTemp))" -ge "$I_WARN_THRESHOLD_HDD" ] ; then
log_warning "$LOGFILE" "HDD notification threshold reached !"
returnCode=1
fi
done
return $returnCode
}
# Parse and validate the input parameters
if ! parseInputParams $ARGUMENTS > "$TMPFILE_ARGS"; then
log_info "$LOGFILE" "-------------------------------------"
cat "$TMPFILE_ARGS" | log_error "$LOGFILE"
get_log_entries_ts "$LOGFILE" "$START_TIMESTAMP" | sendMail "$SCRIPT_NAME : Invalid arguments"
else
log_info "$LOGFILE" "-------------------------------------"
cat "$TMPFILE_ARGS" | log_info "$LOGFILE"
# run script if possible (lock not existing)
run_main "$LOGFILE" "$SCRIPT_NAME"
# in case of error, send mail with extract of log file
[ "$?" -eq "2" ] && get_log_entries_ts "$LOGFILE" "$START_TIMESTAMP" | sendMail "$SCRIPT_NAME : issue occured during execution"
fi
$BIN_RM "$TMPFILE_ARGS"
exit 0