aboutsummaryrefslogtreecommitdiffstats
path: root/src/extras/rh-upload-core
blob: 4bea94781f0a33c8b2650981c0bd8d83c0d72913 (plain) (blame)
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
#!/bin/bash

#################################################################################
#										#
# upload-core									#
# Version - 0.2									#
# Copyright (c) 2007 Red Hat, Inc. All rights reserved.				#
#										#
#										#
# Written by David Mair								#
# Idea stolen from Chris Snook :-)						#
#										#
# Purpose - To help in the automation and encryption of kernel vmcore files.	#
# Specifically, this script will compress, encrypt, md5sum,			#
# and upload the core file automatically when invoked.				#
# Items are optional and specified by command line switch.			#
# ###############################################################################

## Global directives
umask 0077

## Declare some variables
date=`/bin/date -u +%G%m%d%k%M%S | /usr/bin/tr -d ' '`
destination="dropbox.redhat.com/incoming"
NOUPLOAD=NO
SPLIT=0


## Let's explain the usage

function usage {
	echo
	echo
	echo "Upload-core is a shell script for automating the handling of kernel vmcore files"
	echo "for system administrators when working with support technicians."
	echo "The script allows echo the user to compress, checksum, encrypt and upload a core"
	echo "file with one command."
	echo
	echo "Usage: upload-core [-cehnNq] [-s size of hunks in MB] -f filename"
	echo
	echo "-c|--checksum  : perform an md5 checksum on the file"
	echo "-e|--encrypt   : encrypt the core file"
	echo "-f|--file      : file to act on (required)"
	echo "-h|--help      : show this usage help"
	echo "-n|--nocompress: do not compress the file (otherwise the file will be gzipped)"
	echo "-N|--noupload  : Do NOT upload to an ftp drop box"
	echo "-q|--quiet     : Do everything I ask and do it quietly"
	echo "-s|--split     : split file into small hunks" 
	echo
	echo
	exit 0
}

if [ $# == 0 ]; then usage
fi

TEMP=`getopt -o heucnqs:f:N --long help,encrypt,quiet,noupload,checksum,nocompress,split:,file: -n 'upload-core.sh' -- "$@"`

if [ $? != 0 ]; then echo "Options error -- Terminating..." >&2; exit 1; fi

eval set -- "$TEMP"

while true ; do 
	case "$1" in
		-h|--help) usage;;
		-e|--encrypt) ENCRYPT=yes; shift;;
		-N|--noupload) NOUPLOAD=yes; shift;;
		-c|--checksum) CHECKSUM=yes; shift;;
		-q|--quiet) QUIET=yes; shift;;
		-n|--nocompress) NOCOMPRESS=yes; shift;;
		-s|--split) 
			case $2 in
			"") echo "You must specify a hunk size." >&2; exit 1 ;;
			*) SPLIT=$2; shift 2;;
			esac ;;
		-F|--force) FORCE=yes; shift;;
		-f|--file) 
			case "$2" in
				"") echo "You must specify a file name." >&2; exit 1 ;;
				*) FILE=$2; shift 2;;
			esac ;;
		--) shift; break ;;
		*) echo "Wrong options or flag specified"; usage;;
	esac
done


# Okay, let's do some work!

# Ensure the -f||--file flag was passed or die

if test -z $FILE; then echo; echo "The -f or --file flag is required! Terminating."; echo; exit 1; fi

# Validate the file exists or die

if [ ! -f $FILE ]; then echo "Invalid filename or file not found. Terminating."; exit 1; fi

function repeat {

if [ "$QUIET" = "yes" ]; then return
else

# Let's repeat back to the user what we're doing and make sure this is what they really wanted.
echo
if [ "$ENCRYPT" = "yes" ] ; then echo " ## Will encrypt the file.";echo; fi

if [ "$NOUPLOAD" = "yes" ] ; then echo " ## Will NOT upload the file.";echo; fi

if [ "$CHECKSUM" = "yes" ] ; then echo " ## Will checksum the file.";echo; fi

if [ "$SPLIT" != "FALSE" ] ; then echo " ## Will split the file.";echo; fi

if [ "$NOCOMPRESS" = "yes" ] ; then echo -e " ## Will \E[41;30m\033[5mNOT\033[0m compress the file. Are you sure?";echo; else echo "Compressing $FILE"; echo; fi
fi
}


function warn {

if [ "$QUIET" = "yes" ]; then return
else
echo "Please note that depending upon the size of your vmcore file this could take"
echo "quite some time to run.  If the options listed above are correct please"
echo "press enter. Otherwise press <ctrl>-<c> to exit the program and start again."
echo
read IGNORE
echo
fi
}

function ticket {
echo
echo "We'll need to use your trouble ticket number for a couple of things. Please"
echo "enter your trouble ticket number:"
read ticket_number
echo
return
}

function file_ops {
# Need to rename the core file before we compress it
if [ "$QUIET" != "yes" ]; then echo "Renaming core file $ticket_number-$date-vmcore"; fi

new_file=$ticket_number-$date-vmcore

/bin/mv $FILE $new_file
}

# Compress the file
function compress {

if [ "$NOCOMPRESS" = "yes" ] 
	then 
	if [ "$QUIET" != "yes"]; then echo "Skipping compression step.";echo; fi
	
	else 
		if [ "$QUIET" != "yes" ]; then echo "Starting file compression. This will take some time.";echo; fi
		# Begin compression of file
		if [ ! /usr/bin/gzip ]; then
			echo "Cannot find gzip in /usr/bin/. Terminating."; exit 1
		else
		/usr/bin/gzip --fast $new_file
		fi
		
fi

new_file="$new_file.gz"

}

