Project

General

Profile

Download (6.41 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# ping_hosts.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2006-2016 Rubicon Communications, LLC (Netgate)
7
# All rights reserved.
8
#
9
# Redistribution and use in source and binary forms, with or without
10
# modification, are permitted provided that the following conditions are met:
11
#
12
# 1. Redistributions of source code must retain the above copyright notice,
13
#    this list of conditions and the following disclaimer.
14
#
15
# 2. Redistributions in binary form must reproduce the above copyright
16
#    notice, this list of conditions and the following disclaimer in
17
#    the documentation and/or other materials provided with the
18
#    distribution.
19
#
20
# 3. All advertising materials mentioning features or use of this software
21
#    must display the following acknowledgment:
22
#    "This product includes software developed by the pfSense Project
23
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
#
25
# 4. The names "pfSense" and "pfSense Project" must not be used to
26
#    endorse or promote products derived from this software without
27
#    prior written permission. For written permission, please contact
28
#    coreteam@pfsense.org.
29
#
30
# 5. Products derived from this software may not be called "pfSense"
31
#    nor may "pfSense" appear in their names without prior written
32
#    permission of the Electric Sheep Fencing, LLC.
33
#
34
# 6. Redistributions of any form whatsoever must retain the following
35
#    acknowledgment:
36
#
37
# "This product includes software developed by the pfSense Project
38
# for use in the pfSense software distribution (http://www.pfsense.org/).
39
#
40
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
# OF THE POSSIBILITY OF SUCH DAMAGE.
52

    
53
# Format of file should be delimited by |
54
#  Field 1:  Source IP
55
#  Field 2:  Destination IP
56
#  Field 3:  Ping count
57
#  Field 4:  Script to run when service is down
58
#  Field 5:  Script to run once service is restored
59
#  Field 6:  Ping time threshold
60
#  Field 7:  Wan ping time threshold
61
#  Field 8:  Address family
62

    
63
# Read in ipsec ping hosts and check the CARP status
64
if [ -f /var/db/ipsecpinghosts ]; then
65
	IPSECHOSTS="/var/db/ipsecpinghosts"
66
	CURRENTIPSECHOSTS="/var/db/currentipsecpinghosts"
67
	IFVPNSTATE=`ifconfig $IFVPN | grep "carp: BACKUP vhid" | wc -l`
68
	if [ $IFVPNSTATE -gt 1 ]; then
69
		echo -e "CARP interface in BACKUP (not pinging ipsec hosts)"
70
		rm -f $CURRENTIPSECHOSTS
71
		touch $CURRENTIPSECHOSTS
72
	else
73
		echo -e "CARP interface is MASTER or non CARP (pinging ipsec hosts)"
74
		cat < $IPSECHOSTS > $CURRENTIPSECHOSTS
75
	fi
76
fi
77

    
78
# General file meant for user consumption
79
if [ -f /var/db/hosts ]; then
80
	HOSTS="/var/db/hosts"
81
fi
82

    
83
# Package specific ping requests
84
if [ -f /var/db/pkgpinghosts ]; then
85
	PKGHOSTS="/var/db/pkgpinghosts"
86
fi
87

    
88
cat $PKGHOSTS $HOSTS $CURRENTIPSECHOSTS >/tmp/tmpHOSTS
89

    
90
if [ ! -d /var/db/pingstatus ]; then
91
	/bin/mkdir -p /var/db/pingstatus
92
fi
93

    
94
if [ ! -d /var/db/pingmsstatus ]; then
95
	/bin/mkdir -p /var/db/pingmsstatus
96
fi
97

    
98
PINGHOSTS=`cat /tmp/tmpHOSTS`
99

    
100
PINGHOSTCOUNT=`cat /tmp/tmpHOSTS | wc -l`
101

    
102
if [ "$PINGHOSTCOUNT" -lt "1" ]; then
103
	exit
104
fi
105

    
106
for TOPING in $PINGHOSTS ; do
107
	echo "PROCESSING $TOPING"
108
	SRCIP=`echo $TOPING | cut -d"|" -f1`
109
	DSTIP=`echo $TOPING | cut -d"|" -f2`
110
	COUNT=`echo $TOPING | cut -d"|" -f3`
111
	FAILURESCRIPT=`echo $TOPING | cut -d"|" -f4`
112
	SERVICERESTOREDSCRIPT=`echo $TOPING | cut -d"|" -f5`
113
	THRESHOLD=`echo $TOPING | cut -d"|" -f6`
114
	WANTHRESHOLD=`echo $TOPING | cut -d"|" -f7`
115
	AF=`echo $TOPING | cut -d"|" -f8`
116
	if [ "$AF" == "inet6" ]; then
117
		PINGCMD=ping6
118
	else
119
		PINGCMD=ping
120
	fi
121
	echo Processing $DSTIP
122
	# Look for a service being down
123
	# Read in previous status
124
	PREVIOUSSTATUS=""
125
	if [ -f "/var/db/pingstatus/${DSTIP}" ]; then
126
		PREVIOUSSTATUS=`cat /var/db/pingstatus/$DSTIP`
127
	fi
128
	$PINGCMD -c $COUNT -S $SRCIP $DSTIP
129
	if [ $? -eq 0 ]; then
130
		# Host is up
131
		if [ "$PREVIOUSSTATUS" != "UP" ]; then
132
			# Service restored
133
			echo "UP" > /var/db/pingstatus/$DSTIP
134
			if [ "$SERVICERESTOREDSCRIPT" != "" ]; then
135
				echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT"
136
				echo "$DSTIP is UP, previous state was DOWN .. Running $SERVICERESTOREDSCRIPT" | logger -p daemon.info -i -t PingMonitor
137
				sh -c $SERVICERESTOREDSCRIPT
138
			fi
139
		fi
140
	else
141
		# Host is down
142
		if [ "$PREVIOUSSTATUS" != "DOWN" ]; then
143
			# Service is down
144
			echo "DOWN" > /var/db/pingstatus/$DSTIP
145
			if [ "$FAILURESCRIPT" != "" ]; then
146
				echo "$DSTIP is DOWN, previous state was UP ..  Running $FAILURESCRIPT"
147
				echo "$DSTIP is DOWN, previous state was UP ..  Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
148
				sh -c $FAILURESCRIPT
149
			fi
150
		fi
151
	fi
152
	echo "Checking ping time $DSTIP"
153
	# Look at ping values themselves
154
	PINGTIME=`$PINGCMD -c 1 -S $SRCIP $DSTIP | awk '{ print $7 }' | grep time | cut -d "=" -f2`
155
	echo "Ping returned $?"
156
	echo $PINGTIME > /var/db/pingmsstatus/$DSTIP
157
	if [ "$THRESHOLD" != "" ]; then
158
		if [ $(echo "${PINGTIME} > ${THRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
159
			echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT"
160
			echo "$DSTIP has exceeded ping threshold $PINGTIME / $THRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
161
			sh -c $FAILURESCRIPT
162
		fi
163
	fi
164
	# Wan ping time threshold
165
	#WANTIME=`rrdtool fetch /var/db/rrd/wan-quality.rrd AVERAGE -r 120 -s -1min -e -1min | grep ":" | cut -f3 -d" " | cut -d"e" -f1`
166
	echo "Checking wan ping time $WANTIME"
167
	echo $WANTIME > /var/db/wanaverage
168
	if [ "$WANTHRESHOLD" != "" -a "$WANTIME" != "" ]; then
169
		if [ $(echo "${WANTIME} > ${WANTHRESHOLD}" | /usr/bin/bc) -eq 1 ]; then
170
			echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT"
171
			echo "$DSTIP has exceeded wan ping threshold $WANTIME / $WANTHRESHOLD .. Running $FAILURESCRIPT" | logger -p daemon.info -i -t PingMonitor
172
			sh -c $FAILURESCRIPT
173
		fi
174
	fi
175
	sleep 1
176
done
177

    
178
exit 0
179

    
(8-8/10)