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>
Fri, 30 Jul 2010 06:50:18 +0000 (16:50 +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 deb796650a85ca352d88095fb9d0b7ba9dd1212b..3bfef4f75c45224029a66eedb9b1f9df9d28dbc3 100755 (executable)
@@ -41,12 +41,17 @@ case "$1" in
        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
+       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
+  }
+}'
        ;;
 
     *)