-
Notifications
You must be signed in to change notification settings - Fork 49
/
check_bond
executable file
·149 lines (132 loc) · 5.24 KB
/
check_bond
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# Nagios plugin to monitor Nic Bonding state
#
# Based on check_bond.sh written by L.Gill 10/08/06 - V.1.0 as found on
# http://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check-network-bonding/details
# currently I maintain my own version at https://github.com/aswen/nagios-plugins/blob/master/check_bond
# Copyright (c) 2010 L.Gill
# Copyright (c) 2011 Alexander Swen <a@swen.nu>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#
# Example configuration
#
# Typical this check is placed on a client and runs via nrpe
# So add this to nrpe.cfg:
# command[check_bond]=/usr/lib/nagios/plugins/check_bond
# This should warn when one of the slaves is down and go critical when the whole bond is not available.
# This plugin defaults to bond0. If you have multiple bonds you can tell the script so by adding -i <if>.
# It will also warn if the Currently Active Slave is not the expected primary interface,
# the default primary interface is eth0, You can override this with -p <if>
# -p can be a comma separated list like "-p eno49,eno50"
# if you have dont_blame_nrpe=1 set you can choose to
# command[check_bond]=/usr/lib/nagios/plugins/check_bond -i $ARG1$ -p $ARG2$
#
# define service {
# use generic-service
# service_description Bond state
# check_command check_nrpe!check_bond
# or
# check_command check_nrpe!check_bond!bond1
# or
# check_command check_nrpe!check_bond!bond1!eth1
#}
#
# ------------------------------------------
# ######## Script Modifications ##########
# ------------------------------------------
# Who When What
# --- ---- ----
# A.Swen 2011-09-07 Add support for other bond module than bond0 (defaults to bond0 however)
# A.Swen 2013-10-11 Remove some obsolete stuff and make the script shorter
# B.Potts 2017-01-16 Check and display expected primary interface on bond
# J.Guldmyr 2018-09-25 Check for a list of primary interfaces instead of a single one
#
#
# SETTINGS
# Default if is bond0
if=bond0
# Default pri is eth0
pri=eth0
# commands
GREP=/bin/grep
AWK=/usr/bin/gawk
LSMOD=/sbin/lsmod
# FUNCTIONS
get_options () {
[ $# -gt 0 ]||result 5
while getopts "i:p:" opt;do
case ${opt} in
i) export if=`echo ${OPTARG}` ;;
p) export pri=`echo ${OPTARG}` ;;
*) result 5;;
esac
done
}
result () {
case $1 in
0) echo "OK: - Bondingmode: $(grep "Bonding Mode:" /proc/net/bonding/${if}), active link: $2";;
1) echo "UNKNOWN: plugin error";rc=3;;
2) echo "CRITICAL: bonding module not loaded";rc=2;;
3) echo "WARNING: state of ${if} device $2 is $3";rc=1;;
4) echo "UNKNOWN: no bondinterface with name ${if} found";rc=3;;
5) echo "UNKNOWN: Usage: ${me} [-i <bond interface name>] [-p <expected primary interface name>]";rc=3;;
6) echo "CRITICAL: bondinterface ${if} has no active slaves";rc=2;;
7) echo "WARNING: Bondingmode: $(grep "Bonding Mode:" /proc/net/bonding/${if}), (unexpected) active link: $2";rc=1;;
8) echo "WARNING: bondinterface with name ${if} has less than 2 interfaces - so zero redundancy";rc=1;;
esac
}
# SCRIPT
# 1st set default return code:
rc=0
# test if this script was called correctly
[ $# -eq 1 -o $# -eq 3 -o $# -gt 4 ] && result 5
[ $rc -gt 0 ] && exit $rc
[ $# -eq 2 -o $# -eq 4 ] && get_options $@
[ $rc -gt 0 ] && exit $rc
# 1st we check if bonding module is loaded
[ "$(${LSMOD}|grep bonding)" = "" ] && result 2
[ $rc -gt 0 ] && exit $rc
# test if there is any bond interface with this name
[ -f "/proc/net/bonding/${if}" ] || result 4
[ $rc -gt 0 ] && exit $rc
case $(grep "Bonding Mode:" /proc/net/bonding/${if}) in
*"IEEE 802.3ad Dynamic link aggregation"*) bondingmode=lacp;;
*) bondingmode=masterslave;;
esac
# Inspect the state of the entire bond interface
if [ "$bondingmode" == "lacp" ]
then
ifstate=$(${AWK} '/MII Status:/ {print $3}' /proc/net/bonding/bond0 | head -n 1)
ifslavecount=$(${AWK} '/Slave Interface:/ {print $3}' /proc/net/bonding/bond0 | wc -l)
[ "${ifstate}" != "up" ]&& result 6
[[ "${pri}" =~ "${ifstate}" ]] || result 7 "${ifstate}"
[ ${ifslavecount} -lt 2 ]&& result 8
else
ifstate=$(${AWK} '/Currently Active Slave/ {print $4}' /proc/net/bonding/${if})
[ "${ifstate}" = "None" ]&& result 6
[[ "${pri}" =~ "${ifstate}" ]] || result 7 "${ifstate}"
[ $rc -gt 0 ] && exit $rc
fi
# test state of each if in bond
ethdevs=$(${AWK} '/Slave Interface/ {print $3}' /proc/net/bonding/${if})
for ethdev in ${ethdevs};do
state=$(${GREP} -A1 "Slave Interface: ${ethdev}" /proc/net/bonding/${if}|${AWK} '/MII Status:/ {print $3}')
if [ "${state}" != "up" ];then
result 3 ${ethdev} ${state}
fi
done
[ $rc -eq 0 ] && result 0 "${ifstate}"
exit $rc
#END