1
|
#!/bin/sh
|
2
|
|
3
|
# pfSense ping helper
|
4
|
# written by Scott Ullrich
|
5
|
# (C)2006 Scott Ullrich
|
6
|
# All rights reserved.
|
7
|
|
8
|
# Format of file should be deliminted by |
|
9
|
# Field 1: Source ip
|
10
|
# Field 2: Destination ip
|
11
|
# Field 3: Ping count
|
12
|
# Field 4: Script to run when service is down
|
13
|
# Field 5: Script to run once service is restored
|
14
|
# Field 6: Ping time threshold
|
15
|
# Field 7: Wan ping time threshold
|
16
|
# Field 8: Address family
|
17
|
|
18
|
# Read in ipsec ping hosts and check the CARP status
|
19
|
if [ -f /var/db/ipsecpinghosts ]; then
|
20
|
IPSECHOSTS="/var/db/ipsecpinghosts"
|
21
|
CURRENTIPSECHOSTS="/var/db/currentipsecpinghosts"
|
22
|
IFVPNSTATE=`ifconfig $IFVPN | grep "carp: BACKUP vhid" | wc -l`
|
23
|
if [ $IFVPNSTATE -gt 1 ]; then
|
24
|
echo -e "CARP interface in BACKUP (not pinging ipsec hosts)"
|
25
|
rm -f $CURRENTIPSECHOSTS
|
26
|
touch $CURRENTIPSECHOSTS
|
27
|
else
|
28
|
echo -e "CARP interface is MASTER or non CARP (pinging ipsec hosts)"
|
29
|
cat < $IPSECHOSTS > $CURRENTIPSECHOSTS
|
30
|
fi
|
31
|
fi
|
32
|
|
33
|
# General file meant for user consumption
|
34
|
if [ -f /var/db/hosts ]; then
|
35
|
HOSTS="/var/db/hosts"
|
36
|
fi
|
37
|
|
38
|
# Package specific ping requests
|
39
|
if [ -f /var/db/pkgpinghosts ]; then
|
40
|
PKGHOSTS="/var/db/pkgpinghosts"
|
41
|
fi
|
42
|
|
43
|
cat $PKGHOSTS $HOSTS $IPSECHOSTS >/tmp/tmpHOSTS
|
44
|
|
45
|
if [ ! -d /var/db/pingstatus ]; then
|
46
|
/bin/mkdir -p /var/db/pingstatus
|
47
|
fi
|
48
|
|
49
|
if [ ! -d /var/db/pingmsstatus ]; then
|
50
|
/bin/mkdir -p /var/db/pingmsstatus
|
51
|
fi
|
52
|
|
53
|
PINGHOSTS=`cat /tmp/tmpHOSTS`
|
54
|
|
55
|
PINGHOSTCOUNT=`cat /tmp/tmpHOSTS | wc -l`
|
56
|
|
57
|
if [ "$PINGHOSTCOUNT" -lt "1" ]; then
|
58
|
exit
|
59
|
fi
|
60
|
|
61
|
for TOPING in $PINGHOSTS ; do
|
62
|
echo "PROCESSING $TOPING"
|
63
|
SRCIP=`echo $TOPING | cut -d"|" -f1`
|
64
|
DSTIP=`echo $TOPING | cut -d"|" -f2`
|
65
|
COUNT=`echo $TOPING | cut -d"|" -f3`
|
66
|
FAILURESCRIPT=`echo $TOPING | cut -d"|" -f4`
|
67
|
SERVICERESTOREDSCRIPT=`echo $TOPING | cut -d"|" -f5`
|
68
|
THRESHOLD=`echo $TOPING | cut -d"|" -f6`
|
69
|
WANTHRESHOLD=`echo $TOPING | cut -d"|" -f7`
|
70
|
AF=`echo $TOPING | cut -d"|" -f8`
|
71
|
if [ "$AF" == "inet6" ]; then
|
72
|
PINGCMD=ping6
|
73
|
else
|
74
|
PINGCMD=ping
|
75
|
fi
|
76
|
echo Processing $DSTIP
|
77
|
# Look for a service being down
|
78
|
# Read in previous status
|
79
|
PREVIOUSSTATUS=""
|
80
|
if [ -f "/var/db/pingstatus/${DSTIP}" ]; then
|
81
|
PREVIOUSSTATUS=`cat /var/db/pingstatus/$DSTIP`
|
82
|
fi
|
83
|
$PINGCMD -c $COUNT -S $SRCIP $DSTIP
|
84
|
if [ $? -eq 0 ]; then
|
85
|
# Host is up
|
86
|
if [ "$PREVIOUSSTATUS" != "UP" ]; then
|
87
|
# Service restored
|
88
|
echo "UP" > /var/db/pingstatus/$DSTIP
|
89
|
if [ "$SERVICERESTOREDSCRIPT" != "" ]; then
|
90
|
echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT"
|
91
|
echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT" | logger -p daemon.info -i -t PingMonitor
|
92
|
sh -c $SERVICERESTOREDSCRIPT
|
93
|
fi
|
94
|
fi
|
95
|
else
|
96
|
# Host is down
|
97
|
if [ "$PREVIOUSSTATUS" != "DOWN" ]; then
|
98
|
# Service is down
|
99
|
echo "DOWN" > /var/db/pingstatus/$DSTIP
|
100
|
if [ "$FAILURESCRIPT" != "" ]; then
|
101
|
echo "$DSTIP is DOWN, previous state was UP .. Running $FAILURESCRIPT"
|
102
|
echo "$DSTIP is DOWN, previous state was UP .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
103
|
sh -c $FAILURESCRIPT
|
104
|
fi
|
105
|
fi
|
106
|
fi
|
107
|
echo "Checking ping time $DSTIP"
|
108
|
# Look at ping values themselves
|
109
|
PINGTIME=`$PINGCMD -c 1 -S $SRCIP $DSTIP | awk '{ print $7 }' | grep time | cut -d "=" -f2`
|
110
|
echo "Ping returned $?"
|
111
|
echo $PINGTIME > /var/db/pingmsstatus/$DSTIP
|
112
|
if [ "$THRESHOLD" != "" ]; then
|
113
|
if [ $(echo "${PINGTIME} > ${THRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
|
114
|
echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT"
|
115
|
echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
116
|
sh -c $FAILURESCRIPT
|
117
|
fi
|
118
|
fi
|
119
|
# Wan ping time threshold
|
120
|
#WANTIME=`rrdtool fetch /var/db/rrd/wan-quality.rrd AVERAGE -r 120 -s -1min -e -1min | grep ":" | cut -f3 -d" " | cut -d"e" -f1`
|
121
|
echo "Checking wan ping time $WANTIME"
|
122
|
echo $WANTIME > /var/db/wanaverage
|
123
|
if [ "$WANTHRESHOLD" != "" -a "$WANTIME" != "" ]; then
|
124
|
if [ $(echo "${WANTIME} > ${WANTHRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
|
125
|
echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT"
|
126
|
echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
127
|
sh -c $FAILURESCRIPT
|
128
|
fi
|
129
|
fi
|
130
|
sleep 1
|
131
|
done
|
132
|
|
133
|
exit 0
|
134
|
|