-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
genBugInfo
executable file
·360 lines (295 loc) · 9.5 KB
/
genBugInfo
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
SCRIPT=${0##*/}
SCRIPT_VERSION="1.1"
# change seperator from whitespace to not
SAVEIFS=$IFS
IFS_NEWLINES=$(echo -en "\n\b")
IFS=${IFS_NEWLINES}
NOW=`date +%y%m%d-%H%M%S`
OUTFILE=${SCRIPT}-${NOW}.lst
# =============================================================================
# Usage info
#
show_help() {
# takes $1 as as desired exit code
ERROR_LEVEL=$1
cat >&2 << EOF
Usage: ${SCRIPT} [-dhnv]
$SCRIPT v${SCRIPT_VERSION} - Generate command-output-list to attach to bug report
where:
-d Enable script-(d)ebug output
-h Display this (h)elp and exit
-n no-do: list-to-screen but don't run the commands
-v Enable (v)erbose mode. Can be used multiple times for increased
verbosity.
Typical Use Cases:
${SCRIPT} # run commands and write report file
EOF
exit ${ERROR_LEVEL}
}
# =============================================================================
# Simple message printing functions
#
infoMessage () {
# takes $1 as message text
MESSAGE=$1
echo "${SCRIPT}:INFO- ${MESSAGE}" >&2
}
warningMessage () {
# takes $1 as message text
MESSAGE=$1
echo "${SCRIPT}:WARNING- ${MESSAGE}" >&2
}
errorMessage () {
# takes $1 as message text
MESSAGE=$1
echo "${SCRIPT}:ERROR- ${MESSAGE}" >&2
}
fatalMessage () {
# takes $1 as message text and $2 as exit code
MESSAGE=$1
ERROR_LEVEL=$2
errorMessage "${MESSAGE}"
exit ${ERROR_LEVEL}
}
debugMessage () {
# takes $1 as message text and (optional) $2 as gating debug level
# ONLY displays message if DEBUG is enabled
## TODO add gating level logic
MESSAGE=$1
GATING_LEVEL=$2
if [ -n "${DEBUG}" ]; then
echo "${SCRIPT}(DBG): ${MESSAGE}" >&2
fi
}
progressMessage () {
# takes $1 as message text
MESSAGE=$1
echo "${SCRIPT}: ${MESSAGE}"
}
verboseMessage () {
# takes $1 as message text and $2 as gating verbose level
# if gating <= current then display message
## TODO add gating level logic
MESSAGE=$1
GATING_LEVEL=$2
#echo "${SCRIPT}:Verbose(${GATING_LEVEL})- ${MESSAGE}" >&2
if [ ${VERBOSE_LEVEL} -gt 0 ]; then
echo "${SCRIPT}:Verbose- ${MESSAGE}" >&2
fi
}
# =============================================================================
# Initialize our variables
#
export DEBUG=""
export DEBUG_LEVEL=0
export VERBOSE_LEVEL=0
export ACTION_REQUESTED="no"
export WITHOUT_DOING=""
# =============================================================================
# Process the command line arguments
#
OPTIND=1 # Reset is necessary if getopts was used previously in the script. It is a good idea to make this local in a function.
while getopts "hdnv" opt; do
case "$opt" in
d)
export DEBUG_LEVEL=$((DEBUG_LEVEL+1))
export DEBUG=yes
if [ ${DEBUG_LEVEL} -eq 1 ]; then
# when first setting debug set this too
export VERBOSE_LEVEL=$((VERBOSE_LEVEL+1))
fi
;;
h)
show_help 0
;;
n)
export ACTION_REQUESTED="yes"
export WITHOUT_DOING="yes"
if [ $VERBOSE_LEVEL -eq 0 ]; then
export VERBOSE_LEVEL=$((VERBOSE_LEVEL+1))
fi
progressMessage "* Mode NO-DO (will not actually run) *"
;;
v)
export VERBOSE_LEVEL=$((VERBOSE_LEVEL+1))
progressMessage "verbose set to ${VERBOSE_LEVEL}!"
;;
'?')
echo "$SCRIPT: Question Mark!" >&2
show_help 0
;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.
# =============================================================================
# final checks and setup before execution
#
if [ -n "$DEBUG" ]; then
debugMessage "ending with p0=$0 p1=$1 p2=$2 p3=$3 p4=$4 ..."
echo ""
fi
EXTRA_ARG=$1
if [ -n "${EXTRA_ARG}" ]; then
errorMessage "Unexpected argument [${EXTRA_ARG}], aborting"
show_help 2
fi
#if [ "${ACTION_REQUESTED}" = "no" ]; then
# echo ""
# progressMessage "No Actions Specified..."
# #show_help 0
#fi
##if [ "${WITHOUT_DOING}" = "yes" ]; then
# verboseMessage "Test Complete"
# exit 0
#fi
# =============================================================================
showCommand () {
COMMAND=$@
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
#echo ""
echo "# ${COMMAND}"
else
if [ -z "${WITHOUT_DOING}" ]; then
## do this quietly...
#echo "" >>${OUTFILE} 2>&1
echo "# ${COMMAND}" >>${OUTFILE} 2>&1
echo " " >>${OUTFILE} 2>&1
else
echo "${SCRIPT}-(NODO): # ${COMMAND}"
fi
fi
}
endCommand () {
COMMAND=$@
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
echo ""
echo ""
else
if [ -z "${WITHOUT_DOING}" ]; then
## do this quietly...
echo "" >>${OUTFILE} 2>&1
echo " ----" >>${OUTFILE} 2>&1
echo "" >>${OUTFILE} 2>&1
fi
fi
}
# mark our outfile
RUN_DATE=`date +%y/%m/%d-%H:%M:%S`
if [ ${VERBOSE_LEVEL} -eq 0 -a -z "${WITHOUT_DOING}" ]; then
echo "# SCRIPT ${SCRIPT} v${SCRIPT_VERSION} run ${RUN_DATE}" >>${OUTFILE} 2>&1
echo "# ----------------------------------------------------------------------" >>${OUTFILE} 2>&1
echo "" >>${OUTFILE} 2>&1
fi
# ----------------------------------------------------------------------
showCommand "/bin/cat /etc/apt/sources.list | /bin/egrep -v '#'"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/cat /etc/apt/sources.list | /bin/egrep -v '#'
else
/bin/cat /etc/apt/sources.list | /bin/egrep -v '#' >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/bin/cat /etc/apt/sources.list | /bin/egrep -v '#' | /usr/bin/awk '{ print \$3 }' | /bin/grep . | /usr/bin/sort -u | head -1"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/cat /etc/apt/sources.list | /bin/egrep -v '#' | /usr/bin/awk '{ print $3 }' | /bin/grep . | /usr/bin/sort -u | head -1
else
/bin/cat /etc/apt/sources.list | /bin/egrep -v '#' | /usr/bin/awk '{ print $3 }' | /bin/grep . | /usr/bin/sort -u | head -1 >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/bin/uname -r"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/uname -r
else
/bin/uname -r >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/bin/hostname -f"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/hostname -f
else
/bin/hostname -f >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/usr/bin/uptime"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/usr/bin/uptime
else
/usr/bin/uptime >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/sbin/ifconfig"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/sbin/ifconfig
else
/sbin/ifconfig >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/sbin/ifconfig | /bin/egrep 'Link|flags|inet|ether' | /bin/egrep -v -i 'lo:|loopback|inet6|\:\:1|127\.0\.0\.1'"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/sbin/ifconfig | /bin/egrep 'Link|flags|inet|ether' | /bin/egrep -v -i 'lo:|loopback|inet6|\:\:1|127\.0\.0\.1'
else
/sbin/ifconfig | /bin/egrep 'Link|flags|inet|ether' | /bin/egrep -v -i 'lo:|loopback|inet6|\:\:1|127\.0\.0\.1' >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/sbin/route"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/sbin/route
else
/sbin/route >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/bin/ls -l /var/log/dpkg.log /var/log/dpkg.log.1 2>/dev/null"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/ls -l /var/log/dpkg.log /var/log/dpkg.log.1 2>/dev/null
else
/bin/ls -l /var/log/dpkg.log /var/log/dpkg.log.1 2>/dev/null >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/bin/grep 'status installed' /var/log/dpkg.log /var/log/dpkg.log.1 2>/dev/null | sort | tail -1"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/grep 'status installed' /var/log/dpkg.log /var/log/dpkg.log.1 2>/dev/null | sort | tail -1
else
/bin/grep 'status installed' /var/log/dpkg.log /var/log/dpkg.log.1 2>/dev/null | sort | tail -1 >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/bin/df -m"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/df -m
else
/bin/df -m >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "/bin/df -m | /usr/bin/tail -n +2 | /bin/egrep -v 'tmpfs|boot'"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
/bin/df -m | /usr/bin/tail -n +2 | /bin/egrep -v 'tmpfs|boot'
else
/bin/df -m | /usr/bin/tail -n +2 | /bin/egrep -v 'tmpfs|boot' >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
showCommand "ls -l /opt/vc/bin/vcgencmd /usr/bin/vcgencmd"
if [ ${VERBOSE_LEVEL} -gt 0 -a -z "${WITHOUT_DOING}" ]; then
ls -l /opt/vc/bin/vcgencmd /usr/bin/vcgencmd
else
ls -l /opt/vc/bin/vcgencmd /usr/bin/vcgencmd >>${OUTFILE} 2>&1
fi
endCommand
# ----------------------------------------------------------------------
if [ -f ${OUTFILE} ]; then
progressMessage "Wrote ${OUTFILE}"
fi
# restore separator
IFS=$SAVEIFS
progressMessage "Done"
exit 0