Project

General

Profile

Feature #7895 » check_carp_freebsd.sh

Added support for multiple VHIDs per NIC - Stéphane Lapie, 10/17/2017 11:10 AM

 
1
#!/bin/sh
2
# Nagios local plugin for checking CARP status on FreeBSD
3
# By: Stephane LAPIE <stephane.lapie@asahinet.com>
4
# 2017/10/18 : Handle multiple VHIDs on a single NIC
5
# 2017/10/02 : Initial release
6

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

    
11
usage()
12
{
13
	echo "Usage: $0 [-B|--backup|-M|--master] [NIC]"
14
	exit 3
15
}
16

    
17
# Test ifconfig function for debugging on another OS or with other ifconfig output
18
#ifconfig()
19
#{
20
#	TESTFILE=/tmp/testifconfig.txt
21
#	if [ "$1" = "-l" ] ; then
22
#		cat $TESTFILE | grep "^[a-z][a-z]*[0-9][0-9]*: flags=" | cut -d : -f 1
23
#	elif [ ! -z "$1" ] ; then
24
#		cat $TESTFILE | sed "/^$1: flags=/,/^[a-z][a-z]*[0-9][0-9]*: flags=/ !D" | grep carp
25
#	else
26
#		cat $TESTFILE
27
#	fi
28
#}
29

    
30
exit_nagios()
31
{
32
	case $1 in
33
		0)
34
			echo "OK - $2"
35
			;;
36
		1)
37
			echo "WARNING - $2"
38
			;;
39
		2)
40
			echo "CRITICAL - $2"
41
			;;
42
		*)
43
			echo "UNKNOWN - $2"
44
			;;
45
	esac
46
	exit $1
47
}
48

    
49
###############################################################################
50
# Check OS
51

    
52
OS=`uname -s`
53
if ! (echo "$OS" | grep -E "FreeBSD|OpenBSD" >/dev/null) ; then
54
	echo "UNKNOWN - OS '$OS' not supported by this script"
55
	exit 3
56
fi
57

    
58
###############################################################################
59
# Check if CARP is used
60

    
61
CARP_OUTPUT=`ifconfig | grep "carp:"`
62
if [ -z "$CARP_OUTPUT" ] ; then
63
	exit_nagios 3 "No NIC is CARP enabled"
64
fi
65

    
66
###############################################################################
67
# Parse arguments
68

    
69
OK_MODE=ADVSKEW
70
NG_MODE=""
71

    
72
GETOPT_TEMP=`getopt MBh "$@"`
73
eval set -- "$GETOPT_TEMP"
74

    
75
while true ; do
76
	case "$1" in
77
		-M|--master)
78
			OK_MODE=MASTER;
79
			NG_MODE=BACKUP;
80
			shift
81
			;;
82
		-B|--backup)
83
			OK_MODE=BACKUP;
84
			NG_MODE=MASTER;
85
			shift
86
			;;
87
		-h|--help)
88
			usage
89
			;;
90
		--) shift ; break ;;
91
		*) echo "Internal error!"; exit 3 ;;
92
	esac
93
done
94

    
95
###############################################################################
96
# Check NIC list
97

    
98
NIC_LIST=`ifconfig -l`
99
NICS="$@"
100

    
101
# If no NIC was specified only keep the CARP enabled ones
102
if [ -z "$NICS" ] ; then
103
	for nic in $NIC_LIST ; do
104
		if (ifconfig $nic | grep "carp:" >/dev/null) ; then
105
			NICS="$NICS $nic"
106
		fi
107
	done
108
fi
109

    
110
return_code=0 # Default to OK
111
return_msg="CARP Status :"
112
# Check status of each CARP enabled NIC
113
for nic in $NICS ; do
114
	# carp: MASTER vhid 232 advbase 1 advskew 0
115
	# carp: BACKUP vhid 232 advbase 1 advskew 100
116
	carp_output=`ifconfig $nic | grep "carp:" | tr -d '\t'`
117
	# 232
118
	carp_vhids=`echo "$carp_output" | sed 's/^carp: [^ ][^ ]* vhid \([0-9][0-9]*\) .*/\1/'`
119

    
120
	for carp_vhid in $carp_vhids ; do
121
		# MASTER
122
		# BACKUP
123
		carp_vhid_output=`echo "$carp_output" | grep "carp: [^ ][^ ]* vhid $carp_vhid "`
124
		carp_status=`echo "$carp_vhid_output" | sed 's/^carp: //;s/ .*//'`
125

    
126
		if [ -z "$carp_status" ] ; then # NIC was not CARP enabled ?
127
			return_msg="${return_msg} ${nic} is not CARP enabled"
128
			if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
129
		else
130
			# Handle the case of several VHIDs on the same NIC, by taking the first value
131
			# This hinges on the fact that a MASTER *MUST* have advskew == 0
132
			carp_advskew=`echo "$carp_vhid_output" | sed 's/.* advskew \([0-9][0-9]*\)/\1/' | head -1`
133
			# advskew 0 -> should be MASTER
134
			# advskew > 0 -> should be BACKUP
135

    
136
			return_msg="${return_msg} ${nic} (vhid $carp_vhid) is ${carp_status}"
137
			case $OK_MODE in
138
				MASTER|BACKUP)
139
					if [ "$carp_status" == "$OK_MODE" ] ; then
140
						return_msg="${return_msg} (OK)"
141
					elif [ "$carp_status" == "$NG_MODE" ] ; then
142
						return_msg="${return_msg} (CRITICAL)"
143
						if [ "$return_code" -lt 2 ] ; then return_code=2 ; fi
144
					else
145
						return_msg="${return_msg} (UNKNOWN)"
146
						if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
147
					fi
148
					;;
149
				ADVSKEW)
150
					if [ "$carp_advskew" -eq 0 ] ; then # advskew > 0, should be BACKUP
151
						if [ "$carp_status" = "MASTER" ] ; then
152
							return_msg="${return_msg} (OK)"
153
						elif [ "$carp_status" = "BACKUP" ] ; then
154
							return_msg="${return_msg} (CRITICAL)"
155
							if [ "$return_code" -lt 2 ] ; then return_code=2 ; fi
156
						else
157
							return_msg="${return_msg} (UNKNOWN)"
158
							if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
159
						fi
160
					else
161
						if [ "$carp_status" = "BACKUP" ] ; then
162
							return_msg="${return_msg} (OK)"
163
						elif [ "$carp_status" = "MASTER" ] ; then
164
							return_msg="${return_msg} (CRITICAL)"
165
							if [ "$return_code" -lt 2 ] ; then return_code=2 ; fi
166
						else
167
							return_msg="${return_msg} (UNKNOWN)"
168
							if [ "$return_code" -lt 3 ] ; then return_code=3 ; fi
169
						fi
170
					fi
171
					;;
172
			esac
173
		fi
174
	done
175
	return_msg="${return_msg};"
176
done
177

    
178
exit_nagios "$return_code" "$return_msg"
(3-3/3)