Project

General

Profile

Download (5.2 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-2013 BSD Perimeter
7
# Copyright (c) 2013-2016 Electric Sheep Fencing
8
# Copyright (c) 2014-2018 Rubicon Communications, LLC (Netgate)
9
# All rights reserved.
10
#
11
# Licensed under the Apache License, Version 2.0 (the "License");
12
# you may not use this file except in compliance with the License.
13
# You may obtain a copy of the License at
14
#
15
# http://www.apache.org/licenses/LICENSE-2.0
16
#
17
# Unless required by applicable law or agreed to in writing, software
18
# distributed under the License is distributed on an "AS IS" BASIS,
19
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
# See the License for the specific language governing permissions and
21
# limitations under the License.
22

    
23
# Format of file should be delimited by |
24
#  Field 1:  Source IP
25
#  Field 2:  Destination IP
26
#  Field 3:  Ping count
27
#  Field 4:  Script to run when service is down
28
#  Field 5:  Script to run once service is restored
29
#  Field 6:  Ping time threshold
30
#  Field 7:  Wan ping time threshold
31
#  Field 8:  Address family
32

    
33
# Read in ipsec ping hosts and check the CARP status
34
# Only perform this check if there are IPsec hosts to ping, see
35
#   https://redmine.pfsense.org/issues/8172
36
if [ -f /var/db/ipsecpinghosts -a -s /var/db/ipsecpinghosts ]; then
37
	IPSECHOSTS="/var/db/ipsecpinghosts"
38
	CURRENTIPSECHOSTS="/var/db/currentipsecpinghosts"
39
	IFVPNSTATE=`ifconfig $IFVPN | grep "carp: BACKUP vhid" | wc -l`
40
	if [ $IFVPNSTATE -gt 1 ]; then
41
		echo -e "CARP interface in BACKUP (not pinging ipsec hosts)"
42
		rm -f $CURRENTIPSECHOSTS
43
		touch $CURRENTIPSECHOSTS
44
	else
45
		echo -e "CARP interface is MASTER or non CARP (pinging ipsec hosts)"
46
		cat < $IPSECHOSTS > $CURRENTIPSECHOSTS
47
	fi
48
fi
49

    
50
# General file meant for user consumption
51
if [ -f /var/db/hosts ]; then
52
	HOSTS="/var/db/hosts"
53
fi
54

    
55
# Package specific ping requests
56
if [ -f /var/db/pkgpinghosts ]; then
57
	PKGHOSTS="/var/db/pkgpinghosts"
58
fi
59

    
60
# Make sure at least one of these has contents, otherwise cat will be stuck waiting on input
61
if [ ! -z "${PKGHOSTS}" -o ! -z "${HOSTS}" -o ! -z "${CURRENTIPSECHOSTS}" ]; then
62
	cat $PKGHOSTS $HOSTS $CURRENTIPSECHOSTS >/tmp/tmpHOSTS
63
else
64
	# Nothing to do!
65
	exit
66
fi
67

    
68
if [ ! -d /var/db/pingstatus ]; then
69
	/bin/mkdir -p /var/db/pingstatus
70
fi
71

    
72
if [ ! -d /var/db/pingmsstatus ]; then
73
	/bin/mkdir -p /var/db/pingmsstatus
74
fi
75

    
76
PINGHOSTS=`cat /tmp/tmpHOSTS`
77

    
78
PINGHOSTCOUNT=`cat /tmp/tmpHOSTS | wc -l`
79

    
80
if [ "$PINGHOSTCOUNT" -lt "1" ]; then
81
	exit
82
fi
83

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

    
156
exit 0
(12-12/14)