1 |
ee498244
|
Seth Mos
|
#!/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 |
|
|
|
17 |
|
|
# Read in ipsec ping hosts and check the CARP status
|
18 |
|
|
if [ -f /var/db/ipsecpinghosts ]; then
|
19 |
|
|
IPSECHOSTS="/var/db/ipsecpinghosts"
|
20 |
|
|
CURRENTIPSECHOSTS="/var/db/currentipsecpinghosts"
|
21 |
|
|
IFVPNSTATE=`ifconfig $IFVPN | grep "carp: BACKUP vhid" | wc -l`
|
22 |
|
|
if [ $IFVPNSTATE -gt 1 ]; then
|
23 |
|
|
echo -e "CARP interface in BACKUP (not pinging ipsec hosts)"
|
24 |
|
|
rm -f $CURRENTIPSECHOSTS
|
25 |
|
|
touch $CURRENTIPSECHOSTS
|
26 |
|
|
else
|
27 |
|
|
echo -e "CARP interface is MASTER or non CARP (pinging ipsec hosts)"
|
28 |
|
|
cat < $IPSECHOSTS > $CURRENTIPSECHOSTS
|
29 |
|
|
fi
|
30 |
|
|
fi
|
31 |
|
|
|
32 |
|
|
# General file meant for user consumption
|
33 |
|
|
if [ -f /var/db/hosts ]; then
|
34 |
|
|
HOSTS="/var/db/hosts"
|
35 |
|
|
fi
|
36 |
|
|
|
37 |
|
|
# Package specific ping requests
|
38 |
|
|
if [ -f /var/db/pkgpinghosts ]; then
|
39 |
|
|
PKGHOSTS="/var/db/pkgpinghosts"
|
40 |
|
|
fi
|
41 |
|
|
|
42 |
|
|
cat $PKGHOSTS $HOSTS $IPSECHOSTS >/tmp/tmpHOSTS
|
43 |
|
|
|
44 |
|
|
if [ ! -d /var/db/pingstatus ]; then
|
45 |
|
|
/bin/mkdir -p /var/db/pingstatus
|
46 |
|
|
fi
|
47 |
|
|
|
48 |
|
|
if [ ! -d /var/db/pingmsstatus ]; then
|
49 |
|
|
/bin/mkdir -p /var/db/pingmsstatus
|
50 |
|
|
fi
|
51 |
|
|
|
52 |
|
|
PINGHOSTS=`cat /tmp/tmpHOSTS`
|
53 |
|
|
|
54 |
|
|
PINGHOSTCOUNT=`cat /tmp/tmpHOSTS | wc -l`
|
55 |
|
|
|
56 |
|
|
if [ "$PINGHOSTCOUNT" -lt "1" ]; then
|
57 |
|
|
exit
|
58 |
|
|
fi
|
59 |
|
|
|
60 |
|
|
for TOPING in $PINGHOSTS ; do
|
61 |
|
|
echo "PROCESSING $TOPING"
|
62 |
|
|
SRCIP=`echo $TOPING | cut -d"|" -f1`
|
63 |
|
|
DSTIP=`echo $TOPING | cut -d"|" -f2`
|
64 |
|
|
COUNT=`echo $TOPING | cut -d"|" -f3`
|
65 |
|
|
FAILURESCRIPT=`echo $TOPING | cut -d"|" -f4`
|
66 |
|
|
SERVICERESTOREDSCRIPT=`echo $TOPING | cut -d"|" -f5`
|
67 |
|
|
THRESHOLD=`echo $TOPING | cut -d"|" -f6`
|
68 |
|
|
WANTHRESHOLD=`echo $TOPING | cut -d"|" -f7`
|
69 |
|
|
echo Processing $DSTIP
|
70 |
|
|
# Look for a service being down
|
71 |
|
|
ping -c $COUNT -S $SRCIP $DSTIP
|
72 |
bfa49cae
|
Chris Buechler
|
if [ $? -eq 0 ]; then
|
73 |
|
|
# Host is up
|
74 |
|
|
# Read in previous status
|
75 |
|
|
PREVIOUSSTATUS=`cat /var/db/pingstatus/$DSTIP`
|
76 |
|
|
if [ "$PREVIOUSSTATUS" = "DOWN" ]; then
|
77 |
|
|
# Service restored
|
78 |
|
|
if [ "$SERVICERESTOREDSCRIPT" != "" ]; then
|
79 |
|
|
echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT"
|
80 |
|
|
echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT" | logger -p daemon.info -i -t PingMonitor
|
81 |
|
|
sh -c $SERVICERESTOREDSCRIPT
|
82 |
|
|
fi
|
83 |
|
|
fi
|
84 |
|
|
echo "UP" > /var/db/pingstatus/$DSTIP
|
85 |
|
|
else
|
86 |
|
|
# Host is down
|
87 |
|
|
PREVIOUSSTATUS=`cat /var/db/pingstatus/$DSTIP`
|
88 |
|
|
if [ "$PREVIOUSSTATUS" = "UP" ]; then
|
89 |
|
|
# Service is down
|
90 |
|
|
if [ "$FAILURESCRIPT" != "" ]; then
|
91 |
|
|
echo "$DSTIP is DOWN, previous state was UP .. Running $FAILURESCRIPT"
|
92 |
|
|
echo "$DSTIP is DOWN, previous state was UP .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
93 |
|
|
sh -c $FAILURESCRIPT
|
94 |
|
|
fi
|
95 |
|
|
fi
|
96 |
|
|
echo "DOWN" > /var/db/pingstatus/$DSTIP
|
97 |
|
|
fi
|
98 |
|
|
echo "Checking ping time $DSTIP"
|
99 |
|
|
# Look at ping values themselves
|
100 |
|
|
PINGTIME=`ping -c 1 -S $SRCIP $DSTIP | awk '{ print $7 }' | grep time | cut -d "=" -f2`
|
101 |
|
|
echo "Ping returned $?"
|
102 |
|
|
echo $PINGTIME > /var/db/pingmsstatus/$DSTIP
|
103 |
|
|
if [ "$THRESHOLD" != "" ]; then
|
104 |
|
|
if [ "$PINGTIME" -gt "$THRESHOLD" ]; then
|
105 |
|
|
echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT"
|
106 |
|
|
echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
107 |
|
|
sh -c $FAILURESCRIPT
|
108 |
|
|
fi
|
109 |
|
|
fi
|
110 |
|
|
# Wan ping time threshold
|
111 |
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`
|
112 |
bfa49cae
|
Chris Buechler
|
echo "Checking wan ping time $WANTIME"
|
113 |
|
|
echo $WANTIME > /var/db/wanaverage
|
114 |
|
|
if [ "$WANTHRESHOLD" != "" ]; then
|
115 |
|
|
if [ "$WANTIME" -gt "$WANTHRESHOLD" ]; then
|
116 |
|
|
echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT"
|
117 |
|
|
echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
|
118 |
|
|
sh -c $FAILURESCRIPT
|
119 |
|
|
fi
|
120 |
|
|
fi
|
121 |
|
|
sleep 1
|
122 |
ee498244
|
Seth Mos
|
done
|
123 |
|
|
|
124 |
|
|
exit 0
|