Project

General

Profile

Feature #7895 » check_carp_freebsd.sh

Stéphane Lapie, 09/27/2017 05:14 AM

 
1
#!/bin/sh
2
# Check CARP status on BSD machines
3
# - Works with either no parameter on standard configurations (slave has an advskew > 0)
4
# - Possible to override expected state definition with -M/-B flags
5
#
6
# Author : Stephane LAPIE <stephane.lapie@asahinet.com>
7

    
8
cd `dirname $0`
9
DIR=`pwd`
10
SCRIPT=`basename $0`
11

    
12
usage()
13
{
14
	echo "Usage: $0 [-h] [-B|-M] [NIC]"
15
	echo "	-B : Expects the NICs to be in BACKUP mode"
16
	echo "	-M : Expects the NICs to be in MASTER mode"
17
	echo "	If no NICs are specified, all currently CARP enabled ones will be checked"
18
	exit 3
19
}
20

    
21
exit_nagios()
22
{
23
	case $1 in
24
		0)
25
			echo "OK - $2"
26
			;;
27
		1)
28
			echo "WARNING - $2"
29
			;;
30
		2)
31
			echo "CRITICAL - $2"
32
			;;
33
		*)
34
			echo "UNKNOWN - $2"
35
			;;
36
	esac
37
}
38

    
39
###############################################################################
40
# Check OS
41

    
42
OS=`uname -s`
43
if ! (echo "$OS" | grep -E "FreeBSD|OpenBSD" >/dev/null) ; then
44
	echo "UNKNOWN - OS '$OS' not supported by this script"
45
	exit 3
46
fi
47

    
48
###############################################################################
49
# Check if CARP is used
50

    
51
CARP_OUTPUT=`ifconfig | grep "carp:"`
52
if [ -z "$CARP_OUTPUT" ] ; then
53
	exit_nagios 3 "No NIC is CARP enabled"
54
fi
55

    
56
###############################################################################
57
# Parse arguments
58

    
59
OK_MODE=ADVSKEW
60
NG_MODE=""
61

    
62
GETOPT_TEMP=`getopt MBh "$@"`
63
eval set -- "$GETOPT_TEMP"
64

    
65
while true ; do
66
	case "$1" in
67
		-M)
68
			OK_MODE=MASTER;
69
			NG_MODE=BACKUP;
70
			shift
71
			;;
72
		-B)
73
			OK_MODE=BACKUP;
74
			NG_MODE=MASTER;
75
			shift
76
			;;
77
		-h)
78
			usage
79
			;;
80
		--) shift ; break ;;
81
		*) echo "Internal error!"; exit 3 ;;
82
	esac
83
done
84

    
85
###############################################################################
86
# Check NIC list
87

    
88
NIC_LIST=`ifconfig -l`
89
NICS="$@"
90

    
91
# If no NIC was specified only keep the CARP enabled ones
92
if [ -z "$NICS" ] ; then
93
	for nic in $NIC_LIST ; do
94
		if (ifconfig $nic | grep "carp:" >/dev/null) ; then
95
			NICS="$NICS $nic"
96
		fi
97
	done
98
fi
99

    
100
return_code=0 # Default to OK
101
return_msg="CARP Status :"
102
# Check status of each CARP enabled NIC
103
for nic in $NICS ; do
104
	carp_output=`ifconfig $nic | grep "carp:" | tr -d '\t'`
105
	# carp: MASTER vhid 232 advbase 1 advskew 0
106
	# carp: BACKUP vhid 232 advbase 1 advskew 100
107
	carp_status=`echo "$carp_output" | sed 's/^carp: //;s/ .*//'`
108
	# MASTER
109
	# BACKUP
110
	if [ -z "$carp_status" ] ; then # NIC was not CARP enabled ?
111
		return_msg="${return_msg} ${nic} is not CARP enabled"
112
		if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
113
	else
114
		carp_advskew=`echo "$carp_output" | sed 's/.* advskew \([0-9][0-9]*\)/\1/'`
115
		# advskew 0 -> should be MASTER
116
		# advskew > 0 -> should be BACKUP
117

    
118
		return_msg="${return_msg} ${nic} is ${carp_status}"
119
		case $OK_MODE in
120
			MASTER|BACKUP)
121
				if [ "$carp_status" == "$OK_MODE" ] ; then
122
					return_msg="${return_msg} (OK)"
123
				elif [ "$carp_status" == "$NG_MODE" ] ; then
124
					return_msg="${return_msg} (CRITICAL)"
125
					if [ "$return_code" -lt 2 ] ; then return_code=2 ; fi
126
				else
127
					return_msg="${return_msg} (UNKNOWN)"
128
					if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
129
				fi
130
				;;
131
			ADVSKEW)
132
				if [ "$carp_advskew" -eq 0 ] ; then # advskew > 0, should be BACKUP
133
					if [ "$carp_status" = "MASTER" ] ; then
134
						return_msg="${return_msg} (OK)"
135
					elif [ "$carp_status" = "BACKUP" ] ; then
136
						return_msg="${return_msg} (CRITICAL)"
137
						if [ "$return_code" -lt 2 ] ; then return_code=2 ; fi
138
					else
139
						return_msg="${return_msg} (UNKNOWN)"
140
						if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
141
					fi
142
				else
143
					if [ "$carp_status" = "BACKUP" ] ; then
144
						return_msg="${return_msg} (OK)"
145
					elif [ "$carp_status" = "MASTER" ] ; then
146
						return_msg="${return_msg} (CRITICAL)"
147
						if [ "$return_code" -lt 2 ] ; then return_code=2 ; fi
148
					else
149
						return_msg="${return_msg} (UNKNOWN)"
150
						if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
151
					fi
152
				fi
153
				;;
154
		esac
155
	fi
156
	return_msg="${return_msg};"
157
done
158

    
159
exit_nagios "$return_code" "$return_msg"
(1-1/3)