ctdb-scripts: Move interface monitoring code to functions file
authorMartin Schwenke <martin@meltin.net>
Fri, 18 Dec 2015 04:43:33 +0000 (15:43 +1100)
committerAmitay Isaacs <amitay@samba.org>
Mon, 25 Jan 2016 06:18:24 +0000 (07:18 +0100)
Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/config/events.d/10.interface
ctdb/config/functions

index 428c23b3cab34fa2fe501e0e7821e05f027530ba..03f007420a2ea07fce097a733bc8f390fca14ea7 100755 (executable)
@@ -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>"
-    _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
index 68e53abf74b97d91ed7f9e50540925b58b1be35b..b714c63223c62b8b81323b2d0b8fdb592cd44aa1 100755 (executable)
@@ -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>"
+    _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 () {