-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.sh
executable file
·125 lines (115 loc) · 2.92 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
120
121
122
123
124
125
#!/bin/bash
function environmentTest {
if [ -f /home/pi/custodes/values/configured ] ; then
requiredFiles=(defaultGateway localHostList mtuSize publicIP remoteHost)
for i in ${requiredFiles[@]} ; do
if [[ ! -f /home/pi/custodes/values/$i ]] ; then
logger "Custodes: event=missing_config_file=$i"
cleanRun=1
exit 1
fi
done
else
logger "Custodes: event=no_initial_config"
echo "No initial config data found."
cleanRun=1
exit 1
fi
}
function importValues {
remoteHost=$(cat /home/pi/custodes/values/remoteHost)
mtuSize=$(cat /home/pi/custodes/values/mtuSize)
publicIP=$(cat /home/pi/custodes/values/publicIP)
localHostList=($(cat /home/pi/custodes/values/localHostList))
defaultGateway=$(netstat -nr | grep UG | awk '{print $2}')
routingInterface=$(netstat -nr | grep UG | awk '{print $8}')
}
function mtuCalc() {
#add in logic to only run sometimes; time serial divided by something
mtuSize=$1
remoteIP=$2
mode=0
pass=0
failPoint=1
while [[ $failPoint > 0 ]]; do
mtu=$(ping -D -c 1 -t 2 -s $mtuSize $remoteIP &>/dev/null)
if [ $? -eq 1 ] ; then
if [ $mode -eq 0 ] ; then
mtuSize=`expr $mtuSize - 10`
else
mtuSize=`expr $mtuSize - 1`
fi
else
if [ $pass -eq 0 ] ; then
if [ $mtuSize -ne $1 ] ; then
mtuSize=`expr $mtuSize + 9`
mode=1
pass=1
else
failPoint=0
fi
else
if [[ $mtuSize -ne $(cat /home/pi/custodes/values/mtuSize) ]] ; then
logger "Custodes: event=mtuSizeChanged oldMTU=$1 newMTU=$mtuSize"
cleanRun=1
echo $mtuSize > /home/pi/custodes/values/mtuSize
fi
failPoint=0
fi
fi
done
}
function hostPoll() {
ping -c 1 -t 5 $1 &> /dev/null
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
function localSitePoll {
for i in ${localHostList[@]} ; do
hostPoll $i
if [ $? -eq 1 ]; then
logger "Custodes: event=localHostDown ip=$i"
cleanRun=1
fi
done
}
function internetCheck() {
hostPoll $1
if [ $? -eq 1 ]; then
logger "Custodes: event=internetDown"
clearnrun=1
fi
}
function publicIPcheck() {
currentPublicIP=$(dig +short myip.opendns.com @resolver1.opendns.com)
if [ $? -eq 10 ] ; then
logger "Custodes: event=unable_to_determine_public_ip"
clearnrun=1
return 1
else
if [[ $1 != $currentPublicIP ]] ; then
logger "Custodes: event=publicIPchanged old_public_ip=$1 new_public_ip=$currentPublicIP"
echo $currentPublicIP > /home/pi/custodes/values/publicIP
fi
fi
}
cleanRun=0
environmentTest
importValues
hostPoll $defaultGateway
if [ $? -eq 0 ] ; then
publicIPcheck $publicIP
localSitePoll
internetCheck $remoteHost
mtuCalc $mtuSize $remoteHost
else
echo "Custodes: event=default_gateway_no_icmp"
fi
if [ $cleanRun -eq 0 ] ; then
logger "Custodes: event=cleanRun"
else
logger "Custodes: event=failedRun"
fi