# Encrypt the file
function encrypt {

if [ "$ENCRYPT" = "yes" ]
	then 
		if [ "$QUIET" != "yes" ]; then echo "Beginning file encryption.  This should only take a few minutes.";echo; fi
		# Use the ticket number as the ssl keyfile name
		if [ ! /usr/bin/openssl ]; then
			echo "Cannot find openssl in /usr/bin. Terminating."; exit 1
		fi
		/usr/bin/openssl rand -out $ticket_number-$date.key -base64 48
		if [ "$QUIET" != "yes" ]; then
		echo "You have chosen to encrypt your core file. Your passkey file is"
		echo "$ticket_number-$date.key. Please attach this key to your ticket."
		echo
		fi
		/usr/bin/openssl aes-128-cbc -in $new_file -out $new_file.aes -pass file:$ticket_number-$date.key

new_file="$new_file.aes"				

fi
}

function checksum {

if [ "$CHECKSUM" = "yes" ]
	then
	
		if [ "$QUIET" != "yes" ]; then echo "Beginning $new_file checksum. This should only take a few minutes.";echo; fi
	if [ ! /usr/bin/md5sum ]; then
		echo "Cannot find md5sum in /usr/bin. Terminating."; exit 1
	fi
	md5result=`/usr/bin/md5sum $new_file|awk '{print $1}'`
	echo $md5result > $ticket_number-$date-checksum.out

fi

}

function split {

if [ "$SPLIT" = "0" ]; then return; fi

		hunk_size=$SPLIT
		if (( $hunk_size > 0 )) && (( $hunk_size < 1001 ))
		then
			if [ ! /usr/bin/split ]; then
				echo "Cannot find split in /usr/bin. Terminating."; exit 1
			fi
		# We need to make a directory to keep things sane
		if [ "$QUIET" != "yes" ]; then echo "Creating directory $ticket_number-$date to house file hunks"; fi
		/bin/mkdir $ticket_number-$date 
		/usr/bin/split -b "$hunk_size"m -d $new_file $ticket_number-$date/$new_file
		else
		echo "Invalid hunk size argument. Please enter a number greater than 0 and less than 1001."
		echo "Terminating."; exit 1
		fi


}

function upload {

if [ "$NOUPLOAD" = "yes" ]; then 
	echo "All file operations are complete. The file(s) is ready to upload at your convenience."; return
	else
	echo "All file operation are complete. The file(s) is now ready to be uploaded."
	echo "Please enter the destination host (default is dropbox.redhat.com)"
	read destination_input
		if [ "$destination_input" != "" ]; then	destination=$destination_input; fi
	if [ "$QUIET" != "yes" ]; then
	echo
	echo "Okay, uploading to $destination.  Depending upon the file size and link throughput"
	echo "this could take quite a while.  When the upload completes this script will provide"
	echo "additional information such as the md5sum, ssl key file, etc. and then exit."
	echo "Unless you do not have lftp installed you should be able to monitor upload status."
	echo "If lftp is not available then this script will exit and the core file(s) will need"
	echo "to be uploaded to your target system manually.  The information indicated above"
	echo "will still be provided."
	echo
        fi
	if [ ! /usr/bin/lftp ]; then 
		# No lftp installed
		echo "lftp could not be found in /usr/bin. The file(s) will need to be uploaded manually."
	
	else
	# Make the lftp script first
	echo "lftp $destination <<EOF" > lftp_scripts
	if [ "$SPLIT" = "yes" ]; then
		echo "lcd $ticket_number" >> lftp_scripts
		echo "mirror -R" >> lftp_scripts
	else
		echo "put $new_file" >> lftp_scripts
	fi
	echo "quit 0" >> lftp_scripts
	echo "EOF" >> lftp_scripts
	/usr/bin/lftp -f lftp_scripts
	fi
fi
}

function closure {

if [ "$ENCRYPT" = "yes" ] ; then 
	echo
	echo " ## File was encrypted with $ticket_number-$date.key. Please upload the key"
	echo "to Issue Tracker or send it to your support representative for decryption"
	echo "after upload.";echo; 
fi


if [ "$CHECKSUM" = "yes" ] ; then 
	echo
	echo "## A checksum was performed on your core file (prior to splitting if you chose"
	echo "to do so)."
	echo
	echo "The checksum results are in:"
	echo "$ticket_number-$date-checksum.out." 
	echo
	echo "Please include this when updating your trouble ticket so your support"
	echo "representative can verify the copy uploaded.";echo
fi

if [ "$SPLIT" != 0 ]; then
	echo
	echo "## Your core file was split and the hunks are in $ticket_number."
	echo
fi

echo "This script has completed successfully.  If you performed file encryption and/or file"
echo "splitting you may want to consider removing those files once your support representative"
echo "confirms receipt.  This will reduce the amount of space being utilised on your system."
echo "It is NOT recommended to remove the gzipped copy of the core file."
echo
echo -en "\E[40;31m\033[3mThis would be the only remaining copy of the core file on your system.\033[0m"
echo
echo
echo "It is recommended to retain the core file until your support representative indicates"
echo "that the problem has been identified and/or resolved."

}

# Run through the functions
repeat
warn
ticket
file_ops
compress
encrypt
checksum
split
upload
closure