From: Martin Schwenke Date: Thu, 27 Aug 2015 03:22:49 +0000 (+1000) Subject: ctdb-scripts: Use ss instead of netstat for finding TCP connections X-Git-Tag: talloc-2.1.7~231 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=04fe9e20749985c71fef1bce7f6e4c439fe11c81;p=thirdparty%2Fsamba.git ctdb-scripts: Use ss instead of netstat for finding TCP connections ss with a filter is much faster than post-processing output from netstat. CTDB already has a hard dependency on iproute2 for IP address handling, so depending on ss is no big deal. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- diff --git a/ctdb/config/functions b/ctdb/config/functions index 782978dad84..8a8ee8c89f6 100755 --- a/ctdb/config/functions +++ b/ctdb/config/functions @@ -531,11 +531,7 @@ get_tcp_connections_for_ip () { _ip="$1" - netstat -tn | awk -v ip=$_ip \ - 'index($1, "tcp") == 1 && \ - (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \ - && $6 == "ESTABLISHED" \ - {print $4" "$5}' + ss -tn state established "src [$_ip]" | awk 'NR > 1 {print $3, $4}' } ######################################################## @@ -1183,17 +1179,24 @@ update_tickles () # What public IPs do I hold? _ips=$(ctdb -X ip | awk -F'|' -v pnn=$pnn '$3 == pnn {print $2}') - # IPs as a regexp choice - _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))" + # IPs and port as ss filters + _ip_filter="" + for _ip in $_ips ; do + _ip_filter="${_ip_filter}${_ip_filter:+ || }src [${_ip}]" + done + _port_filter="sport == :${_port}" # Record connections to our public IPs in a temporary file. # This temporary file is in CTDB's private state directory and # $$ is used to avoid a very rare race involving CTDB's script # debugging. No security issue, nothing to see here... _my_connections="${tickledir}/${_port}.connections.$$" - netstat -tn | - awk -v destpat="^${_ipschoice}:${_port}\$" \ - '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' | + # Parentheses are needed around the filters for precedence but + # the parentheses can't be empty! + ss -tn state established \ + "${_ip_filter:+( ${_ip_filter} )}" \ + "${_port_filter:+( ${_port_filter} )}" | + awk 'NR > 1 {print $4, $3}' | sort >"$_my_connections" # Record our current tickles in a temporary file diff --git a/ctdb/tests/eventscripts/stubs/ss b/ctdb/tests/eventscripts/stubs/ss new file mode 100755 index 00000000000..e8d804481df --- /dev/null +++ b/ctdb/tests/eventscripts/stubs/ss @@ -0,0 +1,88 @@ +#!/bin/bash + +prog="ss" + +usage () +{ + cat >&2 <