Project

General

Profile

Download (4.03 KB) Statistics
| Branch: | Tag: | Revision:
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

    
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
	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 "UP" > /var/db/pingstatus/$DSTIP
80
				echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT"
81
				echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT" | logger -p daemon.info -i -t PingMonitor
82
				sh -c $SERVICERESTOREDSCRIPT
83
			fi
84
		fi
85
		echo "UP" > /var/db/pingstatus/$DSTIP
86
	else
87
		# Host is down
88
		PREVIOUSSTATUS=`cat /var/db/pingstatus/$DSTIP`
89
		if [ "$PREVIOUSSTATUS" = "UP" ]; then
90
			# Service is down
91
			if [ "$FAILURESCRIPT" != "" ]; then
92
				echo "DOWN" > /var/db/pingstatus/$DSTIP
93
				echo "$DSTIP is DOWN, previous state was UP ..  Running $FAILURESCRIPT"
94
				echo "$DSTIP is DOWN, previous state was UP ..  Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
95
				sh -c $FAILURESCRIPT
96
			fi
97
		fi
98
		echo "DOWN" > /var/db/pingstatus/$DSTIP
99
	fi
100
	echo "Checking ping time $DSTIP"
101
	# Look at ping values themselves
102
	PINGTIME=`ping -c 1 -S $SRCIP $DSTIP | awk '{ print $7 }' | grep time | cut -d "=" -f2`
103
	echo "Ping returned $?"
104
	echo $PINGTIME > /var/db/pingmsstatus/$DSTIP
105
	if [ "$THRESHOLD" != "" ]; then
106
		if [ "$PINGTIME" -gt "$THRESHOLD" ]; then
107
			echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT"
108
			echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
109
			sh -c $FAILURESCRIPT
110
		fi
111
	fi
112
	# Wan ping time threshold
113
	WANTIME=`rrdtool fetch /var/db/rrd/wan-quality.rrd AVERAGE -r 120 -s -1min -e -1min | grep ":" | cut -f3 -d" " | cut -d"e" -f1`
114
	echo "Checking wan ping time $WANTIME"
115
	echo $WANTIME > /var/db/wanaverage
116
	if [ "$WANTHRESHOLD" != "" ]; then
117
		if [ "$WANTIME" -gt "$WANTHRESHOLD" ]; then
118
			echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT"
119
			echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
120
			sh -c $FAILURESCRIPT
121
		fi
122
	fi
123
	sleep 1
124
done
125

    
126
exit 0
127

    
(4-4/7)