-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvmware-view-log-collector
executable file
·149 lines (127 loc) · 3.74 KB
/
vmware-view-log-collector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
DEFAULT_TARGET='view-log.tar.gz'
function printError()
{
echo >&2 "${0##*/}: $1"
}
function Usage()
{
cat <<EOF
Usage: ${0##*/} [FILE]
This scripts locates the latest log file generated from the
VMware View Client. The log file will be packaged into a new
file in the current directory.
OPTIONS:
--help: Shows this help message.
-u|--user <username> Selects the username to collect files for.
EOF
}
# Check whether need to copy the PCoIP log and copy logs
# Directory name may have space, and file name have the wildcard character.
# So if use quotes to enclose the file name, ls command won't find it, and must
# put the file name outside of the quotes. So 4 arguments are needed.
function cpPCoIPLog()
{
local cmpLogDir="$1"
local cmpLogGlob="$2"
local destViewLogFile="$3"
local destTargetDir="$4"
# In one connection, there are maybe more than one PCoIP/mks
# logs and all should be collected. Use the most recently
# view log's first line to get the view log's create time,
# and then compare it with the mtime of
# PCoIP/mks log, if PCoIP/mks is newer, just collect it.
local destViewLogTime=$(date -d "$(head -n 1 "$destViewLogFile" | sed -e 's/: vmware-view|.*//')" "+%s")
for logFile in $(ls -t "$cmpLogDir"/$cmpLogGlob 2>/dev/null) ; do
local cmpLogTime=$(stat -c %Y "$logFile")
if [ $cmpLogTime -lt $destViewLogTime ] ; then
return
fi
if ! cp "$logFile" "$destTargetDir" ; then
printError "Unable to copy log file $logFile to $destTargetDir."
exit 1
fi
done
}
target="$DEFAULT_TARGET"
username="$USER"
while [ $# -ne 0 ]; do
arg=$1
shift
case $arg in
--help)
Usage
exit
;;
-u|--user)
username="$1"
shift
;;
--)
target="$@"
shift $#
;;
*)
if [ ${arg:0:1} == '-' ] ; then
printError "Unknown argument: $arg."
exit 1
else
target="$arg"
fi
;;
esac
done
logDirectory="/tmp/vmware-$username"
# Find the directory that logs are stored.
if [ ! -d "/tmp/vmware-$username/" ] ; then
printError "The log directory $logDirectory does not exist."
exit 1
fi
viewLogGlob="vmware-view-[0-9]*.log"
# Ensure at least one log file exits.
if ! ls "/tmp/vmware-$username"/$viewLogGlob &>/dev/null ; then
printError "No log found in $logDirectory."
exit 1
fi
# Find the most 'recent' log and zip it.
targetDirectory="${target%.tar.gz}"
# Create a temporary directory in which to work.
if ! tmpdir=$(mktemp -d) ; then
printError "Failed to create temporary directory."
exit 1
fi
if ! mkdir "$tmpdir/$targetDirectory" ; then
printError "Failed to create $tmpdir/$targetDirectory."
exit 1
fi
fileToCp=$(ls -t "$logDirectory"/$viewLogGlob | head -n 1)
if [ -z "$fileToCp" ] ; then
printError "Unable to locate log file in $logDirectory."
exit 1
fi
if ! cp "$fileToCp" "$tmpdir/$targetDirectory" ; then
printError "Unable to copy log file $fileToCp to $tmpdir/$targetDirectory."
exit 1
fi
viewLogFile=$fileToCp
# Move PCoIP logs to the temp directory
pcoipLogDir="/tmp/teradici-$username"
pcoipLogGlob="pcoip_client_*.txt*"
mksLogGlob="mks-[0-9]*.log"
# Copy the mks logs
cpPCoIPLog "$logDirectory" "$mksLogGlob" "$viewLogFile" "$tmpdir/$targetDirectory"
# Copy the pcoip_client logs
cpPCoIPLog "$pcoipLogDir" "$pcoipLogGlob" "$viewLogFile" "$tmpdir/$targetDirectory"
# Move into the temp directory to prevent tar from prepending the
# temp directory on the target directory.
pushd "$tmpdir" >/dev/null
tar czf "$target" "$targetDirectory"
tarResult="$?"
popd >/dev/null
if [ "$tarResult" -eq 0 ] ; then
mv "$tmpdir/$target" .
else
printError "Unable to make $target."
fi
rm -rf "$tmpdir"
exit 0