dreport: Add packaging support
Resolves: openbmc/openbmc#1507
Change-Id: I15f76cd8e6652334bcfcc2923773d67fafcfed97
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/tools/dreport b/tools/dreport
new file mode 100755
index 0000000..d576a26
--- /dev/null
+++ b/tools/dreport
@@ -0,0 +1,205 @@
+#! /bin/bash
+
+help=$"
+ dreport creates an archive(xz compressed) consisting of the following:
+ * Configuration information
+ * Debug information
+ * A summary report
+ The type parameter controls the content of the data. The generated
+ archive is stored in the user specified location.
+
+usage: dreport [OPTION]
+
+Options:
+ -n, —-name <name> Name to be used for the archive.
+ Default name format obmcdump_<id>_<epochtime>
+ -d, —-dir <directory> Archive directory to copy the compressed report.
+ Default output directory is /tmp/dreport
+ -i, —-id <id> Dump identifier to associate with the archive.
+ Identifiers include numeric characters.
+ Default dump identifier is 0
+ -t, —-type <type> Data collection type. Valid types are
+ "user", "core".
+ Default type is "user" initiated.
+ -f, —-file <file> Optional file to be included in the archive.
+ Absolute path of the file must be passed as
+ parameter. This is useful to include application
+ core in the dump.
+ -s, --size <size> Maximum allowed size(in KB) of the archive.
+ Report will be truncated in case size exceeds
+ this limit. Default size is 500KB.
+ -v, —-verbose Increase logging verbosity.
+ -V, --version Output version information.
+ -q, —-quiet Only log fatal errors to stderr
+ -h, —-help Display this help and exit.
+"
+
+#CONSTANTS
+declare -r TRUE=1
+declare -r FALSE=0
+declare -r USERINITIATED_TYPE=0
+declare -r APPLICATIONCORED_TYPE=1
+declare -r DUMP_MAX_SIZE=500 #in KB
+declare -r SUMMARY_LOG="summary.log"
+declare -r DREPORT_LOG="dreport.log"
+declare -r TMP_DIR="/tmp/dreport"
+
+#VARIABLES
+declare -x name=$"obmcdump_00000000_$(date +"%s")"
+declare -x dump_dir="/tmp"
+declare -x dump_id=0
+declare -x dump_type=$USERINITIATED_TYPE
+declare -x verbose=$FALSE
+declare -x quiet=$FALSE
+declare -x dump_size=$DUMP_MAX_SIZE
+declare -x name_dir="$TMP_DIR/$name"
+
+# PACKAGE VERSION
+PACKAGE_VERSION="0.0.1"
+
+# @brief Packaging the dump and transferring to dump location.
+function package()
+{
+ mkdir -p "$dump_dir"
+ if [ $? -ne 0 ]; then
+ log_error "Could not create the destination directory $dump_dir"
+ dest_dir=$TMP_DIR
+ fi
+
+ #TODO openbmc/openbmc#1506 Enable file level compression.
+ #tar and compress the files.
+ tar_file="$name_dir.tar.xz"
+ tar -Jcf "$tar_file" -C "$TMP_DIR" "$name"
+
+ #remove the temporary name specific directory
+ rm -r "$name_dir"
+
+ #check the file size is in the allowed limit
+ if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then
+ echo "File size exceeds the limit allowed"
+ rm -rf "$TMP_DIR"
+ exit 1
+ #TODO openbmc/openbmc#1506 Revisit the error handling
+ fi
+
+ echo "Report is available in $dump_dir"
+
+ if [ "$TMP_DIR" == "$dump_dir" ]; then
+ return
+ fi
+
+ #copy the compressed tar file into the destination
+ cp "$tar_file" "$dump_dir"
+ if [ $? -ne 0 ]; then
+ echo "Failed to copy the $tar_file to $dump_dir"
+ return
+ else
+ rm -rf "$TMP_DIR"
+ fi
+}
+# @brief log the error message
+# @param error message
+function log_error()
+{
+ echo "ERROR: $@" >>"$name_dir/$DREPORT_LOG"
+ if ((quiet != TRUE)); then
+ echo "ERROR: $@" >&2
+ fi
+}
+
+# @brief log warning message
+# @param warning message
+function log_warning()
+{
+ if ((verbose == TRUE)); then
+ echo "WARNING: $@" >>"$name_dir/$DREPORT_LOG"
+ if ((quiet != TRUE)); then
+ echo "WARNING: $@" >&2
+ fi
+ fi
+}
+
+# @brief log info message
+# @param info message
+function log_info()
+{
+ if ((verbose == TRUE)); then
+ echo "INFO: $@" >>"$name_dir/$DREPORT_LOG"
+ if ((quiet != TRUE)); then
+ echo "INFO: $@" >&1
+ fi
+ fi
+}
+
+# @brief log summary message
+# @param message
+function log_summary()
+{
+ echo "$@" >> "$name_dir/$SUMMARY_LOG"
+ if ((quiet != TRUE)); then
+ echo "$@" >&1
+ fi
+}
+
+# @brief Main function
+function main()
+{
+ mkdir -p "$TMP_DIR/$name"
+ if [ $? -ne 0 ]; then
+ log_error "Failed to create the temporary directory."
+ exit;
+ fi
+
+ log_summary "Version: $PACKAGE_VERSION"
+
+ #TODO Add Dump report generating script.
+
+ package #package the dump
+}
+
+TEMP=`getopt -o n:d:i:t:s:f:vVqh \
+ --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \
+ -- "$@"`
+eval set -- "$TEMP"
+
+while [[ $# -gt 1 ]]; do
+ key="$1"
+ case $key in
+ -n|--name)
+ name=$2
+ shift 2;;
+ -d|--dir)
+ dump_dir=$2
+ shift 2;;
+ -i|--dumpid)
+ dump_id=$2
+ shift 2;;
+ -t|--type)
+ dump_type=$2
+ shift 2;;
+ -s|--size)
+ dump_size=$2
+ shift 2;;
+ -f|--file)
+ dump_file=$2
+ shift 2;;
+ -v|—-verbose)
+ verbose=$TRUE
+ shift;;
+ -V|--version)
+ shift;;
+ -q|—-quiet)
+ quiet=$TRUE
+ shift;;
+ -h|--help)
+ echo "$help"
+ exit;;
+ *) # unknown option
+ log_error "Unknown argument: $1"
+ log_info "$help"
+ exit 1;;
+ esac
+done
+
+main #main program
+exit $?