From: John Wolfe Date: Fri, 13 Aug 2021 18:35:58 +0000 (-0700) Subject: Added a configurable logging capability to the network script. X-Git-Tag: stable-11.3.5~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4265556a1e60a0298c03cf06222d5106e77325f;p=thirdparty%2Fopen-vm-tools.git Added a configurable logging capability to the network script. 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. --- diff --git a/open-vm-tools/scripts/linux/network b/open-vm-tools/scripts/linux/network index 0c2a93e5a..db37304df 100644 --- a/open-vm-tools/scripts/linux/network +++ b/open-vm-tools/scripts/linux/network @@ -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 @@ -26,31 +26,104 @@ # 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 $*'" diff --git a/open-vm-tools/tools.conf b/open-vm-tools/tools.conf index 4d55ebd37..1682c3c44 100644 --- a/open-vm-tools/tools.conf +++ b/open-vm-tools/tools.conf @@ -160,6 +160,16 @@ # 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