-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.sh
executable file
·119 lines (95 loc) · 2.82 KB
/
monitor.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
#!/bin/bash
# USB-RLY02 Network Monitor
# Copyright (C) 2014 Eugenio Bonifacio
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
CONTROLLER_DIR="`dirname \"$0\"`"
DATE=$(date +"%d-%m-%Y %T")
CONTROLLER="$CONTROLLER_DIR/rly02.py"
FILE="monitor.dat"
HOSTS="http://www.google.com http://192.168.1.14"
#HOSTS="http://www.google.com http://www.repubblica.it"
RETRIES=`cat $FILE`
MAX_RETRIES=2
echo $DATE
if [ ! -f $CONTROLLER ]; then
echo "Controller non trovato"
exit 1
fi
# Controllo esistenza del file
test -f $FILE
if [ $? -ne 0 ]; then
echo "File non esistente, creazione..."
touch $FILE
echo "Creato"
fi
# Controllo contenuto numerico del file
egrep -e "^([0-9]+)" $FILE &> /dev/null
# Se non ha contenuto numerico lo inizializzo a zero
if [ $? -ne 0 ]; then
echo "Inizializzazione file"
echo 0 > $FILE
fi
echo "Tentativi falliti in precedenza $RETRIES"
# Controllo gli host
FAILED=0
for HOST in $HOSTS
do
echo "Controllo '$HOST'..."
ONLINE=0
HTML=`wget -q --tries=10 --timeout=20 -O - $HOST`
if [ $? -eq 0 ]; then
#my router redirects to mobile backup if no line is detected
echo $HTML | grep "192.168.1.1" &> /dev/null
if [ $? -ne 0 ]; then
ONLINE=1
fi
fi
if [ $ONLINE -eq 1 ]; then
echo "Online"
else
echo "Offline"
FAILED=$(( $FAILED+1 ))
fi
done
# Se ci sono stati dei fallimenti, incremento i tentativi
if [ $FAILED -gt 0 ]; then
echo "Numero di host che non hanno risposto: $FAILED"
RETRIES=$(( $RETRIES+1 ))
if [ $RETRIES -ge $MAX_RETRIES ] && [ $(($RETRIES % $MAX_RETRIES)) -eq 0 ]; then
echo "Spegnimento Router"
if [ -f $CONTROLLER ]; then
echo "Spento"
python $CONTROLLER -r 1 -a on
sleep 60
echo "Acceso"
python $CONTROLLER -r 1 -a off
else
echo "Controller non trovato"
fi
#RETRIES=0 non lo azzero per utilizzarlo all'inizio dello script per verificare una situazione successiva allo spegnimento
fi
else
echo "Rete accessibile"
if [ $RETRIES -ge $MAX_RETRIES ]; then
echo "Il router è stato spento"
echo "Invio email..."
echo "Il router è stato spento" | mail -s "Monitor rete internet" root
fi
RETRIES=0
MAX_REACHED=0
fi
echo $RETRIES > $FILE
echo "Tentativi totali falliti $RETRIES"