Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | help=$" |
| 4 | dreport creates an archive(xz compressed) consisting of the following: |
| 5 | * Configuration information |
| 6 | * Debug information |
| 7 | * A summary report |
| 8 | The type parameter controls the content of the data. The generated |
| 9 | archive is stored in the user specified location. |
| 10 | |
| 11 | usage: dreport [OPTION] |
| 12 | |
| 13 | Options: |
| 14 | -n, —-name <name> Name to be used for the archive. |
| 15 | Default name format obmcdump_<id>_<epochtime> |
| 16 | -d, —-dir <directory> Archive directory to copy the compressed report. |
| 17 | Default output directory is /tmp/dreport |
| 18 | -i, —-id <id> Dump identifier to associate with the archive. |
| 19 | Identifiers include numeric characters. |
| 20 | Default dump identifier is 0 |
| 21 | -t, —-type <type> Data collection type. Valid types are |
| 22 | "user", "core". |
| 23 | Default type is "user" initiated. |
| 24 | -f, —-file <file> Optional file to be included in the archive. |
| 25 | Absolute path of the file must be passed as |
| 26 | parameter. This is useful to include application |
| 27 | core in the dump. |
| 28 | -s, --size <size> Maximum allowed size(in KB) of the archive. |
| 29 | Report will be truncated in case size exceeds |
| 30 | this limit. Default size is 500KB. |
| 31 | -v, —-verbose Increase logging verbosity. |
| 32 | -V, --version Output version information. |
| 33 | -q, —-quiet Only log fatal errors to stderr |
| 34 | -h, —-help Display this help and exit. |
| 35 | " |
| 36 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 37 | #CONSTANTS |
| 38 | declare -r TRUE=1 |
| 39 | declare -r FALSE=0 |
| 40 | declare -r USERINITIATED_TYPE=0 |
| 41 | declare -r APPLICATIONCORED_TYPE=1 |
| 42 | declare -r DUMP_MAX_SIZE=500 #in KB |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 43 | declare -r SUMMARY_LOG="summary.log" |
| 44 | declare -r DREPORT_LOG="dreport.log" |
| 45 | declare -r TMP_DIR="/tmp/dreport" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 46 | |
| 47 | #VARIABLES |
| 48 | declare -x name=$"obmcdump_00000000_$(date +"%s")" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 49 | declare -x dump_dir="/tmp" |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 50 | declare -x dump_id=0 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 51 | declare -x dump_type=$USERINITIATED_TYPE |
| 52 | declare -x verbose=$FALSE |
| 53 | declare -x quiet=$FALSE |
| 54 | declare -x dump_size=$DUMP_MAX_SIZE |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 55 | declare -x name_dir="$TMP_DIR/$name" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 56 | |
| 57 | # PACKAGE VERSION |
| 58 | PACKAGE_VERSION="0.0.1" |
| 59 | |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 60 | # @brief Packaging the dump and transferring to dump location. |
| 61 | function package() |
| 62 | { |
| 63 | mkdir -p "$dump_dir" |
| 64 | if [ $? -ne 0 ]; then |
| 65 | log_error "Could not create the destination directory $dump_dir" |
| 66 | dest_dir=$TMP_DIR |
| 67 | fi |
| 68 | |
| 69 | #TODO openbmc/openbmc#1506 Enable file level compression. |
| 70 | #tar and compress the files. |
| 71 | tar_file="$name_dir.tar.xz" |
| 72 | tar -Jcf "$tar_file" -C "$TMP_DIR" "$name" |
| 73 | |
| 74 | #remove the temporary name specific directory |
| 75 | rm -r "$name_dir" |
| 76 | |
| 77 | #check the file size is in the allowed limit |
| 78 | if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then |
| 79 | echo "File size exceeds the limit allowed" |
| 80 | rm -rf "$TMP_DIR" |
| 81 | exit 1 |
| 82 | #TODO openbmc/openbmc#1506 Revisit the error handling |
| 83 | fi |
| 84 | |
| 85 | echo "Report is available in $dump_dir" |
| 86 | |
| 87 | if [ "$TMP_DIR" == "$dump_dir" ]; then |
| 88 | return |
| 89 | fi |
| 90 | |
| 91 | #copy the compressed tar file into the destination |
| 92 | cp "$tar_file" "$dump_dir" |
| 93 | if [ $? -ne 0 ]; then |
| 94 | echo "Failed to copy the $tar_file to $dump_dir" |
| 95 | return |
| 96 | else |
| 97 | rm -rf "$TMP_DIR" |
| 98 | fi |
| 99 | } |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 100 | # @brief log the error message |
| 101 | # @param error message |
| 102 | function log_error() |
| 103 | { |
| 104 | echo "ERROR: $@" >>"$name_dir/$DREPORT_LOG" |
| 105 | if ((quiet != TRUE)); then |
| 106 | echo "ERROR: $@" >&2 |
| 107 | fi |
| 108 | } |
| 109 | |
| 110 | # @brief log warning message |
| 111 | # @param warning message |
| 112 | function log_warning() |
| 113 | { |
| 114 | if ((verbose == TRUE)); then |
| 115 | echo "WARNING: $@" >>"$name_dir/$DREPORT_LOG" |
| 116 | if ((quiet != TRUE)); then |
| 117 | echo "WARNING: $@" >&2 |
| 118 | fi |
| 119 | fi |
| 120 | } |
| 121 | |
| 122 | # @brief log info message |
| 123 | # @param info message |
| 124 | function log_info() |
| 125 | { |
| 126 | if ((verbose == TRUE)); then |
| 127 | echo "INFO: $@" >>"$name_dir/$DREPORT_LOG" |
| 128 | if ((quiet != TRUE)); then |
| 129 | echo "INFO: $@" >&1 |
| 130 | fi |
| 131 | fi |
| 132 | } |
| 133 | |
| 134 | # @brief log summary message |
| 135 | # @param message |
| 136 | function log_summary() |
| 137 | { |
| 138 | echo "$@" >> "$name_dir/$SUMMARY_LOG" |
| 139 | if ((quiet != TRUE)); then |
| 140 | echo "$@" >&1 |
| 141 | fi |
| 142 | } |
| 143 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 144 | # @brief Main function |
| 145 | function main() |
| 146 | { |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 147 | mkdir -p "$TMP_DIR/$name" |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 148 | if [ $? -ne 0 ]; then |
| 149 | log_error "Failed to create the temporary directory." |
| 150 | exit; |
| 151 | fi |
| 152 | |
| 153 | log_summary "Version: $PACKAGE_VERSION" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 154 | |
| 155 | #TODO Add Dump report generating script. |
| 156 | |
| 157 | package #package the dump |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | TEMP=`getopt -o n:d:i:t:s:f:vVqh \ |
| 161 | --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \ |
| 162 | -- "$@"` |
| 163 | eval set -- "$TEMP" |
| 164 | |
| 165 | while [[ $# -gt 1 ]]; do |
| 166 | key="$1" |
| 167 | case $key in |
| 168 | -n|--name) |
| 169 | name=$2 |
| 170 | shift 2;; |
| 171 | -d|--dir) |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 172 | dump_dir=$2 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 173 | shift 2;; |
| 174 | -i|--dumpid) |
| 175 | dump_id=$2 |
| 176 | shift 2;; |
| 177 | -t|--type) |
| 178 | dump_type=$2 |
| 179 | shift 2;; |
| 180 | -s|--size) |
| 181 | dump_size=$2 |
| 182 | shift 2;; |
| 183 | -f|--file) |
| 184 | dump_file=$2 |
| 185 | shift 2;; |
| 186 | -v|—-verbose) |
| 187 | verbose=$TRUE |
| 188 | shift;; |
| 189 | -V|--version) |
| 190 | shift;; |
| 191 | -q|—-quiet) |
| 192 | quiet=$TRUE |
| 193 | shift;; |
| 194 | -h|--help) |
| 195 | echo "$help" |
| 196 | exit;; |
| 197 | *) # unknown option |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 198 | log_error "Unknown argument: $1" |
| 199 | log_info "$help" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 200 | exit 1;; |
| 201 | esac |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 202 | done |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 203 | |
| 204 | main #main program |
| 205 | exit $? |