From 85316c04154466b18f266de90362eb08cbec64ee Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Fri, 18 Dec 2015 15:43:33 +1100 Subject: [PATCH] ctdb-scripts: Move interface monitoring code to functions file Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/config/events.d/10.interface | 91 ------------------------------ ctdb/config/functions | 94 +++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 91 deletions(-) diff --git a/ctdb/config/events.d/10.interface b/ctdb/config/events.d/10.interface index 428c23b3cab..03f007420a2 100755 --- a/ctdb/config/events.d/10.interface +++ b/ctdb/config/events.d/10.interface @@ -52,97 +52,6 @@ get_all_interfaces () all_interfaces=$(echo $all_interfaces $ctdb_ifaces | tr ' ' '\n' | sort -u) } -# If the interface is a virtual one (e.g. VLAN) then get the -# underlying interface -interface_get_real () -{ - # Output of "ip link show " - _iface_info="$1" - - # Extract the full interface description to see if it is a VLAN - _t=$(echo "$_iface_info" | - awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; \ - print iface }') - case "$_t" in - *@*) - # VLAN: use the underlying interface, after the '@' - echo "${_t##*@}" - ;; - *) - # Not a regular VLAN. For backward compatibility, assume - # there is some other sort of VLAN that doesn't have the - # '@' in the output and only use what is before a '.'. If - # there is no '.' then this will be the whole interface - # name. - echo "${_t%%.*}" - esac -} - -# Check whether an interface is operational -interface_monitor () -{ - _iface="$1" - - _iface_info=$(ip link show "$_iface" 2>&1) || { - echo "ERROR: Monitored interface ${_iface} does not exist" - return 1 - } - - - # If the interface is a virtual one (e.g. VLAN) then get the - # underlying interface. - _realiface=$(interface_get_real "$_iface_info") - - if _bi=$(get_proc "net/bonding/${_realiface}" 2>/dev/null) ; then - # This is a bond: various monitoring strategies - echo "$_bi" | grep -q 'Currently Active Slave: None' && { - echo "ERROR: No active slaves for bond device ${_realiface}" - return 1 - } - echo "$_bi" | grep -q '^MII Status: up' || { - echo "ERROR: public network interface ${_realiface} is down" - return 1 - } - echo "$_bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && { - # This works around a bug in the driver where the - # overall bond status can be up but none of the actual - # physical interfaces have a link. - echo "$_bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || { - echo "ERROR: No active slaves for 802.ad bond device ${_realiface}" - return 1 - } - } - - return 0 - else - # Not a bond - case "$_iface" in - lo*) - # loopback is always working - return 0 - ;; - ib*) - # we don't know how to test ib links - return 0 - ;; - *) - ethtool "$_iface" | grep -q 'Link detected: yes' || { - # On some systems, this is not successful when a - # cable is plugged but the interface has not been - # brought up previously. Bring the interface up - # and try again... - ip link set "$_iface" up - ethtool "$_iface" | grep -q 'Link detected: yes' || { - echo "ERROR: No link on the public network interface ${_iface}" - return 1 - } - } - return 0 - ;; - esac - fi -} - monitor_interfaces() { get_all_interfaces diff --git a/ctdb/config/functions b/ctdb/config/functions index 68e53abf74b..b714c63223c 100755 --- a/ctdb/config/functions +++ b/ctdb/config/functions @@ -638,6 +638,100 @@ flush_route_cache () set_proc_maybe sys/net/ipv6/route/flush 1 } +######################################################## +# Interface monitoring + +# If the interface is a virtual one (e.g. VLAN) then get the +# underlying interface +interface_get_real () +{ + # Output of "ip link show " + _iface_info="$1" + + # Extract the full interface description to see if it is a VLAN + _t=$(echo "$_iface_info" | + awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; \ + print iface }') + case "$_t" in + *@*) + # VLAN: use the underlying interface, after the '@' + echo "${_t##*@}" + ;; + *) + # Not a regular VLAN. For backward compatibility, assume + # there is some other sort of VLAN that doesn't have the + # '@' in the output and only use what is before a '.'. If + # there is no '.' then this will be the whole interface + # name. + echo "${_t%%.*}" + esac +} + +# Check whether an interface is operational +interface_monitor () +{ + _iface="$1" + + _iface_info=$(ip link show "$_iface" 2>&1) || { + echo "ERROR: Monitored interface ${_iface} does not exist" + return 1 + } + + + # If the interface is a virtual one (e.g. VLAN) then get the + # underlying interface. + _realiface=$(interface_get_real "$_iface_info") + + if _bi=$(get_proc "net/bonding/${_realiface}" 2>/dev/null) ; then + # This is a bond: various monitoring strategies + echo "$_bi" | grep -q 'Currently Active Slave: None' && { + echo "ERROR: No active slaves for bond device ${_realiface}" + return 1 + } + echo "$_bi" | grep -q '^MII Status: up' || { + echo "ERROR: public network interface ${_realiface} is down" + return 1 + } + echo "$_bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && { + # This works around a bug in the driver where the + # overall bond status can be up but none of the actual + # physical interfaces have a link. + echo "$_bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || { + echo "ERROR: No active slaves for 802.ad bond device ${_realiface}" + return 1 + } + } + + return 0 + else + # Not a bond + case "$_iface" in + lo*) + # loopback is always working + return 0 + ;; + ib*) + # we don't know how to test ib links + return 0 + ;; + *) + ethtool "$_iface" | grep -q 'Link detected: yes' || { + # On some systems, this is not successful when a + # cable is plugged but the interface has not been + # brought up previously. Bring the interface up + # and try again... + ip link set "$_iface" up + ethtool "$_iface" | grep -q 'Link detected: yes' || { + echo "ERROR: No link on the public network interface ${_iface}" + return 1 + } + } + return 0 + ;; + esac + fi +} + ######################################################## # Simple counters _ctdb_counter_common () {