| 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. |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 17 | Default output directory is /tmp |
| Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 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 |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 40 | declare -r UNLIMITED="unlimited" |
| 41 | declare -r SUMMARY_DUMP="summary" |
| 42 | declare -r TYPE_USER="user" |
| 43 | declare -r TYPE_CORE="core" |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 44 | declare -r SUMMARY_LOG="summary.log" |
| 45 | declare -r DREPORT_LOG="dreport.log" |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 46 | declare -r TMP_DIR="/tmp" |
| 47 | declare -r EPOCHTIME=$(date +"%s") |
| 48 | |
| 49 | #Error Codes |
| 50 | declare -r SUCCESS="0" |
| 51 | declare -r INTERNAL_FAILURE="1" |
| 52 | declare -r RESOURCE_UNAVAILABLE="2" |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 53 | |
| 54 | #VARIABLES |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 55 | declare -x name="" |
| Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 56 | declare -x dump_dir="/tmp" |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 57 | declare -x dump_id="00000000" |
| 58 | declare -x dump_type=$TYPE_USER |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 59 | declare -x verbose=$FALSE |
| 60 | declare -x quiet=$FALSE |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 61 | declare -x dump_size="unlimited" |
| 62 | declare -x name_dir="" |
| 63 | declare -x optional_file="" |
| 64 | declare -x dreport_log="" |
| 65 | declare -x summary_log="" |
| Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame^] | 66 | declare -x cur_dump_size=0 |
| 67 | |
| 68 | # @brief Calculate file or directory compressed size based on input |
| 69 | # and check whether the size in the the allowed size limit. |
| 70 | # Remove the file or directory from the name_dir |
| 71 | # if the check fails. |
| 72 | # @param $1 Source file or directory |
| 73 | # @return 0 on success, error code if size exceeds the limit. |
| 74 | # Limitation: compress and tar will have few bytes size difference |
| 75 | # between tar and compression approch. |
| 76 | function check_size() |
| 77 | { |
| 78 | source=$1 |
| 79 | |
| 80 | #No size check required incase dump_size is set to unlimited |
| 81 | if [ $dump_size = $UNLIMITED ]; then |
| 82 | return 0 |
| 83 | fi |
| 84 | |
| 85 | #get the file or directory size |
| 86 | if [[ -d $source ]] && [[ -n $source ]]; then |
| 87 | tar -cf "$source.tar" -C \ |
| 88 | $(dirname "$source") $(basename "$source") |
| 89 | size=$(stat -c%s "$source.tar") |
| 90 | rm "$source.tar" |
| 91 | else |
| 92 | size=$(stat -c%s "$source") |
| 93 | fi |
| 94 | |
| 95 | if [ $((size + cur_dump_size)) -gt $dump_size ]; then |
| 96 | #Exceed the allowed limit, |
| 97 | #tar and compress the files and check the size |
| 98 | tar -Jcf "$name_dir.tar.xz" -C \ |
| 99 | $(dirname "$name_dir") $(basename "$name_dir") |
| 100 | size=$(stat -c%s "$name_dir.tar.xz") |
| 101 | if [ $size -gt $dump_size ]; then |
| 102 | #Remove the the specific data from the name_dir and contniue |
| 103 | rm "$source" "$name_dir.tar.xz" |
| 104 | return $RESOURCE_UNAVAILABLE |
| 105 | else |
| 106 | rm "$name_dir.tar.xz" |
| 107 | fi |
| 108 | fi |
| 109 | |
| 110 | #Remove the compressed file from the name directory |
| 111 | cur_dump_size=$((size + cur_dump_size)) |
| 112 | return $SUCCESS |
| 113 | } |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 114 | |
| Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame] | 115 | # @brief Initial version of the summary log |
| 116 | init_summary() |
| 117 | { |
| 118 | log_summary "Name: $name.tar.xz" |
| 119 | log_summary "Epochtime: $EPOCHTIME" |
| 120 | log_summary "ID: $dump_id" |
| 121 | log_summary "Type: $dump_type" |
| 122 | log_summary "Optional file: $optional_file" |
| 123 | } |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 124 | |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 125 | # @brief Check the validity of user inputs and initialize global |
| 126 | # variables. Create directory for temporary data collection |
| 127 | # @return 0 on success, error code otherwise |
| 128 | |
| 129 | function initialize() |
| 130 | { |
| 131 | #Dump file name |
| 132 | if [ -z $name ]; then |
| 133 | name=$"obmcdump_"$dump_id"_$EPOCHTIME" |
| 134 | fi |
| 135 | |
| 136 | #Create temporary data directory. |
| 137 | mkdir -p "$TMP_DIR/$name" |
| 138 | if [ $? -ne 0 ]; then |
| 139 | echo "Error: Failed to create the temporary directory." |
| 140 | return $RESOURCE_UNAVAILABLE; |
| 141 | fi |
| 142 | |
| 143 | #name directory |
| 144 | name_dir="$TMP_DIR/$name" |
| 145 | |
| 146 | #dreport log file |
| 147 | dreport_log="$name_dir/$DREPORT_LOG" |
| 148 | |
| 149 | #summary log file |
| 150 | summary_log="$name_dir/$SUMMARY_LOG" |
| 151 | |
| 152 | #Type |
| 153 | if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then |
| 154 | log_error "Invalid -type, Only summary log is available" |
| 155 | dump_type=$SUMMARY_DUMP |
| 156 | fi |
| 157 | |
| 158 | #Size |
| 159 | #Check the input is integer. |
| 160 | if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then |
| 161 | #Converts in to bytes. |
| 162 | dump_size="$((dump_size * 1024))" |
| 163 | else |
| 164 | dump_size=$UNLIMITED |
| 165 | fi |
| 166 | |
| 167 | return $SUCCESS |
| 168 | } |
| 169 | |
| Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 170 | # @brief Packaging the dump and transferring to dump location. |
| 171 | function package() |
| 172 | { |
| 173 | mkdir -p "$dump_dir" |
| 174 | if [ $? -ne 0 ]; then |
| 175 | log_error "Could not create the destination directory $dump_dir" |
| 176 | dest_dir=$TMP_DIR |
| 177 | fi |
| 178 | |
| 179 | #TODO openbmc/openbmc#1506 Enable file level compression. |
| 180 | #tar and compress the files. |
| 181 | tar_file="$name_dir.tar.xz" |
| 182 | tar -Jcf "$tar_file" -C "$TMP_DIR" "$name" |
| 183 | |
| 184 | #remove the temporary name specific directory |
| 185 | rm -r "$name_dir" |
| 186 | |
| 187 | #check the file size is in the allowed limit |
| 188 | if [ $(stat -c%s "$tar_file") -gt $dump_size ]; then |
| 189 | echo "File size exceeds the limit allowed" |
| 190 | rm -rf "$TMP_DIR" |
| 191 | exit 1 |
| 192 | #TODO openbmc/openbmc#1506 Revisit the error handling |
| 193 | fi |
| 194 | |
| 195 | echo "Report is available in $dump_dir" |
| 196 | |
| 197 | if [ "$TMP_DIR" == "$dump_dir" ]; then |
| 198 | return |
| 199 | fi |
| 200 | |
| 201 | #copy the compressed tar file into the destination |
| 202 | cp "$tar_file" "$dump_dir" |
| 203 | if [ $? -ne 0 ]; then |
| 204 | echo "Failed to copy the $tar_file to $dump_dir" |
| 205 | return |
| 206 | else |
| 207 | rm -rf "$TMP_DIR" |
| 208 | fi |
| 209 | } |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 210 | # @brief log the error message |
| 211 | # @param error message |
| 212 | function log_error() |
| 213 | { |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 214 | echo "ERROR: $@" >> $dreport_log |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 215 | if ((quiet != TRUE)); then |
| 216 | echo "ERROR: $@" >&2 |
| 217 | fi |
| 218 | } |
| 219 | |
| 220 | # @brief log warning message |
| 221 | # @param warning message |
| 222 | function log_warning() |
| 223 | { |
| 224 | if ((verbose == TRUE)); then |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 225 | echo "WARNING: $@" >> $dreport_log |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 226 | if ((quiet != TRUE)); then |
| 227 | echo "WARNING: $@" >&2 |
| 228 | fi |
| 229 | fi |
| 230 | } |
| 231 | |
| 232 | # @brief log info message |
| 233 | # @param info message |
| 234 | function log_info() |
| 235 | { |
| 236 | if ((verbose == TRUE)); then |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 237 | echo "INFO: $@" >> $dreport_log |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 238 | if ((quiet != TRUE)); then |
| 239 | echo "INFO: $@" >&1 |
| 240 | fi |
| 241 | fi |
| 242 | } |
| 243 | |
| 244 | # @brief log summary message |
| 245 | # @param message |
| 246 | function log_summary() |
| 247 | { |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 248 | echo "$@" >> $summary_log |
| 249 | if ((quiet != TRUE)); then |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 250 | echo "$@" >&1 |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 251 | fi |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 254 | # @brief Main function |
| 255 | function main() |
| 256 | { |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 257 | #initialize the global variables and |
| 258 | #create temporary storage locations |
| 259 | initialize |
| 260 | result=$? |
| 261 | if [[ ${result} -ne $SUCCESS ]]; then |
| 262 | echo $(date -u)" Error: Failed to initialize, Exiting" |
| 263 | exit; |
| 264 | fi |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 265 | |
| Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame] | 266 | #Initilize the summary log |
| 267 | init_summary |
| 268 | |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 269 | #TODO Add Dump report generating script. |
| Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 270 | |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 271 | package #package the dump |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | TEMP=`getopt -o n:d:i:t:s:f:vVqh \ |
| 275 | --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \ |
| 276 | -- "$@"` |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 277 | |
| 278 | if [ $? -ne 0 ] |
| 279 | then |
| 280 | echo "Error: Invalid options" |
| 281 | exit 1 |
| 282 | fi |
| 283 | |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 284 | eval set -- "$TEMP" |
| 285 | |
| 286 | while [[ $# -gt 1 ]]; do |
| 287 | key="$1" |
| 288 | case $key in |
| 289 | -n|--name) |
| 290 | name=$2 |
| 291 | shift 2;; |
| 292 | -d|--dir) |
| Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 293 | dump_dir=$2 |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 294 | shift 2;; |
| 295 | -i|--dumpid) |
| 296 | dump_id=$2 |
| 297 | shift 2;; |
| 298 | -t|--type) |
| 299 | dump_type=$2 |
| 300 | shift 2;; |
| 301 | -s|--size) |
| 302 | dump_size=$2 |
| 303 | shift 2;; |
| 304 | -f|--file) |
| Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 305 | optional_file=$2 |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 306 | shift 2;; |
| 307 | -v|—-verbose) |
| 308 | verbose=$TRUE |
| 309 | shift;; |
| 310 | -V|--version) |
| 311 | shift;; |
| 312 | -q|—-quiet) |
| 313 | quiet=$TRUE |
| 314 | shift;; |
| 315 | -h|--help) |
| 316 | echo "$help" |
| 317 | exit;; |
| 318 | *) # unknown option |
| Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 319 | log_error "Unknown argument: $1" |
| 320 | log_info "$help" |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 321 | exit 1;; |
| 322 | esac |
| Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 323 | done |
| Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 324 | |
| 325 | main #main program |
| 326 | exit $? |