Project

General

Profile

Feature #7895 » check_carp_freebsd.sh

Added "exit $1" command after reporting error - Stéphane Lapie, 10/02/2017 03:53 AM

 
1
#!/bin/sh
2
cd `dirname $0`
3
DIR=`pwd`
4
SCRIPT=`basename $0`
5

    
6
usage()
7
{
8
	echo "Usage: $0 [-B|--backup|-M|--master] [NIC]"
9
	exit 3
10
}
11

    
12
exit_nagios()
13
{
14
	case $1 in
15
		0)
16
			echo "OK - $2"
17
			;;
18
		1)
19
			echo "WARNING - $2"
20
			;;
21
		2)
22
			echo "CRITICAL - $2"
23
			;;
24
		*)
25
			echo "UNKNOWN - $2"
26
			;;
27
	esac
28
	exit $1
29
}
30

    
31
###############################################################################
32
# Check OS
33

    
34
OS=`uname -s`
35
if ! (echo "$OS" | grep -E "FreeBSD|OpenBSD" >/dev/null) ; then
36
	echo "UNKNOWN - OS '$OS' not supported by this script"
37
	exit 3
38
fi
39

    
40
###############################################################################
41
# Check if CARP is used
42

    
43
CARP_OUTPUT=`ifconfig | grep "carp:"`
44
if [ -z "$CARP_OUTPUT" ] ; then
45
	exit_nagios 3 "No NIC is CARP enabled"
46
fi
47

    
48
###############################################################################
49
# Parse arguments
50

    
51
OK_MODE=ADVSKEW
52
NG_MODE=""
53

    
54
GETOPT_TEMP=`getopt MBh "$@"`
55
eval set -- "$GETOPT_TEMP"
56

    
57
while true ; do
58
	case "$1" in
59
		-M|--master)
60
			OK_MODE=MASTER;
61
			NG_MODE=BACKUP;
62
			shift
63
			;;
64
		-B|--backup)
65
			OK_MODE=BACKUP;
66
			NG_MODE=MASTER;
67
			shift
68
			;;
69
		-h|--help)
70
			usage
71
			;;
72
		--) shift ; break ;;
73
		*) echo "Internal error!"; exit 3 ;;
74
	esac
75
done
76

    
77
###############################################################################
78
# Check NIC list
79

    
80
NIC_LIST=`ifconfig -l`
81
NICS="$@"
82

    
83
# If no NIC was specified only keep the CARP enabled ones
84
if [ -z "$NICS" ] ; then
85
	for nic in $NIC_LIST ; do
86
		if (ifconfig $nic | grep "carp:" >/dev/null) ; then
87
			NICS="$NICS $nic"
88
		fi
89
	done
90
fi
91

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

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

    
151
exit_nagios "$return_code" "$return_msg"
(2-2/3)