Optimise 61.nfstickle to write the tickles more efficiently.
authorMartin Schwenke <martin@meltin.net>
Mon, 26 Jul 2010 06:22:59 +0000 (16:22 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Mon, 9 Aug 2010 02:27:00 +0000 (12:27 +1000)
Currently the file for each IP address is reopened to append the
details of each source socket.

This optimisation puts all the logic into awk, including the matching
of output lines from netstat.  The source sockets for each for each
destination IP are written into an array entry and then each array
entry is written to the corresponding file in a single operation.

Signed-off-by: Martin Schwenke <martin@meltin.net>
config/events.d/61.nfstickle

index 99004984c82c982fee56bff268209a8bbd9c6e07..98023583e37fc8b73586ebcb85897853ff13ed14 100755 (executable)
@@ -15,24 +15,6 @@ ctdb_start_stop_service
 
 [ -z "$NFS_TICKLE_SHARED_DIRECTORY" ] && exit 0
 
-store_tickles()
-{
-       # always create these direcotries since NFS might be enabled at runtime
-       # and we dont want to restart ctdbd
-       mkdir -p $CTDB_BASE/state/nfstickle
-       mkdir -p $NFS_TICKLE_SHARED_DIRECTORY/`hostname`
-
-       mydir=$NFS_TICKLE_SHARED_DIRECTORY/`hostname`
-       rm -f $mydir/*
-       # record our connections to shared storage
-       netstat -tn |egrep '^tcp[[:space:]]+[0-9]+[[:space:]]+[0-9]+[[:space:]]+[0-9\.]+:2049.*ESTABLISHED' |
-               awk '{print $4" "$5}' | 
-               while read dest src; do
-                       ip=${dest%:*}
-                       echo $src >> $mydir/$ip
-               done
-}
-
 case "$1" in 
      startup)
        ctdb_service_start
@@ -57,7 +39,20 @@ case "$1" in
        ;;
 
      monitor)
-       store_tickles &
+       mydir=$NFS_TICKLE_SHARED_DIRECTORY/`hostname`
+       rm -f $mydir/*
+       # record our connections to shared storage
+       netstat -tn |
+       awk -v mydir="$mydir" '
+$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ /:2049$/ {
+  destip = gensub(/:2049$/, "", 1, $4);
+  c[destip] = c[destip] (c[destip] ? "\n" : "" ) $5;
+}
+END {
+  for (ip in c) {
+    print c[ip] > mydir "/" ip
+  }
+}'
        ;;
 
     *)