1 |
ee498244
|
Seth Mos
|
#!/bin/sh
|
2 |
ac24dc24
|
Renato Botelho
|
#
|
3 |
|
|
# ping_hosts.sh
|
4 |
|
|
#
|
5 |
|
|
# part of pfSense (https://www.pfsense.org)
|
6 |
38809d47
|
Renato Botelho do Couto
|
# Copyright (c) 2006-2013 BSD Perimeter
|
7 |
|
|
# Copyright (c) 2013-2016 Electric Sheep Fencing
|
8 |
8f585441
|
Luiz Souza
|
# Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
|
9 |
ee498244
|
Seth Mos
|
# All rights reserved.
|
10 |
ac24dc24
|
Renato Botelho
|
#
|
11 |
b12ea3fb
|
Renato Botelho
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
12 |
|
|
# you may not use this file except in compliance with the License.
|
13 |
|
|
# You may obtain a copy of the License at
|
14 |
ac24dc24
|
Renato Botelho
|
#
|
15 |
b12ea3fb
|
Renato Botelho
|
# http://www.apache.org/licenses/LICENSE-2.0
|
16 |
ac24dc24
|
Renato Botelho
|
#
|
17 |
b12ea3fb
|
Renato Botelho
|
# Unless required by applicable law or agreed to in writing, software
|
18 |
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
19 |
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
20 |
|
|
# See the License for the specific language governing permissions and
|
21 |
|
|
# limitations under the License.
|
22 |
ee498244
|
Seth Mos
|
|
23 |
7d61beba
|
Phil Davis
|
# Format of file should be delimited by |
|
24 |
|
|
# Field 1: Source IP
|
25 |
|
|
# Field 2: Destination IP
|
26 |
ee498244
|
Seth Mos
|
# Field 3: Ping count
|
27 |
|
|
# Field 4: Script to run when service is down
|
28 |
|
|
# Field 5: Script to run once service is restored
|
29 |
|
|
# Field 6: Ping time threshold
|
30 |
|
|
# Field 7: Wan ping time threshold
|
31 |
e3e85044
|
Seth Mos
|
# Field 8: Address family
|
32 |
ee498244
|
Seth Mos
|
|
33 |
|
|
# Read in ipsec ping hosts and check the CARP status
|
34 |
45d078c5
|
jim-p
|
# Only perform this check if there are IPsec hosts to ping, see
|
35 |
|
|
# https://redmine.pfsense.org/issues/8172
|
36 |
|
|
if [ -f /var/db/ipsecpinghosts -a -s /var/db/ipsecpinghosts ]; then
|
37 |
ee498244
|
Seth Mos
|
IPSECHOSTS="/var/db/ipsecpinghosts"
|
38 |
|
|
CURRENTIPSECHOSTS="/var/db/currentipsecpinghosts"
|
39 |
|
|
IFVPNSTATE=`ifconfig $IFVPN | grep "carp: BACKUP vhid" | wc -l`
|
40 |
|
|
if [ $IFVPNSTATE -gt 1 ]; then
|
41 |
|
|
echo -e "CARP interface in BACKUP (not pinging ipsec hosts)"
|
42 |
|
|
rm -f $CURRENTIPSECHOSTS
|
43 |
|
|
touch $CURRENTIPSECHOSTS
|
44 |
|
|
else
|
45 |
|
|
echo -e "CARP interface is MASTER or non CARP (pinging ipsec hosts)"
|
46 |
|
|
cat < $IPSECHOSTS > $CURRENTIPSECHOSTS
|
47 |
|
|
fi
|
48 |
|
|
fi
|
49 |
|
|
|
50 |
|
|
# General file meant for user consumption
|
51 |
|
|
if [ -f /var/db/hosts ]; then
|
52 |
|
|
HOSTS="/var/db/hosts"
|
53 |
|
|
fi
|
54 |
|
|
|
55 |
|
|
# Package specific ping requests
|
56 |
|
|
if [ -f /var/db/pkgpinghosts ]; then
|
57 |
|
|
PKGHOSTS="/var/db/pkgpinghosts"
|
58 |
|
|
fi
|
59 |
|
|
|
60 |
45d078c5
|
jim-p
|
# Make sure at least one of these has contents, otherwise cat will be stuck waiting on input
|
61 |
|
|
if [ ! -z "${PKGHOSTS}" -o ! -z "${HOSTS}" -o ! -z "${CURRENTIPSECHOSTS}" ]; then
|
62 |
|
|
cat $PKGHOSTS $HOSTS $CURRENTIPSECHOSTS >/tmp/tmpHOSTS
|
63 |
|
|
else
|
64 |
|
|
# Nothing to do!
|
65 |
|
|
exit
|
66 |
|
|
fi
|
67 |
ee498244
|
Seth Mos
|
|
68 |
|
|
if [ ! -d /var/db/pingstatus ]; then
|
69 |
|
|
/bin/mkdir -p /var/db/pingstatus
|
70 |
|
|
fi
|
71 |
|
|
|
72 |
|
|
if [ ! -d /var/db/pingmsstatus ]; then
|
73 |
|
|
/bin/mkdir -p /var/db/pingmsstatus
|
74 |
|
|
fi
|
75 |
|
|
|
76 |
|
|
PINGHOSTS=`cat /tmp/tmpHOSTS`
|
77 |
|
|
|
78 |
|
|
PINGHOSTCOUNT=`cat /tmp/tmpHOSTS | wc -l`
|
79 |
|
|
|
80 |
|
|
if [ "$PINGHOSTCOUNT" -lt "1" ]; then
|
81 |
|
|
exit
|
82 |
|
|
fi
|
83 |
|
|
|
84 |
|
|
for TOPING in $PINGHOSTS ; do
|
85 |
|
|
echo "PROCESSING $TOPING"
|
86 |
|
|
SRCIP=`echo $TOPING | cut -d"|" -f1`
|
87 |
|
|
DSTIP=`echo $TOPING | cut -d"|" -f2`
|
88 |
|
|
COUNT=`echo $TOPING | cut -d"|" -f3`
|
89 |
|
|
FAILURESCRIPT=`echo $TOPING | cut -d"|" -f4`
|
90 |
|
|
SERVICERESTOREDSCRIPT=`echo $TOPING | cut -d"|" -f5`
|
91 |
|
|
THRESHOLD=`echo $TOPING | cut -d"|" -f6`
|
92 |
|
|
WANTHRESHOLD=`echo $TOPING | cut -d"|" -f7`
|
93 |
e3e85044
|
Seth Mos
|
AF=`echo $TOPING | cut -d"|" -f8`
|
94 |
aff70640
|
Seth Mos
|
if [ "$AF" == "inet6" ]; then
|
95 |
e3e85044
|
Seth Mos
|
PINGCMD=ping6
|
96 |
aff70640
|
Seth Mos
|
else
|
97 |
|
|
PINGCMD=ping
|
98 |
e3e85044
|
Seth Mos
|
fi
|
99 |
ee498244
|
Seth Mos
|
echo Processing $DSTIP
|
100 |
|
|
# Look for a service being down
|
101 |
cd14bb19
|
Renato Botelho
|
# Read in previous status
|
102 |
|
|
PREVIOUSSTATUS=""
|
103 |
|
|
if [ -f "/var/db/pingstatus/${DSTIP}" ]; then
|
104 |
|
|
PREVIOUSSTATUS=`cat /var/db/pingstatus/$DSTIP`
|
105 |
|
|
fi
|
106 |
e3e85044
|
Seth Mos
|
$PINGCMD -c $COUNT -S $SRCIP $DSTIP
|
107 |
bfa49cae
|
Chris Buechler
|
if [ $? -eq 0 ]; then
|
108 |
|
|
# Host is up
|
109 |
cd14bb19
|
Renato Botelho
|
if [ "$PREVIOUSSTATUS" != "UP" ]; then
|
110 |
bfa49cae
|
Chris Buechler
|
# Service restored
|
111 |
d1d3428a
|
Renato Botelho
|
echo "UP" > /var/db/pingstatus/$DSTIP
|
112 |
bfa49cae
|
Chris Buechler
|
if [ "$SERVICERESTOREDSCRIPT" != "" ]; then
|
113 |
|
|
echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT"
|
114 |
|
|
echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT" | logger -p daemon.info -i -t PingMonitor
|
115 |
|
|
sh -c $SERVICERESTOREDSCRIPT
|
116 |
|
|
fi
|
117 |
|
|
fi
|
118 |
|
|
else
|
119 |
|
|
# Host is down
|
120 |
cd14bb19
|
Renato Botelho
|
if [ "$PREVIOUSSTATUS" != "DOWN" ]; then
|
121 |
bfa49cae
|
Chris Buechler
|
# Service is down
|
122 |
d1d3428a
|
Renato Botelho
|
echo "DOWN" > /var/db/pingstatus/$DSTIP
|
123 |
bfa49cae
|
Chris Buechler
|
if [ "$FAILURESCRIPT" != "" ]; then
|
124 |
|
|
echo "$DSTIP is DOWN, previous state was UP .. Running $FAILURESCRIPT"
|
125 |
|
|
echo "$DSTIP is DOWN, previous state was UP .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
126 |
|
|
sh -c $FAILURESCRIPT
|
127 |
|
|
fi
|
128 |
|
|
fi
|
129 |
|
|
fi
|
130 |
|
|
echo "Checking ping time $DSTIP"
|
131 |
|
|
# Look at ping values themselves
|
132 |
e3e85044
|
Seth Mos
|
PINGTIME=`$PINGCMD -c 1 -S $SRCIP $DSTIP | awk '{ print $7 }' | grep time | cut -d "=" -f2`
|
133 |
bfa49cae
|
Chris Buechler
|
echo "Ping returned $?"
|
134 |
|
|
echo $PINGTIME > /var/db/pingmsstatus/$DSTIP
|
135 |
|
|
if [ "$THRESHOLD" != "" ]; then
|
136 |
67e86129
|
Renato Botelho
|
if [ $(echo "${PINGTIME} > ${THRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
|
137 |
bfa49cae
|
Chris Buechler
|
echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT"
|
138 |
|
|
echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
139 |
|
|
sh -c $FAILURESCRIPT
|
140 |
|
|
fi
|
141 |
|
|
fi
|
142 |
|
|
# Wan ping time threshold
|
143 |
020b954f
|
Chris Buechler
|
#WANTIME=`rrdtool fetch /var/db/rrd/wan-quality.rrd AVERAGE -r 120 -s -1min -e -1min | grep ":" | cut -f3 -d" " | cut -d"e" -f1`
|
144 |
bfa49cae
|
Chris Buechler
|
echo "Checking wan ping time $WANTIME"
|
145 |
|
|
echo $WANTIME > /var/db/wanaverage
|
146 |
72a95734
|
Renato Botelho
|
if [ "$WANTHRESHOLD" != "" -a "$WANTIME" != "" ]; then
|
147 |
67e86129
|
Renato Botelho
|
if [ $(echo "${WANTIME} > ${WANTHRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
|
148 |
bfa49cae
|
Chris Buechler
|
echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT"
|
149 |
|
|
echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
150 |
|
|
sh -c $FAILURESCRIPT
|
151 |
|
|
fi
|
152 |
|
|
fi
|
153 |
|
|
sleep 1
|
154 |
ee498244
|
Seth Mos
|
done
|
155 |
|
|
|
156 |
|
|
exit 0
|