-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdomoticzCheck.sh
executable file
·132 lines (124 loc) · 4.39 KB
/
domoticzCheck.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
#!/bin/bash
#check that domoticz is running, and restart it if no process is running since TIME_DOMOTICZ_UNAVAILABLE minutes
#this script must be called by /etc/rc.local : put the following line in /etc/rc.local, before "exit 0" removing the first character #
#/usr/local/sbin/domoticzCheck.sh &
TIME_DOMOTICZ_UNAVAILABLE=5 #minutes: restart domoticz if unavailable since 5 minutes
CHECK_PLUGINS=1 #check that all python plugins are running correctly
DOMOTICZ_LOG=/var/log/domoticz.log #domoticz log file
DOMOTICZ_LOG_STRING='(WebServer.* thread seems to have ended unexpectedly|received fatal signal 11)' #regular expression (for egrep) to search in the last log lines to determines if a plugin has been stopped
DOMOTICZ_LOG_STRING2='( seems to have ended unexpectedly|Domoticz and DomoticzEx modules both found in interpreter)'
count=0
loglinesold=0
loglinesnew=0
loglines=0
function logcount () {
if [ -f "${DOMOTICZ_LOG}" ]; then
loglinesold=${loglinesnew}
loglinesnew=`wc -l ${DOMOTICZ_LOG}|cut -d ' ' -f 1`
if [ "a${loglinesold}" == "a0" ]; then loglinesold=${loglinesnew} ; fi
loglines=$(( ${loglinesnew} - ${loglinesold} ))
if [ $loglines -lt 0 ]; then loglines=0; fi
else
loglines=0
fi
}
function restartDomoticz () {
if [ -n "`pidof domoticz`" ]; then
killall domoticz
sleep 10
fi
if [ -n "`pidof domoticz`" ]; then
killall -9 domoticz
sleep 10
fi
#disable hyundai kia plugin
sqlite3 /home/pi/domoticz/domoticz.db 'update Hardware set Enabled=0 where ID=31;'
service domoticz restart
}
logerrorscount=0
logcount
while [ 1 ]; do
if [ -z "`pidof domoticz`" ]; then
count=$(( $count + 1 ))
echo "`date` : domoticz not running since ${count} minutes" >>/tmp/domoticzCheck.log
else
# Domoticz process is running
#check that web UI respond....
curl -s -m5 http://127.0.0.1:8080 >/dev/null
if [ $? -eq 28 ]; then
echo "`date` : domoticz not responding" >>/tmp/domoticzCheck.log
count=$(( $count + 2 ))
else
count=0
fi
if [ "a${CHECK_PLUGINS}" == "a1" ]; then
logcount
if [ $loglines -gt 0 ]; then
# Check that domoticz_hyundai_kia plugin is running correctly
ret=`tail -n $loglines ${DOMOTICZ_LOG} |egrep "${DOMOTICZ_LOG_STRING}"`
ret2=`tail -n $loglines ${DOMOTICZ_LOG} |egrep "${DOMOTICZ_LOG_STRING2}"`
if [ -n "$ret" ]; then
logerrorscount=$(( $logerrorscount +1 ))
if [ $logerrorscount -ge 2 ]; then
echo -e "`date` : Restart domoticz because at more than 2 errors found:" >>/tmp/domoticzCheck.log
echo "$ret" >>/tmp/domoticzCheck.log
echo "=====================================" >>/tmp/domoticzCheck.log
#echo "Restarting domoticz because log file contain the selected string"
service domoticz restart
logerrorscount=0
fi
elif [ -n "${ret2}" ]; then
logerrorscount=$(( $logerrorscount +1 ))
if [ $logerrorscount -ge 10 ]; then
echo "`date` : Restart domoticz because at least one plugin thread has ended:" >>/tmp/domoticzCheck.log
echo "${ret2}" >>/tmp/domoticzCheck.log
echo "=====================================" >>/tmp/domoticzCheck.log
restartDomoticz
logerrorscount=0
fi
else
#error message not found
if [ $logerrorscount -gt 0 ]; then
logerrorscount=$(( $logerrorscount -1 ))
fi
fi
#echo "logerrorscount=$logerrorscount"
fi
fi
fi
if [ $count -ge $TIME_DOMOTICZ_UNAVAILABLE ]; then
#echo "Restart domoticz after 5 minutes it is off"
echo "`date` : Restart domoticz because not active since ${TIME_DOMOTICZ_UNAVAILABLE} minutes" >>/tmp/domoticzCheck.log
restartDomoticz
fi
# check that /var/log partition is not full
df /var/log>/dev/null 2>&1
if [ $? -eq 0 ]; then
# partition exists
perc=`df /var/log|tail -n 1|awk '{print $5}'|tr -d %`
if [ $perc -gt 80 ];then
# Erase the 5 greater files
cd /var/log
for file in `ls -Sr /var/log |tail -n 5`; do > $file; done
#restart domoticz to flush logfile
service rsyslog restart
restartDomoticz
fi
fi
# check that /tmp partition is not full
df /tmp>/dev/null 2>&1
if [ $? -eq 0 ]; then
# partition exists
perc=`df /tmp|tail -n 1|awk '{print $5}'|tr -d %`
if [ $perc -gt 80 ];then
# Erase the 10 greater files
cd /tmp
rm `ls -Sr /tmp |tail -n 10`
#remove video and pictures
rm *.mp4 *.jpg *.png 2>/dev/null
# now restart domoticz to free the domoticz.log file and let domoticz write a new log
restartDomoticz
fi
fi
sleep 60
done