-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlanmonitor_controller2.sh
executable file
·126 lines (100 loc) · 2.89 KB
/
wlanmonitor_controller2.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
#!/bin/bash
# This script is written to make sure wlanmonitor.service runs all the time. If wlanmonitor seems not to be looping then it dumps some logs, restarts RPi and sends the logs at the next boot.
NO_LOOPING_ELAPSE=600
EXEC_DIR=/home/pi/work/wlan-monitor
LOG_DIR=$EXEC_DIR/log
echo "wlanmonitor_controller starts."
function dump_logs()
{
echo "In dump_logs()"
# get kernel log
dmesg > "$LOG_DIR/dmesg.txt"
# get wlanmonitor service log
systemctl -n 500 status wlanmonitor > "$LOG_DIR/systemctl_status.txt"
# get syslog
tail -n 2000 /var/log/syslog > "$LOG_DIR/syslog.txt"
# get mail log
tail -n 500 /var/log/mail.log > "$LOG_DIR/mail-log.txt"
tail -n 500 /var/log/mail.err > "$LOG_DIR/mail-err.txt"
}
function send_logs()
{
echo "In send_logs()"
mail_cmd_line="echo '' | "
mail_cmd_line+="mail -s \"Message from wlanmonitor_controller. This message has been sent because wlanmonitor_controller can't recover wlanmonitor from error and thus decide to reboot.\" "
if [ -s "$LOG_DIR/dmesg.txt" ]; then
mail_cmd_line+="-A \"$LOG_DIR/dmesg.txt\" "
fi
if [ -s "$LOG_DIR/systemctl_status.txt" ]; then
mail_cmd_line+="-A \"$LOG_DIR/systemctl_status.txt\" "
fi
if [ -s "$LOG_DIR/syslog.txt" ]; then
mail_cmd_line+="-A \"$LOG_DIR/syslog.txt\" "
fi
if [ -s "$LOG_DIR/mail-log.txt" ]; then
mail_cmd_line+="-A \"$LOG_DIR/mail-log.txt\" "
fi
if [ -s "$LOG_DIR/mail-err.txt" ]; then
mail_cmd_line+="-A \"$LOG_DIR/mail-err.txt\" "
fi
mail_cmd_line+="[email protected],[email protected]"
eval $mail_cmd_line
echo "mail has been sent with logs."
}
function connect_to_internet()
{
wlanmonitor_startup2.sh
${EXEC_DIR}/wifiap_evaluate2.sh associate wlan0 coffeebreak_5g coffeebreak1**
sleep 10
${EXEC_DIR}/wifiap_evaluate2.sh connect_internet wlan0
sleep 10
if [ $? -eq 0 ]; then
return 0
fi
return 1
}
sleep 20
# send logs via email if there's some to send
if find $LOG_DIR -type f -name "*.txt" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
echo "Trying to stop wlanmonitor.."
systemctl stop wlanmonitor
sleep 1
connect_to_internet
if [ $? -eq 0 ]; then
send_logs
sleep 30
else
echo "Cannot connect to internet."
fi
find $LOG_DIR -type f -name "*.txt" -exec rm -rv {} \;
echo "Restaring wlanmonitor.."
systemctl start wlanmonitor
echo "Restarted wlanmonitor.."
sleep 1
fi
sleep 600
# controller main loop
while :
do
# Check if the process continues looping
stamp=$(</var/opt/wlanmonitor_tick)
if [ -z "$stamp" ]; then
stamp=0
fi
echo "$stamp"
now=$(date +%s 2>&1)
elapse="$(($now-$stamp))"
echo "$elapse"
if (( $elapse > $NO_LOOPING_ELAPSE )); then
echo "wlanmonitor seems to be not looping.. Logs are dumped."
dump_logs
sleep 5
echo "Forcing to reboot.."
systemctl --force reboot
else
echo "wlanmonitor loops okay."
fi
sleep 600
done
exit 1
# eof