Debian Codegraph @

assembly/src/release/bin/activemq-diag

raw symbols SCIP
1 #!/bin/sh
2 # ------------------------------------------------------------------------
3 # Licensed to the Apache Software Foundation (ASF) under one or more
4 # contributor license agreements. See the NOTICE file distributed with
5 # this work for additional information regarding copyright ownership.
6 # The ASF licenses this file to You under the Apache License, Version 2.0
7 # (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ------------------------------------------------------------------------
18 #
19 ## diag.sh - a script to run diagnostic commands and save output to a log file
20 #
21 # Author: Cal Lott <[email protected]>
22 #
23 PATH=$PATH:$HOME/bin:/bin/:/usr/bin:/sbin:/usr/sbin
24
25 # Set a script name string for later use.
26 SCRIPT_NAME=`basename $0`
27 ORIGINAL_COMMAND_LINE="$0 $*"
28 LOCALHOST=`uname -n | awk -F. '{print $1}'`
29
30 # Declare some functions.
31
32 # Prints argument to stdout, prepended with "[YYYYMMDD-HH:MM:SS]".
33 dateprint() {
34 echo "[`date +%Y%m%d-%H:%M:%S`] $*"
35 }
36
37 # Runs argument list as a single shell command.
38 BAR_SEPARATOR='------------------------------------------------'
39 run() {
40 echo
41 echo $BAR_SEPARATOR
42 dateprint "START: $*"
43 executable=`echo $* | awk '{print $1}'`
44 dateprint "path to executable: `which $executable 2>/dev/null`"
45 $*
46 last_exit_code=$?
47 cumulative_exit_code=`expr $cumulative_exit_code + $last_exit_code`
48 dateprint "exit $last_exit_code"
49 dateprint "END: $*"
50 echo $BAR_SEPARATOR
51 echo
52 }
53
54 # Print usage information.
55 usage() {
56 cat << EOF
57
58 $SCRIPT_NAME - run a set of standard diagnostic commands
59
60 Usage: $SCRIPT_NAME [-l logfile] [-jz] [-hv] <pid1> <pid2> ... <pidN>
61
62 This script runs several diagnostic commands on the local system and saves the
63 output to a gzipped log file. You may also specify an optional list of pids,
64 which will cause additional diagnostic commands (lsoft, jstat, etc.) to be run
65 against those specific processes only.
66
67 The default path for the datestamped log file is as follows:
68
69 \$HOME/log/$SCRIPT_NAME.\$LOCALHOST.YYYYMMDD-HHMMSS.log
70
71 You can override the default file name with the -l option. (See below.)
72
73
74 Options:
75 -j Also process any pids listed in output of "jps" command.
76 -l <logfile> Use specified log file instead of default.
77 -z Do not gzip log file.
78
79 Other Options:
80 -h Print this message and exit.
81
82 EOF
83 }
84
85 # Print help information.
86 help() {
87 usage
88 }
89
90 # Command line options processing.
91 while getopts "hjl:vz" OPTION
92 do
93 case $OPTION in
94 h)
95 help
96 exit 0
97 ;;
98 j)
99 USE_JPS='TRUE'
100 ;;
101 l)
102 LOGFILE=$OPTARG
103 ;;
104 z)
105 COMPRESSION_OPTION='DISABLED'
106 ;;
107 ?)
108 usage
109 exit 1
110 ;;
111 esac
112 done
113 shift `expr $OPTIND - 1` # remove processed arguments from $*
114 PIDLIST=$* # save list of remaining args as pidlist
115
116 # If requested, use jps to get an additional list of java pids.
117 if [ "$USE_JPS" = 'TRUE' ]
118 then
119 # Save the current list of pids.
120 arg_pids=$PIDLIST
121 # Use jps to grab additional java pids, filtering the jps pid itself.
122 jps_pids=`jps 2>/dev/null | awk '$2 !~ /^Jps/ {print $1}'`
123 # Now obtain an unique sorted list of pids from both groups.
124 PIDLIST=`echo $arg_pids $jps_pids | sort | uniq`
125 fi
126
127 # Use the default logfile if the user did not specify one.
128 if [ "$LOGFILE" = '' ]
129 then
130 LOGFILE="$HOME/log/$SCRIPT_NAME.$LOCALHOST.`date +%Y%m%d-%H%M%S`.log"
131 fi
132 # Create log directory if it does not already exist
133 mkdir `dirname $LOGFILE` >/dev/null 2>&1
134
135 # Print start message to stdout.
136 dateprint "BEGIN - running diagnostics for host: `uname -n`"
137
138 # Complain and die if we cannot create the logfile.
139 touch $LOGFILE >/dev/null 2>&1
140 if [ $? -eq 0 ]
141 then
142 dateprint "logfile created, running diagnostic commands . . ."
143 else
144 dateprint "ERROR: cannot create $LOGFILE, exiting!"
145 exit 1
146 fi
147
148 # Save stdout and stderr descriptors and redirect output to logfile.
149 exec 6>&1 # Link file descriptor 6 with stdout (saves stdout)
150 exec 7>&2 # Link file descriptor 7 with stderr (saves stderr)
151 exec >$LOGFILE 2>&1 # Redirect stdout and stdin to logfile
152
153 ##
154 ## Begin diagnostic output
155 ##
156 dateprint "BEGIN DIAGNOSTIC OUTPUT"
157 dateprint "running on host: `uname -n`"
158 dateprint "unprocessed command line: \"$ORIGINAL_COMMAND_LINE\""
159 dateprint "after options processing: \"$0 $PIDLIST > $LOGFILE\""
160
161 # Each of the following shell commands will be run once.
162 for cmd in \
163 "crontab -l" \
164 "id" \
165 "ifconfig -a" \
166 "df -kl" \
167 "env" \
168 "iostat -x" \
169 "jps -v" \
170 "netstat -anp" \
171 "netstat -r" \
172 "ps -ef" \
173 "sar -A" \
174 "ulimit -a" \
175 "uname -a" \
176 "uptime" \
177 "w" \
178 ; do
179 run $cmd
180 done
181
182 # As above, but these commands are customized for a specific flavor of Unix.
183 OS=`uname -s`
184 case $OS in
185 Linux)
186 for cmd in \
187 "swapon -s" \
188 "top -b -n1" \
189 ; do
190 run $cmd
191 done
192 ;;
193 SunOS)
194 for cmd in \
195 "swap -s" \
196 "top -b -d1" \
197 ; do
198 run $cmd
199 done
200 ;;
201 *)
202 dateprint "WARNING: OS \"$OS\" unknown - running no OS-specific commands"
203 ;;
204 esac
205
206 # Each of these commands are run once per each pid argument
207 for pid in $PIDLIST
208 do
209 for cmd in \
210 "lsof -p $pid" \
211 "jstat -gcutil $pid" \
212 "jstack -l $pid" \
213 ; do
214 run $cmd
215 done
216 done
217
218 dateprint "END DIAGNOSTIC OUTPUT"
219 dateprint "cumulative exit code: $cumulative_exit_code"
220
221 exec 1>&6 6>&- # Restore stdout and close file descriptor 6.
222 exec 2>&7 7>&- # Restore stderr and close file descriptor 7.
223 ##
224 ## End diagnostic output
225 ##
226
227 # Compress the logfile if requested and end script execution
228 if [ "$COMPRESSION_OPTION" = 'DISABLED' ]
229 then
230 dateprint "FINISH - output stored at: $LOGFILE"
231 else
232 nohup gzip $LOGFILE >/dev/null 2>&1 &
233 dateprint "FINISH - output stored at: $LOGFILE.gz"
234 fi
235
236 exit $cumulative_exit_code