-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautoshutdown.sh
executable file
·1326 lines (1195 loc) · 48.2 KB
/
autoshutdown.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
: '
Created by René (https://github.com/rfuehrer)
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
'
THISDIR=$(dirname "$0")
THISDIR=$(dirname "$(realpath "$0")")
# ------- DO NOT EDIT BEFORE THIS LINE -------
# VARIABLES TO EDIT
# CHECKHOSTS: network devices (eg. router or PC) as reference (name or IP) seperated with space
# WAITTIME_SHUTDOWN_SEC (in seconds): time between first and safty ping (for PC: use min. reboot time) to prevent shutdown while rebooting
# SLEEP_TIMER (in seconds): time between regular pings
# SLEEP_MAXLOOP: max loops to wait before shutdown (SLEEP_TIMER * SLEEP_MAXLOOP seconds)
# LOGFILE: name of the logfile
# LOGFILE_MAXLINES: max line number to keep in log file
#CHECKHOSTS="192.168.0.2 192.168.0.4 192.168.0.14"
# ########################################################
# ########################################################
# # DEFAULT VARIABLES
# ########################################################
# ########################################################
APP_NAME="Syno Autoshutdown"
APP_VERSION="2.2"
APP_DATE="05.01.2020"
APP_SOURCE="https://github.com/rfuehrer/syno_autoshutdown/"
SLEEP_TIMER=10
SLEEP_MAXLOOP=180
GRACE_TIMER=4
LOGFILE_MAXLINES=100000
LOGFILE_CLEANUP_DAYS=7
DEBUG_MODE=0
RUNLOOP_COUNTER=0
MAXLOOP_COUNTER=0
DMSS_ACTIVE=0
DMSS_ACTIVE_COUNTER=0
CONFIGFILE=autoshutdown.config
CONFIGFILE_INI=autoshutdown.config.ini
HASHFILE=autoshutdown.config.hash
HASHSCRIPTFILE=autoshutdown.sh.pidhash
HASHSCRIPTFILE_DEV=autoshutdown.sh-dev.pidhash
# ------- DO NOT EDIT BELOW THIS LINE -------
# ########################################################
# ########################################################
# # GENERATED VARIABLES
# ########################################################
# ########################################################
PID=$BASHPID
LOG_TIMESTAMP_FORMAT_DATETIME=$(date +%Y%m%d_%H%M%S)
LOG_TIMESTAMP_FORMAT_DATE=$(date +%Y%m%d)
LOG_TIMESTAMP_FORMAT_TIME=$(date +%H%M%S)
MY_PRIMARY_IP=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
MY_SCAN_RANGE=$(echo "$MY_PRIMARY_IP" | cut -d. -f-3)
MY_START_TIME=$(date +"%d.%m.%Y %H:%M:%S")
MY_HOSTNAME=$(hostname)
SCRIPTFILE=$(basename "$0")
if [ "z$MY_HOSTNAME" != "z" ]; then
CONFIGFILE="autoshutdown-$MY_HOSTNAME.config"
if [ ! -f "$THISDIR/$CONFIGFILE" ]; then
# fallback config (generic)
CONFIGFILE="autoshutdown.config"
fi
fi
SCRIPTFILE="$THISDIR/$SCRIPTFILE"
CONFIGFILE="$THISDIR/$CONFIGFILE"
HASHFILE="$THISDIR/$HASHFILE"
# reset color
COLOR_NC="\033[0m"
MY_PUBLIC_IP=$(curl --silent checkip.amazonaws.com 2>&1)
MY_UUID=$(uuidgen)
MY_WEBSERVER_MAGICKEY_GENERATED=$(uuidgen|md5sum|cut -d ' ' -f 1)
# ########################################################
# ########################################################
# # FUNCTIONS
# ########################################################
# ########################################################
# -------------------------------------------
#######################################
# Convert seconds to human readable format
# Globals:
# -
# Arguments:
# $1: seconds
# $2: format (long/short)
# Returns:
#
#######################################
sec_to_time() {
local MY_SECONDS=$1
local MY_FORMAT=${2:-short}
local sign=""
local hours
local minutes
local seconds
local HRF_SECONDS
if [[ ${MY_SECONDS:0:1} == "-" ]]; then
seconds=${MY_SECONDS:1}
sign="-"
fi
local days=$(( (MY_SECONDS / 3600) / 24))
local hours=$(( (MY_SECONDS / 3600) ))
local minutes=$(( (MY_SECONDS % 3600) / 60 ))
seconds=$(( (MY_SECONDS) % 60 ))
if [ "$MY_FORMAT" != "long" ]; then
HRF_SECONDS=$(printf "%s%02d:%02d:%02d:%02d" "$sign" $days $hours $minutes $seconds)
else
HRF_SECONDS=$(printf "%s%02dd:%02dh:%02dm:%02ds" "$sign" $days $hours $minutes $seconds)
fi
writelog "D" "Conversion of seconds: $MY_SECONDS => $HRF_SECONDS"
echo "$HRF_SECONDS"
}
check_hash_script_modified(){
MD5_HASHSCRIPT_SAVED=$(cat $HASHSCRIPTFILE)
if [ "$MD5_HASHSCRIPT_SAVED" != "$MD5_HASHSCRIPT" ]; then
RET=1
else
RET=0
fi
echo $RET
}
#######################################
# Starts an asynchron webserver
# Globals:
# -
# Arguments:
# $1: port numbr
# Returns:
#
#######################################
# shellcheck disable=SC2009
init_webserver_shutdown(){
# check if script file is modified? if true, the webserver would be restarted after reloading/restarting the script
# SCRIPT_MODIFIED=$(check_hash_script_modified)
# if [ $SCRIPT_MODIFIED -eq 0 ]; then
# shutdown all previous instances
WEBSERVER_INSTANCES=$(ps -ef|grep -v grep|grep -c "$WEBSERVER_SHUTDOWN_SCRIPT")
while [[ $WEBSERVER_INSTANCES -ne 0 ]]; do
writelog "I" "Kill all instances ($WEBSERVER_INSTANCES) of '$WEBSERVER_SHUTDOWN_SCRIPT'"
pkill -f "$WEBSERVER_SHUTDOWN_SCRIPT" >/dev/null 2>&1
sleep 2
WEBSERVER_INSTANCES=$(ps -ef|grep -v grep|grep -c "$WEBSERVER_SHUTDOWN_SCRIPT")
done
sleep 5
WEBSERVER_INSTANCES=$(ps -ef|grep -v grep|grep -c "$WEBSERVER_SHUTDOWN_SCRIPT")
WEBSERVER_STARTUP_FAILED=0
WEBSERVER_STARTUP_COUNTER=0
writelog "I" "Waiting for webserver to start (instances=$WEBSERVER_INSTANCES; loop=$WEBSERVER_STARTUP_COUNTER)"
while [[ $WEBSERVER_STARTUP_FAILED -eq 0 ]] && [[ $WEBSERVER_INSTANCES -eq 0 ]]; do
WEBSERVER_STARTUP_COUNTER=$((WEBSERVER_STARTUP_COUNTER+1))
if [ "$WEBSERVER_INSTANCES" -eq 0 ]; then
PYTHON_EXEC=$(command -v python)
"$PYTHON_EXEC" "$THISDIR/$WEBSERVER_SHUTDOWN_SCRIPT" --port "$WEBSERVER_SHUTDOWN_PORT" --uuid "$MY_UUID" --spath "$WEBSERVER_SHUTDOWN_URL" --tpath "$WEBSERVER_TEST_URL" --magickey "$WEBSERVER_MAGICKEY" --magicword "$WEBSERVER_MAGICWORD" --rpath "$WEBSERVER_DMSS_RESET_URL" &
fi
sleep 2
WEBSERVER_INSTANCES=$(ps -ef|grep -v grep|grep -c "$WEBSERVER_SHUTDOWN_SCRIPT")
writelog "I" "Waiting for webserver to start (instances=$WEBSERVER_INSTANCES; loop=$WEBSERVER_STARTUP_COUNTER)"
if [ "$WEBSERVER_STARTUP_COUNTER" -gt 5 ]; then
WEBSERVER_STARTUP_FAILED=1
fi
done
if [ "$WEBSERVER_INSTANCES" -eq 0 ]; then
writelog "W" "Failed to start webserver!"
else
writelog "I" "Shutdown webserver (external call) set to 'http://$MY_PUBLIC_IP:$WEBSERVER_SHUTDOWN_PORT_EXTERNAL/$MY_UUID/$WEBSERVER_SHUTDOWN_URL'"
writelog "I" "Shutdown webserver (local call) set to 'http://localhost:$WEBSERVER_SHUTDOWN_PORT/$MY_UUID/$WEBSERVER_TEST_URL'"
#notification "$MYNAME" "$MESSAGE_WEBSERVER_SHUTDOWN_START"
#notification "$MYNAME" "http://$MY_PUBLIC_IP:$WEBSERVER_SHUTDOWN_PORT_EXTERNAL/$MY_UUID/$WEBSERVER_SHUTDOWN_URL"
fi
# else
# writelog "I" "Script modified, do not start webserver in this instance... Please wait for reloading."
# fi
}
#######################################
# Resolve hostname from IP address
# Globals:
# -
# Arguments:
# $1: IP address
# Returns:
# hostname
#######################################
get_hostname_from_ip(){
local RET
RET=$(arp -a|grep "$1"|awk '{print $1}'|cut -d. -f1)
echo "$RET"
}
#######################################
# Convert string to lower case
# Globals:
# -
# Arguments:
# $1: string to be converted
# Returns:
# lower case converted string
#######################################
string_to_lower(){
local RET
RET=$(echo "$1" | tr '[:upper:]' '[:lower:]')
echo "$RET"
}
#######################################
# Read/Write value in config file
# Globals:
# $CONFIGFILE
# Arguments:
# $1: variable name
# $2: default value
# $3: description of variable
# $4: output read value to console(log (0/1))
# $5: initialize config value if not present (0/1) - a later call with <>0 will initialize missing values
# Returns:
# -
#######################################
function ini_val() {
# BASH3 Boilerplate: ini_val
#
# This file:
#
# - Can read and write .ini files using pure bash
#
# Limitations:
#
# - All keys inside the .ini file must be unique, regardless of the use of sections
#
# Usage as a function:
#
# source ini_val.sh
# ini_val data.ini connection.host 127.0.0.1
#
# Usage as a command:
#
# ini_val.sh data.ini connection.host 127.0.0.1
#
# Based on a template by BASH3 Boilerplate v2.4.1
# http://bash3boilerplate.sh/#authors
#
# The MIT License (MIT)
# Copyright (c) 2013 Kevin van Zonneveld and contributors
# You are not obligated to bundle the LICENSE file with your b3bp projects as long
# as you leave these references intact in the header comments of your source files.
# ini_val $CONFIGFILE $MY_VAR $MY_DEFAULT $MY_DESCRIPTION $MY_OUTPUT $MY_INIT_CONFIG
local file="${1:-}"
local sectionkey="${2:-}"
local val="${3:-}"
local comment="${4:-}"
local delim="="
local comment_delim=";"
local section=""
local key=""
local current=""
# add default section
local section_default="default"
if [[ ! -f "${file}" ]]; then
# touch file if not exists
touch "${file}"
fi
# Split on . for section. However, section is optional
IFS='.' read -r section key <<< "${sectionkey}"
if [[ ! "${key}" ]]; then
key="${section}"
# default section if not given
section="${section_default}"
fi
current=$(sed -En "/^\[/{h;d;};G;s/^${key}([[:blank:]]*)${delim}(.*)\n\[${section}\]$/\2/p" "${file}"|awk '{$1=$1};1')
if ! grep -q "\[${section}\]" "${file}"; then
# create section if not exists (empty line to seperate new section)
echo >> "${file}"
echo "[${section}]" >> "${file}"
fi
if [[ ! "${val}" ]]; then
# get a value
echo "${current}"
else
# set a value
if [[ ! "${current}" ]]; then
# doesn't exist yet, add
if [[ ! "${section}" ]]; then
# if no section is given, propagate the default section
section=${section_default}
fi
# add to section
if [[ ! "${comment}" ]]; then
# add new key/value without description
RET="/\\[${section}\\]/a\\
${key}${delim}${val}"
else
# add new key/value with description
RET="/\\[${section}\\]/a\\
${comment_delim}[${key}] ${comment}\\
${key}${delim}${val}"
fi
sed -i.bak -e "${RET}" "${file}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${file}.bak"
else
# replace existing (modified to replace only keys in given section)
sed -i.bak -e "/^\[${section}\]/,/^\[.*\]/ s|^\(${key}[ \t]*${delim}[ \t]*\).*$|\1${val}|" "${file}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${file}.bak"
fi
fi
}
#######################################
# Read/Init value in config file
# Globals:
# $CONFIGFILE
# Arguments:
# $1: variable name
# $2: default value
# $3: description of variable
# $4: output read value to console(log (0/1))
# $5: initialize config value if not present (0/1) - a later call with <>0 will initialize missing values
# Returns:
# -
#######################################
read_config_value(){
local MY_VAR=$1
local MY_DEFAULT=$2
local MY_DESCRIPTION=$3
local MY_OUTPUT=$4
local MY_INIT_CONFIG=$5
local RET
RET=""
if grep -q "^$MY_VAR" "$CONFIGFILE"
then
# string found
# RET=$(cat "$CONFIGFILE" | grep "^$MY_VAR=" | cut -d= -f2)
RET=$(grep "^$MY_VAR=" "$CONFIGFILE" | cut -d= -f2)
# : "${ACTIVE_STATUS:=1}"
if [ "$RET" == "" ];then
[ "$MY_OUTPUT" == "1" ] && writelog "I" "No config value '$MY_VAR' in config file. Setting default value '$MY_DEFAULT'."
RET="$MY_DEFAULT"
fi
else
RET=$MY_DEFAULT
if [ "$MY_INIT_CONFIG" != "0" ]; then
# string not found
[ "$MY_OUTPUT" == "1" ] && writelog "I" "No variable '$MY_VAR' found in config file. Initializing variable to config file."
{ echo ; echo "; [$MY_VAR] $MY_DESCRIPTION"; echo "$MY_VAR=$MY_DEFAULT"; } >>"$CONFIGFILE"
fi
fi
# set dynamic variable name to read content
eval "$MY_VAR"=\$RET
[ "$MY_OUTPUT" == "1" ] && writelog "I" "Set variable '$MY_VAR' to value '$RET'"
}
#######################################
# Read config file an variables
# Globals:
# $HASHFILE
# $CONFIGFILE
# Arguments:
# -
# Returns:
# $CHECKHOSTS
# $MYNAME
# $ACTIVE_STATUS
# $DEBUG_MODE
# $SLEEP_TIME
# $SLEEP_MAXLOOP
# $GRACE_TIMER
# $LOGFILE_MAXLINES
# $LOGFILE_CLEANUP_DAYS
# $IFTTT_KEY
# $IFTTT_EVENT
# $SHUTDOWN_BEEP
# $SHUTDOWN_BEEP_COUNT
# $GRACE_BEEP
# $GRACE_BEEP_COUNT
# $NOTIFY_ON_GRACE_START
# $NOTIFY_ON_GRAVE_EVERY
# $NOTIFY_ON_SHUTDOWN
# $NOTIFY_ON_LONGRUN_EVERY
# $NOTIFY_ON_STATUS_CHANGE
# $MESSAGE_SLEEP
# $MESSAGE_GRACE_START
# $MESSAGE_GRACE_EVERY
# $MESSAGE_LONGRUN
# $MESSAGE_STATUS_CHANGE_VAL
# $MESSAGE_STATUS_CHANGE_INV
#######################################
read_config() {
local MD5_HASH_SAVED
MD5_HASH_SAVED=$(cat "$HASHFILE")
local MD5_HASH_CONFIG
MD5_HASH_CONFIG=$(md5sum "$CONFIGFILE"| cut -d ' ' -f 1)
writelog "D" "Config hash : $MY_HOSTNAME : $CONFIGFILE"
writelog "D" "Config hash - actual hash value: $MD5_HASH_CONFIG"
writelog "D" "Config hash - saved hash value : $MD5_HASH_SAVED"
if [ "$MD5_HASH_SAVED" != "$MD5_HASH_CONFIG" ]; then
writelog "W" "Config hash - config modified, reload config"
# save new hash value
echo "$MD5_HASH_CONFIG" > "$HASHFILE"
# reset DMSS vars to prevent execution of existing grace periods and changed config vars
DMSS_ACTIVE=0
DMSS_ACTIVE_COUNTER=0
# reload config
writelog "I" "(Re-)Reading config file..."
read_config_value "CHECKHOSTS" "add-systems-to-monitor seperate-with-spaces" "client (to be checked) information; separated by space (valaue: $)" 1 1
CHECKHOSTS="$CHECKHOSTS "
CHECKHOSTS=$(string_to_lower "$CHECKHOSTS")
read_config_value "CHECKHOSTS_DEEPSLEEP" "add-systems-with-deep-sleep-mode" "client (to be checked) with deep sleep mode (special checks); separated by space (valaue: $)" 1 1
CHECKHOSTS_DEEPSLEEP="$CHECKHOSTS_DEEPSLEEP "
CHECKHOSTS_DEEPSLEEP=$(string_to_lower "$CHECKHOSTS_DEEPSLEEP")
read_config_value "CHECKHOSTS_IGNORE_MULTI_HOSTNAMES" 1 "if set ignore all names except the first one (valaue: 0/1 [1])" 1 1
read_config_value "MYNAME" "$MY_HOSTNAME" "cutsomizable hostname of executing NAS (used in notifications) (valaue: $)" 1 1
read_config_value "ACTIVE_STATUS" 1 "active status of this script (for manual deactivation) (value: 0/1 [1])" 1 1
read_config_value "DEBUG_MODE" 0 "debug mode (outut of debug messages to stdout and log) (value: 0/1 [0])" 1 1
read_config_value "USE_INTERACTIVE_COLOR" 1 "use color codes in interactive/console mode (value: 0/1 [1])" 1 1
# Color codes
#Black 0;30 Dark Gray 1;30
#Red 0;31 Light Red 1;31
#Green 0;32 Light Green 1;32
#Brown/Orange 0;33 Yellow 1;33
#Blue 0;34 Light Blue 1;34
#Purple 0;35 Light Purple 1;35
#Cyan 0;36 Light Cyan 1;36
#Light Gray 0;37 White 1;37
read_config_value "COLOR_ERROR" "\033[0;31m" "color code for error classification (value: $)" 0 1
read_config_value "COLOR_WARNING" "\033[0;33m" "color code for warning classification (value: $)" 0 1
read_config_value "COLOR_INFO" "\033[1;37m" "color code for info classification (value: $)" 0 1
read_config_value "COLOR_DEBUG" "\033[1;30m" "color code for debug classification (value: $)" 0 1
read_config_value "COLOR_PID" "\033[0;35m" "color code for process id" 0 1
read_config_value "SLEEP_TIMER" 60 "wating time (loop) to check clients again (value: # [60])" 1 1
read_config_value "SLEEP_MAXLOOP" 30 "number of max loops (value: # [30])" 1 1
read_config_value "GRACE_TIMER" 20 "start grace period after x loops (value: # [20])" 1 1
read_config_value "LOGFILE_MAXLINES" 1000 "limit log file to number of lines (value: # [1000])" 1 1
read_config_value "LOGFILE_CLEANUP_DAYS" 3 "clean log files older than x days (value: # [3])" 1 1
read_config_value "LOGFILE_FILENAME" "autoshutdown.log" "define log filename; placeholder optionally (#DATETIME#) (value: $ [autoshutdown.log])" 1 1
LOGFILE=$(replace_logfilename_placeholder "$LOGFILE_FILENAME")
LOGFILE="$THISDIR/$LOGFILE"
read_config_value "SCRIPT_DEV_FILENAME" "autoshutdown.sh.txt" "define script filename for edited version (copies from this file to running script at start of loop; prevents text file busy errors) (value: $)" 1 1
SCRIPTFILE_DEV="$THISDIR/$SCRIPT_DEV_FILENAME"
read_config_value "IFTTT_KEY" "" "IFTTT magic key for webhook notifications (value: $)" 0 1
read_config_value "IFTTT_EVENT" "" "IFTTT event name for notifications (value: $)" 1 1
read_config_value "SHUTDOWN_BEEP" 1 "beep system loudspeaker if shutting down (value: 0/1 [1])" 1 1
read_config_value "SHUTDOWN_BEEP_COUNT" 5 "number of beeps at shutdown (value: # [5])" 1 1
read_config_value "GRACE_BEEP" 1 "beep system loudspeaker if in grace period (value: 0/1 [1])" 1 1
read_config_value "GRACE_BEEP_COUNT" 1 "number of beeps in grace period (value: # [1])" 1 1
read_config_value "NOTIFY_ON_GRACE_START" 1 "send notification on start of grace period (value: 0/1 [1])" 1 1
read_config_value "NOTIFY_ON_GRACE_EVERY" 5 "send notification in grace period (value: # [5])" 1 1
read_config_value "NOTIFY_ON_SHUTDOWN" 1 "send notification on shutdown (value: 0/1 [1])" 1 1
read_config_value "NOTIFY_ON_LONGRUN_EVERY" 180 "send notification if system is running a long time (value: # [180])" 1 1
read_config_value "NOTIFY_ON_STATUS_CHANGE" 1 "send notification if status of connected system changes (value: 0/1 [1])" 1 1
read_config_value "DSM_NOTIFY_ON_STATUS_CHANGE" 1 "send Synology DSM notification if status of connected system changes (value: 0/1 [1])" 1 1
read_config_value "MESSAGE_SLEEP" "System will be shut down now..." "notification message if system is shutting down (valaue: $)" 1 1
read_config_value "MESSAGE_GRACE_START" "System will be shut down soon..." "notification message if grace periods starts (valaue: $)" 1 1
read_config_value "MESSAGE_GRACE_EVERY" "System will be shut down soon..." "notification message while in grace period (valaue: $)" 1 1
read_config_value "MESSAGE_LONGRUN" "System is running for a long time..." "notification message if system is running a long time (valaue: $)" 1 1
read_config_value "MESSAGE_STATUS_CHANGE_VAL" "Systems found, starting normal mode..." "notification message if valid systems are found (valaue: $)" 1 1
read_config_value "MESSAGE_STATUS_CHANGE_INV" "No systems found, starting monitoring mode..." "notification message if no valid systems are found (valaue: $)" 1 1
read_config_value "MESSAGE_LAST_SYSTEM_DEEPSLEEP" "Remaining valid system seems to be in deep sleep mode. Continuing checks.." "notification message if remaining system is possible in deep sleep mode (valaue: $)" 1 1
read_config_value "NETWORK_USAGE_INTERFACE" "eth0" "network interface of NAS to be checked (e.g. eth0, eth1, bond0,...) (valaue: $ [eth0])" 1 1
read_config_value "NETWORK_USAGE_INTERFACE_MIN_BYTES" 1000 "Less than x bytes per second for low bandwidth (valaue: # [1000])" 1 1
read_config_value "NETWORK_USAGE_INTERFACE_MAX_BYTES" 5000 "More than x bytes per second for high bandwidth (valaue: # [5000])" 1 1
read_config_value "NETWORK_USAGE_INTERFACE_PROBES" 10 "number of probes to calculate active usage (valaue: # [10])" 1 1
read_config_value "NETWORK_USAGE_INTERFACE_PROBES_POSITIVE" 7 "number of positive probes to identify active usage (valaue: # [7])" 1 1
read_config_value "WEBSERVER_SHUTDOWN_ACTIVE" 0 "set own shutdown webserver active (valaue: 0/1 [0])" 1 1
read_config_value "WEBSERVER_SHUTDOWN_PORT" 8080 "port number of shutdown webserver (valaue: # [8080" 1 1
read_config_value "WEBSERVER_SHUTDOWN_PORT_EXTERNAL" 48080 "external port number of shutdown webserver (valaue: # [48080])" 1 1
read_config_value "WEBSERVER_SHUTDOWN_URL" "shutdown" "path of shutdown webserver to execute shutdown (without prefix slash) (valaue: $)" 1 1
read_config_value "WEBSERVER_TEST_URL" "test" "path of webserver to test functionality (without prefix slash) (valaue: $)" 1 1
read_config_value "WEBSERVER_SHUTDOWN_SCRIPT" "autoshutdown_webserver.py" "path of shutdown webserver (valaue: $)" 1 1
read_config_value "WEBSERVER_SHUTDOWN_WEBSITE" "shutdown initialized" "content of html feedback page (valaue: $)" 1 1
read_config_value "MESSAGE_WEBSERVER_SHUTDOWN_START" "Shutdown Websever initialized..." "notification message if webserver is initialized (valaue: $)" 1 1
read_config_value "MESSAGE_WEBSERVER_SHUTDOWN" "Shutdown of system initialized by webserver" "notification message if shutdown initialized by webserver (valaue: $)" 1 1
read_config_value "WEBSERVER_MAGICKEY" "$MY_WEBSERVER_MAGICKEY_GENERATED" "Permanent magic key to access websever by IFTTT; sync with web request URL in receipe (value: $)" 0 1
read_config_value "WEBSERVER_MAGICWORD" "abracadabra" "Permanent magic word to access websever by IFTTT; advice: change this to sometineg else (value: $ )" 0 1
read_config_value "WEBSERVER_DMSS_RESET_URL" "reset" "path of webserver to deadman's switch (DMSS) functionality (without prefix slash) (valaue: $ [reset])" 1 1
ini_val "${CONFIGFILE_INI}" "DMSS.WEBSERVER_DMSS_RESET_URL" "reset" "path of webserver to deadman's switch (DMSS) functionality (without prefix slash) (valaue: $ [reset])" 1 1
read_config_value "DMSS_GRACE_EVERY" 300 "send desadman's switch (DMSS) link if system is running a long time (value: # [300}])" 1 1
ini_val "${CONFIGFILE_INI}" "DMSS.DMSS_GRACE_EVERY" 300 "send desadman's switch (DMSS) link if system is running a long time (value: # [300}])" 1 1
read_config_value "DMSS_EXECUTE_AFTER_GRACE" 5 "execute desadman's switch (DMSS) if user is not responding (value: # [5])" 1 1
ini_val "${CONFIGFILE_INI}" "DMSS.DMSS_EXECUTE_AFTER_GRACE" 5 "execute desadman's switch (DMSS) if user is not responding (value: # [5])" 1 1
read_config_value "DMSS_RESET_FILENAME" "autoshutdown.reset" "filename to reset desadman's switch (DMSS) if user is responding (value: # [5])" 1 1
ini_val "${CONFIGFILE_INI}" "DMSS.DMSS_RESET_FILENAME" "autoshutdown.reset" "filename to reset desadman's switch (DMSS) if user is responding (value: # [5])" 1 1
read_config_value "MESSAGE_DMSS_NOTIFY" "System is going to be shutdown if not resetted. To reset click here: #WEBSERVER_URL_DMSS_RESET#" "Notification to reset deadman's switch (DMSS) (value: $)" 1 1
ini_val "${CONFIGFILE_INI}" "DMSS.MESSAGE_DMSS_NOTIFY" "System is going to be shutdown if not resetted. To reset click here: #WEBSERVER_URL_DMSS_RESET#" "Notification to reset deadman's switch (DMSS) (value: $)" 1 1
read_config_value "MESSAGE_DMSS_EXECUTE_NOTIFY" "System is going to be shutdown by deadman's switch NOW" "Notification abaount execution of deadman's switch (DMSS) (value: $)" 1 1
ini_val "${CONFIGFILE_INI}" "DMSS.MESSAGE_DMSS_EXECUTE_NOTIFY" "System is going to be shutdown by deadman's switch NOW" "Notification abaount execution of deadman's switch (DMSS) (value: $)" 1 1
# after each config (re)load the webserver has to be starten
# ########################################################
# # WEBSERVER START
if [ "$WEBSERVER_SHUTDOWN_ACTIVE" -eq 1 ];then
init_webserver_shutdown
fi
else
writelog "I" "Config hash - hash value confirmed. No action needed."
fi
}
#######################################
# Check hash value of pidfile
# Globals:
# $SCRIPTFILE
# $HASHSCRIPTFILE
# $MD5_HASHSCRIPT_SAVED
# Arguments:
# -
# Returns:
# $MD5_HASHSCRIPT_SAVED
#######################################
check_pidhash(){
if [ -f "$SCRIPTFILE_DEV" ]; then
local MD5_HASHSCRIPT_DEV
MD5_HASHSCRIPT_DEV=$(md5sum "$SCRIPTFILE_DEV"| cut -d ' ' -f 1)
if [ ! -f "$HASHSCRIPTFILE_DEV" ]; then
# two ecos required, because one alone seems to be ignored
echo "$MD5_HASHSCRIPT_DEV" > "$HASHSCRIPTFILE_DEV"
writelog "I" "Script (dev) hash - init new hash"
writelog "I" "$MD5_HASHSCRIPT_DEV -> $HASHSCRIPTFILE_DEV"
echo "$MD5_HASHSCRIPT_DEV" > "$HASHSCRIPTFILE_DEV"
writelog "I" "Script (dev) hash - hash value confirmed. No action needed."
else
MD5_HASHSCRIPT_DEV_SAVED=$(cat "$HASHSCRIPTFILE_DEV")
writelog "D" "Script (dev) hash - actual hash value: $MD5_HASHSCRIPT_DEV"
writelog "D" "Script (dev) hash - saved hash value : $MD5_HASHSCRIPT_DEV_SAVED"
if [ "$MD5_HASHSCRIPT_DEV_SAVED" != "$MD5_HASHSCRIPT_DEV" ]; then
# do something
writelog "W" "Script (dev) hash - script modified, copy new version of script"
writelog "I" "Script (dev): $SCRIPTFILE_DEV"
writelog "I" "Script : $SCRIPTFILE"
cp "$SCRIPTFILE_DEV" "$SCRIPTFILE"
writelog "I" "Script (dev) copied..."
# two ecos required, because one alone seems to be ignored
echo "$MD5_HASHSCRIPT_DEV" > "$HASHSCRIPTFILE_DEV"
writelog "W" "Script (dev) hash - dev script modified, refresh hash"
writelog "I" "$MD5_HASHSCRIPT_DEV -> $HASHSCRIPTFILE_DEV"
echo "$MD5_HASHSCRIPT_DEV" > "$HASHSCRIPTFILE_DEV"
else
writelog "I" "Script (dev) hash - hash value confirmed. No action needed."
fi
fi
else
writelog "I" "Script (dev) - dev file not present. No action needed."
rm "$HASHSCRIPTFILE_DEV" 2>/dev/null
fi
local MD5_HASHSCRIPT
MD5_HASHSCRIPT=$(md5sum "$SCRIPTFILE"| cut -d ' ' -f 1)
# first run?
if [ ! -f "$HASHSCRIPTFILE" ]; then
writelog "I" "Script hash - init new hash"
echo "$MD5_HASHSCRIPT" > "$HASHSCRIPTFILE"
fi
MD5_HASHSCRIPT_SAVED=$(cat "$HASHSCRIPTFILE")
writelog "D" "Script hash : $SCRIPTFILE"
writelog "D" "Script hash - actual hash value: $MD5_HASHSCRIPT"
writelog "D" "Script hash - saved hash value : $MD5_HASHSCRIPT_SAVED"
if [ "$MD5_HASHSCRIPT_SAVED" != "$MD5_HASHSCRIPT" ]; then
# do something
writelog "W" "Script hash - script modified, restart script"
rm "$HASHSCRIPTFILE"
"$0" "$@" &
exit 0
else
writelog "I" "Script hash - hash value confirmed. No action needed."
fi
}
#######################################
# Beeps via system speaker of NAS
# Globals:
# -
# Arguments:
# $1: number of beeps
# Returns:
# -
#######################################
beeps() {
local BEEPS_NUM
BEEPS_NUM="$1"
for ((i=0; i<BEEPS_NUM; i++))
do
writelog "I" "Beep."
echo 2 > /dev/ttyS1
sleep 1
done
}
#######################################
# Replace placeholders in log filename
# Globals:
# $MY_HOSTNAME
# $LOG_TIMESTAMP_FORMAT_DATETIME
# $LOG_TIMESTAMP_FORMAT_DATE
# $LOG_TIMESTAMP_FORMAT_TIME
# $PID
# Arguments:
# $1: log filename to be converted
# Returns:
# converted string
#######################################
replace_logfilename_placeholder()
{
local retvar="$1"
retvar=${retvar//#DATETIME#/$LOG_TIMESTAMP_FORMAT_DATETIME}
retvar=${retvar//#DATE#/$LOG_TIMESTAMP_FORMAT_DATE}
retvar=${retvar//#TIME#/$LOG_TIMESTAMP_FORMAT_TIME}
retvar=${retvar//#PID#/$PID}
retvar=${retvar//#HOSTNAME#/$MY_HOSTNAME}
echo "$retvar"
}
#######################################
# Replace placeholders in message strings
# Globals:
# $VALID_MARKER_SYSTEMS_LIST
# $MY_START_TIME
# $MY_HOSTNAME
# $MY_PRIMARY_IP
# $RUNLOOP_COUNTER
# $RUNLOOP_TIME
# Arguments:
# $1: string to be converted
# Returns:
# converted string
#######################################
replace_placeholder()
{
local retvar
retvar="$1"
# replace placeholders with variable content
retvar=${retvar//#VALID_MARKER_SYSTEMS_LIST#/$VALID_MARKER_SYSTEMS_LIST}
retvar=${retvar//#MY_START_TIME#/$MY_START_TIME}
retvar=${retvar//#MY_HOSTNAME#/$MY_HOSTNAME}
retvar=${retvar//#MY_PRIMARY_IP#/$MY_PRIMARY_IP}
retvar=${retvar//#RUNLOOP_COUNTER#/$RUNLOOP_COUNTER}
# note: loops * sleep_time
local RUNLOOP_TIME
RUNLOOP_TIME=$((RUNLOOP_COUNTER*SLEEP_TIMER))
retvar=${retvar//#RUNLOOP_TIME#/$RUNLOOP_TIME}
#local RUNLOOP_TIME_SECS=$((RUNLOOP_TIME))
#local RUNLOOP_TIME_DAYS=$(((RUNLOOP_TIME_SECS/60*60)/24))
#local RUNLOOP_TIME_HOURS=$(((RUNLOOP_TIME_SECS/60*60)%24))
#local RUNLOOP_TIME_MINS=$(((RUNLOOP_TIME_SECS/60)%60))
#local RUNLOOP_TIME_SECS=$((RUNLOOP_TIME_SECS%60))
#retvar=${retvar//#RUNLOOP_TIME_HUMAN#/${RUNLOOP_TIME_DAYS}d:${RUNLOOP_TIME_HOURS}h:${RUNLOOP_TIME_MINS}m:${RUNLOOP_TIME_SECS}s}
RUNLOOP_SECONDS_HUMAN=$(sec_to_time "$RUNLOOP_TIME" "long")
retvar=${retvar//#RUNLOOP_TIME_HUMAN#/$RUNLOOP_SECONDS_HUMAN}
local SYS_UPTIME
SYS_UPTIME=$(awk '{print int($1)}' /proc/uptime)
local SYS_UPTIME_HUMAN
SYS_UPTIME_HUMAN=$(sec_to_time "$SYS_UPTIME" "long")
retvar=${retvar//#SYS_UPTIME_HUMAN#/$SYS_UPTIME_HUMAN}
retvar=${retvar//#WEBSERVER_URL_SHUTDOWN#/http://$MY_PUBLIC_IP:$WEBSERVER_SHUTDOWN_PORT_EXTERNAL/$MY_UUID/$WEBSERVER_SHUTDOWN_URL}
retvar=${retvar//#WEBSERVER_URL_DMSS_RESET#/http://$MY_PUBLIC_IP:$WEBSERVER_SHUTDOWN_PORT_EXTERNAL/$MY_UUID/$WEBSERVER_DMSS_RESET_URL}
echo "$retvar"
}
#######################################
# Write message to STDOUT and log file
# Globals:
# $DEBUG_MODE
# $LOGFILE
# $LOGFILE_MAXLINES
# $PID
# Arguments:
# $1: message level (I, D, W, E)
# $2: message
# Returns:
# -
#######################################
writelog()
{
local NOW
NOW=$(date +"%d.%m.%Y %H:%M:%S")
local MSGLEVEL="$1"
local MSG="$2"
# only output if NOT a "D" message or in debug mode
if [ "$MSGLEVEL" != "D" ] || [ "$DEBUG_MODE" -eq 1 ]; then
# use color output?
if [ "$USE_INTERACTIVE_COLOR" == "1" ];then
[ "$COLOR_PID" != "" ] && PID_CONSOLE="${COLOR_PID}$PID${COLOR_NC}"
case "$MSGLEVEL" in
D)
[ "$COLOR_DEBUG" != "" ] && MSGLEVEL_CONSOLE="${COLOR_DEBUG}$MSGLEVEL${COLOR_NC}"
;;
I)
[ "$COLOR_INFO" != "" ] && MSGLEVEL_CONSOLE="${COLOR_INFO}$MSGLEVEL${COLOR_NC}"
;;
W)
[ "$COLOR_WARNING" != "" ] && MSGLEVEL_CONSOLE="${COLOR_WARNING}$MSGLEVEL${COLOR_NC}"
;;
E)
[ "$COLOR_ERROR" != "" ] && MSGLEVEL_CONSOLE="${COLOR_ERROR}$MSGLEVEL${COLOR_NC}"
;;
esac
echo -e "$NOW [$PID_CONSOLE] [$MSGLEVEL_CONSOLE] - $MSG"
else
echo "$NOW [$PID] [$MSGLEVEL] - $MSG"
fi
# log output
echo "$NOW [$PID] [$MSGLEVEL] - $MSG" >>"$LOGFILE"
fi
# shorten logfile to max line number if not set to zero (0)
if [ $LOGFILE_MAXLINES -ne 0 ]; then
# log rotate dynamic logfile
tail -n "$LOGFILE_MAXLINES" "$LOGFILE" >"$LOGFILE.temp"
rm "$LOGFILE"
mv "$LOGFILE.temp" "$LOGFILE"
fi
}
#######################################
# Send notification to Synology DSM
# Globals:
# -
# Arguments:
# $1: Message
# Returns:
# -
#######################################
dsm_notification()
{
local MY_MESSAGE=$1
DSMNOTIFY_EXISTS=$(command -v synodsmnotify|wc -l)
if [ "$DSMNOTIFY_EXISTS" -eq 1 ]; then
writelog "I" "DSM notification sent"
synodsmnotify "@administrators" "$APP_NAME" "$MY_MESSAGE"
fi
}
#######################################
# Send notification to IFTTT
# Globals:
# $IFTTT_KEY
# $IFTTT_EVENT
# Arguments:
# $1: Identifier / Host name
# $2: Message
# Returns:
# -
#######################################
notification()
{
local MY_IDENTIFIER="$1"
local MY_MESSAGE="$2"
if [ "x$IFTTT_KEY" != "x" ]; then
if [ "x$IFTTT_EVENT" != "x" ]; then
if [ "x$MY_IDENTIFIER" != "x" ]; then
writelog "D" "IFTTT Notification NAME : $MY_IDENTIFIER"
writelog "D" "IFTTT Notification STATUS:$MY_MESSAGE"
writelog "D" "IFTTT Notification EVENT :$IFTTT_EVENT"
writelog "D" "IFTTT Notification KEY : $IFTTT_KEY"
writelog "D" "{\"value1\":\"$MY_IDENTIFIER - $MY_MESSAGE\"} https://maker.ifttt.com/trigger/$IFTTT_EVENT/with/key/$IFTTT_KEY"
curl -X POST -H "Content-Type: application/json" -d "{\"value1\":\"$APP_NAME: $MY_IDENTIFIER - $MY_MESSAGE\"}" "https://maker.ifttt.com/trigger/$IFTTT_EVENT/with/key/$IFTTT_KEY" >/dev/null 2>&1
writelog "I" "IFTTT Notification sent: $MY_MESSAGE"
dsm_notification "$MY_MESSAGE"
else
writelog "E" "No notification message stated. Notification aborted."
fi
else
writelog "E" "No notification event name stated. Notification aborted."
fi
else
writelog "E" "No notification magic key stated. Notification aborted."
fi
}
#######################################
# Check network usage (to check deep sleep systems)
# Globals:
# $NETWORK_USAGE_INTERFACE
# $NETWORK_USAGE_INTERFACE_MIN_BYTES
# $NETWORK_USAGE_INTERFACE_MAX_BYTES
# $NETWORK_USAGE_INTERFACE_PROBES_POSITIVE
# Arguments:
# -
# Returns:
# 0/1 status of usage
#######################################
is_network_in_use() {
COUNT_HIGH=0
BYTES_DIFF_MAX=0
#BYTES=$(ifconfig $NW_DEVICE|grep "TX bytes"|cut -d ":" -f 3|cut -d " " -f 1)
BYTES=$(cat "/sys/class/net/$NETWORK_USAGE_INTERFACE/statistics/rx_bytes")
for i in {0..10}
do
# while true; do
BYTES_SAVE="$BYTES"
#BYTES=$(ifconfig $NW_DEVICE|grep "TX bytes"|cut -d ":" -f 3|cut -d " " -f 1)
BYTES=$(cat "/sys/class/net/$NETWORK_USAGE_INTERFACE/statistics/rx_bytes")
BYTES_DIFF=$((BYTES-BYTES_SAVE))
if [ "$BYTES_DIFF" -gt "$BYTES_DIFF_MAX" ]; then
:
fi
if [ "$BYTES_DIFF" -lt "$NETWORK_USAGE_INTERFACE_MIN_BYTES" ];then
:
fi
if [ "$BYTES_DIFF" -gt "$NETWORK_USAGE_INTERFACE_MAX_BYTES" ];then
COUNT_HIGH=$((COUNT_HIGH+1))
fi
sleep 1
done
if [ "$COUNT_HIGH" -ge "$NETWORK_USAGE_INTERFACE_PROBES_POSITIVE" ]; then
echo 1
else
echo 0
fi
}
#######################################
# Trim leading and trailing whitespaces
# Globals:
# -
# Arguments:
# $1 string to be un-whitespaced
# Returns:
# un-whitespaced string
#######################################
trim() {
local var
var="$1"
var="${var#"${var%%[![:space:]]*}"}" # trim leading whitespace chars
var="${var%"${var##*[![:space:]]}"}" # trim trailing whitespace chars
echo -n "$var"
}
#######################################
# Restart loop if valid systems found
# Globals:
# $FOUND_SYSTEMS
# $VALID_MARKER_SYSTEMS_LIST
# Arguments:
# -
# Returns:
# MAXLOOP_COUNTER=0
#######################################
restart_loop() {
# reset counter
MAXLOOP_COUNTER=0
writelog "I" "$FOUND_SYSTEMS marker systems found. Resetting loop."
### Trim whitespaces ###
#VALID_MARKER_SYSTEMS_LIST=$(echo "$VALID_MARKER_SYSTEMS_LIST" | sed -e 's/^[[:space:]]*//')
VALID_MARKER_SYSTEMS_LIST=$(trim "$VALID_MARKER_SYSTEMS_LIST")
# replace inner spaces with ", "
VALID_MARKER_SYSTEMS_LIST=${VALID_MARKER_SYSTEMS_LIST// /, }
local RET
RET=$(replace_placeholder "Found system names: #VALID_MARKER_SYSTEMS_LIST#")
writelog "I" "$RET"
}
#######################################
# Check and notify if valid systems are found again
# Globals:
# $MAXLOOP_COUNTER
# $RUNLOOP_COUNTER
# $NOTIFY_ON_STATUS_CHANGE
# $MESSAGE_STATUS_CHANGE_VAL
# Arguments:
# -
# Returns:
# -
#######################################
notify_restart_loop() {
local RET
if [ $MAXLOOP_COUNTER -ne 0 ]; then
if [ $RUNLOOP_COUNTER -gt 1 ]; then
writelog "I" "--> status change (not found -> found)"
# only send notification after first loop
if [ "$NOTIFY_ON_STATUS_CHANGE" -eq "1" ];then
RET=$(replace_placeholder "$MESSAGE_STATUS_CHANGE_VAL")
writelog "I" "Sending notification (MESSAGE_STATUS_CHANGE_VAL)"
notification "$MYNAME" "$RET"
writelog "I" "Notification sent: $RET"
fi
fi
fi
}
notify_stop_loop() {
writelog "I" ""
}
# ########################################################
# ########################################################
# # MAIN PROGRAM
# ########################################################
# ########################################################
#if pidof -o %PPID -x $SCRIPTFILE>/dev/null; then
# echo "Process already running"
#fi
read_config_value "LOGFILE_FILENAME" "autoshutdown.log" "define log filename; placeholder optionally (#DATETIME#) (value: $)" 0 0
LOGFILE=$(replace_logfilename_placeholder "$LOGFILE_FILENAME")
LOGFILE="$THISDIR/$LOGFILE"
# ########################################################
# # INTRO HEADER
writelog "I" "################################################################################"
writelog "I" "#####"
writelog "I" "##### $APP_NAME"
writelog "I" "#####"
writelog "I" "##### Version $APP_VERSION, $APP_DATE"
writelog "I" "##### $APP_SOURCE"
writelog "I" "##### Licensed under APLv2"
writelog "I" "#####"
writelog "I" "################################################################################"