]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Added a configurable logging capability to the network script.
authorJohn Wolfe <jwolfe@vmware.com>
Fri, 13 Aug 2021 18:35:58 +0000 (11:35 -0700)
committerJohn Wolfe <jwolfe@vmware.com>
Fri, 13 Aug 2021 18:35:58 +0000 (11:35 -0700)
The network script has been updated to:
  - use the vmware-toolbox-cmd to query any network logging configuration.
  - use 'vmtoolsd --cmd "log ..."' to log a message to the vmx logfile
    when the logginging handler is configured to "vmx" or when the logfile
    is full or is not writeable.
Added an example configuration in the tools.conf example file.

open-vm-tools/scripts/linux/network
open-vm-tools/tools.conf

index 0c2a93e5a32fc61833967d528f3c2b6669ea36f4..db37304df8a8bedb547d80b8bb36e77f296948b2 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/sh -x
 ##########################################################
-# Copyright (C) 2001-2018 VMware, Inc. All rights reserved.
+# Copyright (C) 2001-2018, 2021 VMware, Inc. All rights reserved.
 #
 # This program is free software; you can redistribute it and/or modify it
 # under the terms of the GNU Lesser General Public License as published
 # of suspend and resume events, respectively.
 #
 
-logbase=/var/log/vmware-network
+SOURCE=$0
+logdir=/var/log
+logbase=$logdir/vmware-network
 logfile=$logbase.log
 
+# Defines logging mode enabled (1) or disabled (0)
+logmode=1
+
+# Defines whether to rotate logs (1) or not (0)
+logrotate=1
+
+#
+# Get log file path
+#
+get_logfile() {
+   file=`vmware-toolbox-cmd config get logging network.data | \
+        sed -e 's/.*= *//' -e 's/ *$//'`
+   if [ -n "${file##*"UNSET"*}" ]; then
+      logfile=$file
+      logdir=`dirname $logfile`
+      logbase=`echo $logfile | sed 's/\..*$//'`
+   fi
+}
+
+#
+# Get Network logging config
+#
+get_logconfig() {
+   handler=`vmware-toolbox-cmd config get logging network.handler | \
+           sed -e 's/.*= *//' -e 's/ *$//'`
+   case $handler in
+      "file")
+         get_logfile
+         ;;
+      "file+")
+         get_logfile
+         logrotate=0
+         ;;
+      "vmx"|"std")
+         logrotate=0
+         ;;
+      "syslog")
+         logfile=/var/log/syslog
+         logdir=`dirname $logfile`
+         logrotate=0
+         ;;
+      *)
+         ;;
+   esac
+}
+
 #
 # Rotate any logs
 #
 rotate_logfile() {
-    max=9
-    max=`expr $max - 1`
-    for s in `seq $max -1 1`; do
-        d=`expr $s + 1`
-        mv -f $logbase.$s.log $logbase.$d.log
-    done
-    mv -f $logbase.log $logbase.1.log
+   if [ $logrotate -eq 1 ]; then
+      max=`vmware-toolbox-cmd config get logging network.maxOldLogFiles | \
+          sed -e 's/.*= *//' -e 's/ *$//'`
+      if [ -z "${max##*"UNSET"*}" -o `expr "$max" : '[0-9]\+$'` -eq 0 ]; then
+         max=9
+      fi
+      max=`expr $max - 1`
+      for s in `seq $max -1 1`; do
+         d=`expr $s + 1`
+         mv -f $logbase.$s.log $logbase.$d.log
+      done
+      mv -f $logbase.log $logbase.1.log
+   fi
 }
 
-rotate_logfile
-
-# redirect stdio
-exec > $logfile 2>&1
-chmod 0600 $logfile
+#
+# Logging api
+#
+log() {
+   if [ $logmode -eq 1 ]; then
+      if [ "$handler" = "vmx" ]; then
+         `vmtoolsd --cmd "log $*"`
+      elif [ "$handler" = "std" ]; then
+         echo `date` ": $*"
+      elif [ -w $logdir ]; then
+         space=`df -k $logdir | awk 'NR == 2 { print $4 }'`
+         if [ $space -gt 1024 ]; then
+            echo `date` ": $*" >> $logfile
+         else
+            `vmtoolsd --cmd "log WARNING: [$SOURCE] Logging disabled. No space left in $logdir"`
+            logmode=0
+         fi
+      else
+         `vmtoolsd --cmd "log WARNING: [$SOURCE] Logging disabled. $logdir is not writable"`
+         logmode=0
+      fi
+   fi
+}
 
+get_logconfig
+rotate_logfile
 
-echo `date` ": Executing '$0 $*'"
-echo
+log "Executing '$0 $*'"
 
 . `dirname "$0"`/../../statechange.subr
 
@@ -96,7 +169,7 @@ find_networking_script() {
       fi
    done
 
-   echo "$script"
+   log "$script"
 }
 
 
@@ -394,9 +467,9 @@ rescue_NIC()
          fi
 
          if echo $intf_out | grep -q 'UP'; then
-            echo `date` "[rescue_nic] $nic is already active."
+            log "[rescue_nic] $nic is already active."
          else
-            echo `date` "[rescue_nic] activating $nic ..."
+            log "[rescue_nic] activating $nic ..."
 
             # Our best effort to activate interfaces, use ifup if available
             # otherwise use the ip command as fallback.
@@ -612,7 +685,7 @@ main() {
          fi
          ;;
       *)
-         echo "No argument supplied."
+         log "No argument supplied."
          ;;
    esac
 
@@ -620,4 +693,4 @@ main() {
 }
 
 main "$@"
-echo `date` ": Finished '$0 $*'"
+log "Finished '$0 $*'"
index 4d55ebd37ecdc6c6a12464788f70cfe076c2637d..1682c3c44b5520b69f948ed2dbdd022b1e563c24 100644 (file)
 # Default 4096, 0=> disable log caching
 #maxCacheEntries=4096
 
+
+# Set the following configurations for modifying network script logging file.
+# Only for Linux, Mac OS X, Solaris, and FreeBSD
+#network.handler = file
+#network.data = /tmp/network.log
+#network.maxOldLogFiles = 9
+
+# Redirect network script logs to vmx
+#network.handler = vmx
+
 [powerops]
 # Custom scripts for power operations
 # This can be an absolute path, or a path relative to the tools