-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempsd
More file actions
executable file
·488 lines (459 loc) · 12.1 KB
/
tempsd
File metadata and controls
executable file
·488 lines (459 loc) · 12.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/bin/sh
### BEGIN INIT INFO
# Short-Description: Hardware temperature monitor.
# Description: Hardware temperature monitor.
# Provides: entry: tempsd
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
# Sysv script with systemd support: run initSystemd.
#
# A temperature monitor with shutdown when configured limits are exceeded.
# Simple configuration / extension for different sensors. Run sensors-detect
# to install the necessary drivers. Run sensors and determine what to monitor.
#
# If you have to, use `tempsd initSystemd` to config.
#
# See devTemp for a configuration example.
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=tempsd
if [ -d /run ]; then
PIDFILE=/run/$NAME.pid
else
PIDFILE=/var/run/$NAME.pid
fi
DESC="Hardware temperature monitor."
CONFIG=/usr/local/etc/tempsd.cfg
LOG=/var/log/tempsd.log
DEBUG_LOG=/tmp/tempsd.debug
CHECKLOOP=120
STARTUP_ERROR=0
ERROR=""
STX="^B"
ETX="^C"
WARNING_COUNT=2;
SYSTEMD_SERVICE=/etc/systemd/system/tempsd.service
SCRIPT_LOC=`readlink -f "$0"`
SCRIPT_PATH=${SCRIPT_LOC%/*}
if [ -n "${ARRAYS_ENV}" ]; then
. "${ARRAYS_ENV}/array.env"
else
. $SCRIPT_PATH/array.env
fi
#To enable systemd debug edit Environment DEBUG setting
#in /etc/systemd/system/tempsd.service
if [ -z "$DEBUG" ]; then
DEBUG=0;
fi
debug() {
if [ "$DEBUG" -eq 1 ]; then
TSTAMP=`date +%M:%S`
echo $TSTAMP $1 >>$DEBUG_LOG
fi
}
if [ "$DEBUG" -eq 0 ]; then
rm -f $DEBUG_LOG
else
debug "Debug Enabled"
echo "Debug Enabled $DEBUG_LOG" >> $LOG
fi
## Sources of temperature data (Raw output dumped on overheat event) ##########
getNvidiaOp() {
RET=0
nvCmd=`which nvidia-settings`
if [ -n "$nvCmd" -a -e "$nvCmd" ]; then
nvidia-settings -t -q $1/GPUCoreTemp
else
ERROR="Nonexistent nvidia-settings command."
RET=1
fi
return $RET
}
getSensorOp() {
sensors
}
getParams() {
OIFS=$IFS
IFS="_"
for param in $1
do
tparam=`echo $param | sed 's/[\x00\x09\x0a\x0d\x0e\x0f]//g'`
array_add params "$tparam"
done
IFS=$OIFS
}
## Possible check functions (extend here) #####################################
#Calls to these functions are specified in the config file ie
# the name of the func + args delimited by '_' and the max temp
# delimited by ':'. Function calls are delimited by ';' eg:
# temp_1:45;temp_2:45;nvidiaQuadro_0:70'
# Functions must set the TEMP variable.
#nvidiaQuadro_0 -> 0 defines the x11 display number.
#temp_1 -> 1 defines the temperature temp1 from 'sensors' output.
##devTemp is the default function
# arg1: device name; arg2 temperature name; arg3: position number of the temp.
# config eg:
# devTemp_coretemp-isa-0000_Package id 0_4:40;devTemp_coretemp-isa-0000_Core 0_3:40
#
# for sensor output:
# coretemp-isa-0000
# Adapter: ISA adapter
# Package id 0: +34.0°C (high = +82.0°C, crit = +102.0°C)
# Core 0: +29.0°C (high = +82.0°C, crit = +102.0°C)
#
getDevTemp() {
getParams "$1"
paramCount=`array_size params`
if [ $paramCount -lt 3 ]; then
echo "ERROR: DevTemp requires 3 parameters but has $paramCount." >>$LOG
else
DEVICENAME=`get_avar params 1`
TEMPNAME=`get_avar params 2`
FIELDPOS=`get_avar params 3`
array_clear params
debug "devicename:$DEVICENAME tempname:$TEMPNAME field:$FIELDPOS"
TEMP=`sensors | \
awk -v fi=$FIELDPOS -v devName="^$DEVICENAME" -v tempName="^$TEMPNAME" ' \
BEGIN{found=0}
{
if ( $0 ~ /^$/) {
found=0
} else if (found == 0) {
if ($0 ~ devName) {
found=1
}
} else if (found==1) {
if ($0 ~ tempName) {
print $fi
}
}
}'`
debug "devTemp:$TEMP"
TEMP=`echo $TEMP| cut -d + -f 2`
TEMP=`echo $TEMP| cut -d . -f 1`
fi
}
#moboTemp
getMoboTemp() {
TEMP=`sensors | grep "MB Temperature" |awk '{print $3}'`
TEMP=`echo $TEMP| cut -d + -f 2`
TEMP=`echo $TEMP| cut -d . -f 1`
}
#cpuTemp
getCpuTemp() {
TEMP=`getSensorOp | grep "CPU Temperature" |awk '{print $3}'`
TEMP=`echo $TEMP| cut -d + -f 2`
TEMP=`echo $TEMP| cut -d . -f 1`
}
#temp_?
getTemp() {
#example "temp1: +44.0°C"
TEMP=`getSensorOp | grep "temp${1}:" | awk '{print $2}'`
TEMP=`echo $TEMP| cut -d + -f 2`
TEMP=`echo $TEMP| cut -d . -f 1`
#TEMP=${TEMP#+}
#TEMP=${TEMP%.*}
}
getRadeon() {
TEMP=`sensors | \
awk -v num="$1" 'BEGIN{found=0}
{ if ($0 ~ /^radeon-pci/) {found=1}
if ( $0 ~ /^$/) {found=0}
if (found==1) {
if ($0 ~ /^temp1/){print $2}
}
}'`
TEMP=`echo $TEMP| cut -d + -f 2`
TEMP=`echo $TEMP| cut -d . -f 1`
}
#nvidiaQuadro_DISPLAYNO
getNvidiaQuadro() {
DISPLAY=:$1
RET=0
export DISPLAY
xhost >/dev/null 2>/dev/null
X_EXISTS=$?
if [ $X_EXISTS -eq 0 ]; then
TMP=`getNvidiaOp $DISPLAY`
RET=$?
debug "getNvidiaQuadro TMP:$TMP RET:$RET"
if [ $RET -eq 0 ]; then
TEMP=`echo $TMP |cut -f1 -d' '`
debug "getNvidiaQuadro TEMP:$TEMP"
fi
else
TEMP="NO_X"
fi
return $RET
}
getK10temp() {
TEMP=`sensors | \
awk -v num="$1" 'BEGIN{found=0}
{
if ( $0 ~ /^$/) {
found=0
} else if (found == 0) {
if ($0 ~ /^k10temp-pci/) {found=1}
} else if (found==1) {
if ($0 ~ /^Adapter/){found=2}
} else if (found==2) {
if ($0 ~ /^temp1/){
print $2
}
}
}'`
TEMP=`echo $TEMP| cut -d + -f 2`
TEMP=`echo $TEMP| cut -d . -f 1`
}
## Main logic #################################################################
checkTemp() {
#Return OVER:t/fMESSAGE[STX]message[ETX] text else an error message.
cfg_data=$1
cfg_data_test=$2
OIFS="$IFS"
IFS=";"
for sensor in `echo "$cfg_data"`
do
array_add sensors "$sensor"
done;
IFS=$OIFS
sensorNo=1
while [ $sensorNo -le `array_size sensors` ];
do
sensor=`get_avar sensors $sensorNo`
debug "checkTemp sensor: $sensor"
msg=""
OVER="f"
limit=`echo $sensor | cut -s -d : -f 2`
cmd=`echo $sensor | cut -s -d : -f 1`
if [ -z "$cmd" -o -z "$limit" ]; then
echo "Error in tempsd: Not of form 'sensorNameA_arg1_arg2:temperatureLimit;\
sensorNameB_arg1:tempLimit' Sensor data: $sensor " | tee >> $LOG
RET=1;
else
debug "cmd:$cmd"
firstLetter=`echo ${cmd} | cut -c 1 | tr [:lower:] [:upper:]`
endCmd=`echo ${cmd} | cut -c 2- | cut -d_ -f 1`
params=`echo ${cmd} | cut -d_ -f 2-`
cmd=get$firstLetter$endCmd
debug "cmd:$cmd params:$params"
TEMP=""
$cmd "$params"
RET=$?
temp=$TEMP
debug "sensor:$sensor cmd:$cmd RET:$RET temp:$temp"
if [ $RET != 0 ]; then
debug "RET:$RET"
msg="Failure in tempsd testing for SENSOR:$sensor"
elif [ "$temp" = "NO_X" ]; then
#if NO_X then X is not running and we can not get the nv card temp.
#Without X nv card should not be hot so ignore nv card and continue.
debug "No X11, so no nvidia temps."
else
isNum=`echo $temp |grep -c "^[0-9]\+$"`
debug "temp:$temp isNum:$isNum"
if [ $isNum -eq 0 ]; then
msg="ERROR tempsd : No temperature returned from SENSOR:$sensor"
if [ -n "$cfg_data_test" ]; then
#On cfg_data_test ie startup. Allow print of config location.
RET=1;
else
OVER=t;
fi
elif [ -z "$cfg_data_test" -a "$temp" -gt "$limit" ]; then
msg="WARNING tempsd: Running hot! LIMIT:$temp SENSOR:$sensor"
OVER=t;
fi
fi
fi
if [ $RET -ne 0 ]; then
echo OVER${OVER}MESSAGE${STX}${msg}${ETX}
return $RET
elif [ "$OVER" = t ]; then
echo OVER${OVER}MESSAGE${STX}${msg}${ETX}
return $RET
fi
sensorNo=$((sensorNo+1))
done;
echo OVER${OVER}MESSAGE${STX}${ETX}
}
doShutdown() {
shutdown -h 0
}
shutdownWarning() {
causeMsg=$1
{
echo Shutdown imminent!
echo $causeMsg
} | wall
DISPLAY=$TEMPSD_DISPLAY
debug DISPLAY: $DISPLAY
export DISPLAY
xhost >/dev/null 2>/dev/null
X_EXISTS=$?
if [ $X_EXISTS -eq 0 ]; then
xterm -fa *courier* -fs 20 -bg red -fg black -maximized -T "Computer Temperature Monitor" -hold \
-e echo "$causeMsg" & 2>>$LOG >>$LOG
else
echo No X11! Is /etc/X11/xinit/xinitrc or $HOME/.xinitrc configured? ie: xhost +si:localuser:root >>$LOG
fi
WARNING_COUNT=$((WARNING_COUNT-1));
debug WARNING_COUNT=$WARNING_COUNT
}
start() {
debug start
RET=0;
if [ -e "${PIDFILE}" ]; then
echo $NAME already running: $PIDFILE exists.
RET=1;
else
TEMPSD_DISPLAY=`head -1 $CONFIG`
CFG_DATA=`tail -1 $CONFIG`
debug ConfigLocation:$CONFIG
debug "ConfigData:$CFG_DATA"
if [ -z "$CFG_DATA" ]; then
echo
echo Error no config data in $CONFIG.
echo For example set $CONFIG containing:
echo ":0"
echo "temp_1:49;temp_2:50;temp_3:30;nvidiaQuadro_0:70"
STARTUP_ERROR=1
else
OP=$(checkTemp "$CFG_DATA" test 2>&1)
RET=$?
if [ $RET -ne 0 ]; then
echo Error in tempsd config data in $CONFIG. |tee -a $LOG
echo $OP|tee -a $LOG
echo THE TEMPERATURE MONITOR FAILED TO START.|tee -a $LOG
echo OPEATION OF YOUR COMPUTER WITHOUT A TEMPERATURE MONITOR MAY RESULT IN A FIRE!|tee -a $LOG
STARTUP_ERROR=1
RET=0
fi
fi
if [ $STARTUP_ERROR -eq 1 ]; then
WARNING_COUNT=20
fi
(
while [ 1 -eq 1 ]; do
if [ $STARTUP_ERROR -eq 1 ]; then
shutdownWarning "Error on start of tempsd service. Computer may cause a fire."
else
#Capture all stdout and stderr and fail on random output.
if [ "$DEBUG" -eq 1 ]; then
CFG_DATA=`tail -1 $CONFIG`
debug "ConfigData:$CFG_DATA"
fi
OP=$(checkTemp "$CFG_DATA" 2>&1)
RET=$?
if [ "$RET" -ne 0 ]; then
shutdownWarning $OP
else
MATCH=`expr match "$OP" '\(^OVER[tf]MESSAGE'$STX'.*'$ETX'\)'`
OVER=`expr substr "$OP" 5 1`
MSG=`expr substr "$OP" 14 1000`
debug "match:$MATCH"
debug "over:$OVER"
debug "msg:$MSG"
if [ -z "$MATCH" ]; then
shutdownWarning "Error tempsd data collection. Data: $OP"
elif [ "$OVER" != "f" ]; then
shutdownWarning "Computer Overheating. Check the operation of fans, water pump etc. $MSG"
{
date >> $LOG
getSensorOp >> $LOG
getNvidiaOp $TPSD_DISPLAY >> $LOG
} | tee -a $LOG | wall
fi
fi
fi
if [ $WARNING_COUNT -lt 0 ]; then
doShutdown
fi
sleep $CHECKLOOP;
done;
)&
echo $! > $PIDFILE
echo Started $NAME ok.
fi
return $RET;
}
stop() {
RET=0
if [ -e "${PIDFILE}" ]; then
if [ -r "${PIDFILE}" ]; then
PID=`cat ${PIDFILE}`
MSG=`kill -0 $PID`
RET=$?
if [ $RET -ne 0 ]; then
echo $MSG
else
MSG=`kill -15 $PID`
RET=$?
if [ $RET -ne 0 ]; then
echo $MSG
echo Could not stop process $NAME pid: $PID.
else
rm ${PIDFILE}
echo Stopped $NAME ok.
fi
fi
else
echo "No permission to read file ${PIDFILE}."
RET=1;
fi
fi
return $RET
}
initSystemd() {
if [ -e $SYSTEMD_SERVICE ]; then
echo $SYSTEMD_SERVICE already installed.
else
touch $SYSTEMD_SERVICE
chmod 700 $SYSTEMD_SERVICE
(
cat <<EOF
[Unit]
Description=Local Host temperature shutdown monitor.
[Service]
Type=forking
ExecStart=$SCRIPT_LOC start
ExecStop=$SCRIPT_LOC stop
Environment=DEBUG=0
Environment=ARRAYS_ENV=${SCRIPT_PATH}
SuccessExitStatus=0
RestartPreventExitStatus=1 2
PIDFile=$PIDFILE
[Install]
WantedBy=multi-user.target
EOF
) > $SYSTEMD_SERVICE
systemctl enable tempsd.service
systemctl start tempsd.service
fi
}
case "$1" in
start)
echo Starting $NAME...
start
;;
stop)
echo Stopping $NAME...
stop
;;
restart)
echo Restarting $NAME...
stop
start
;;
initSystemd)
echo Installing $SYSTEMD_SERVICE
initSystemd
;;
*)
echo "Usage /etc/init.d/$NAME {start|stop|initSystemd}"
exit 2;
;;
esac
# vim: tabstop=2 shiftwidth=2 expandtab: