Project

General

Profile

Download (4.19 KB) Statistics
| Branch: | Tag: | Revision:
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 7d61beba Phil Davis
# Format of file should be delimited by |
9
#  Field 1:  Source IP
10
#  Field 2:  Destination IP
11 ee498244 Seth Mos
#  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 e3e85044 Seth Mos
#  Field 8:  Address family
17 ee498244 Seth Mos
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 7e1aa4b7 Chris Buechler
cat $PKGHOSTS $HOSTS $CURRENTIPSECHOSTS >/tmp/tmpHOSTS
44 ee498244 Seth Mos
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 e3e85044 Seth Mos
	AF=`echo $TOPING | cut -d"|" -f8`
71 aff70640 Seth Mos
	if [ "$AF" == "inet6" ]; then
72 e3e85044 Seth Mos
		PINGCMD=ping6
73 aff70640 Seth Mos
	else
74
		PINGCMD=ping
75 e3e85044 Seth Mos
	fi
76 ee498244 Seth Mos
	echo Processing $DSTIP
77
	# Look for a service being down
78 cd14bb19 Renato Botelho
	# Read in previous status
79
	PREVIOUSSTATUS=""
80
	if [ -f "/var/db/pingstatus/${DSTIP}" ]; then
81
		PREVIOUSSTATUS=`cat /var/db/pingstatus/$DSTIP`
82
	fi
83 e3e85044 Seth Mos
	$PINGCMD -c $COUNT -S $SRCIP $DSTIP
84 bfa49cae Chris Buechler
	if [ $? -eq 0 ]; then
85
		# Host is up
86 cd14bb19 Renato Botelho
		if [ "$PREVIOUSSTATUS" != "UP" ]; then
87 bfa49cae Chris Buechler
			# Service restored
88 d1d3428a Renato Botelho
			echo "UP" > /var/db/pingstatus/$DSTIP
89 bfa49cae Chris Buechler
			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 cd14bb19 Renato Botelho
		if [ "$PREVIOUSSTATUS" != "DOWN" ]; then
98 bfa49cae Chris Buechler
			# Service is down
99 d1d3428a Renato Botelho
			echo "DOWN" > /var/db/pingstatus/$DSTIP
100 bfa49cae Chris Buechler
			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 e3e85044 Seth Mos
	PINGTIME=`$PINGCMD -c 1 -S $SRCIP $DSTIP | awk '{ print $7 }' | grep time | cut -d "=" -f2`
110 bfa49cae Chris Buechler
	echo "Ping returned $?"
111
	echo $PINGTIME > /var/db/pingmsstatus/$DSTIP
112
	if [ "$THRESHOLD" != "" ]; then
113 67e86129 Renato Botelho
		if [ $(echo "${PINGTIME} > ${THRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
114 bfa49cae Chris Buechler
			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 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`
121 bfa49cae Chris Buechler
	echo "Checking wan ping time $WANTIME"
122
	echo $WANTIME > /var/db/wanaverage
123 72a95734 Renato Botelho
	if [ "$WANTHRESHOLD" != "" -a "$WANTIME" != "" ]; then
124 67e86129 Renato Botelho
		if [ $(echo "${WANTIME} > ${WANTHRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
125 bfa49cae Chris Buechler
			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 ee498244 Seth Mos
done
132
133
exit 0