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") |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 48 | declare -r TIME_STAMP="date -u" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 49 | |
| 50 | #Error Codes |
| 51 | declare -r SUCCESS="0" |
| 52 | declare -r INTERNAL_FAILURE="1" |
| 53 | declare -r RESOURCE_UNAVAILABLE="2" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 54 | |
| 55 | #VARIABLES |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 56 | declare -x name="" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 57 | declare -x dump_dir="/tmp" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 58 | declare -x dump_id="00000000" |
| 59 | declare -x dump_type=$TYPE_USER |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 60 | declare -x verbose=$FALSE |
| 61 | declare -x quiet=$FALSE |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 62 | declare -x dump_size="unlimited" |
| 63 | declare -x name_dir="" |
| 64 | declare -x optional_file="" |
| 65 | declare -x dreport_log="" |
| 66 | declare -x summary_log="" |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 67 | declare -x cur_dump_size=0 |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 68 | declare -a command_list=("") |
Jayanth Othayoth | 47a1416 | 2017-08-09 07:10:39 -0500 | [diff] [blame] | 69 | declare -x core_pid="0" |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 70 | |
| 71 | # @brief Initialize user type command array |
| 72 | function runlevel_user() |
| 73 | { |
| 74 | command_list=( |
| 75 | get_fw_level |
| 76 | get_uname |
| 77 | get_uptime |
| 78 | get_disk_usage |
| 79 | bmc_state |
| 80 | host_state |
| 81 | chassis_state |
| 82 | get_host_info |
| 83 | get_obmc_console |
| 84 | get_cpuinfo |
| 85 | get_meminfo |
| 86 | get_top |
| 87 | get_esel |
| 88 | get_journal |
| 89 | get_failed_services |
| 90 | ) |
| 91 | } |
| 92 | |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 93 | # @brief Initialize core type command array |
| 94 | function runlevel_core() |
| 95 | { |
| 96 | command_list=( |
| 97 | move_optional_file |
| 98 | get_proc_journal |
| 99 | get_fw_level |
| 100 | get_uname |
| 101 | get_uptime |
| 102 | get_disk_usage |
| 103 | bmc_state |
| 104 | host_state |
| 105 | chassis_state |
| 106 | get_host_info |
| 107 | get_failed_services |
| 108 | get_obmc_console |
| 109 | get_cpuinfo |
| 110 | get_meminfo |
| 111 | get_top |
| 112 | ) |
| 113 | } |
| 114 | |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 115 | function get_fw_level() |
| 116 | { |
| 117 | desc="Firmware Release" |
| 118 | command="cat /etc/os-release" |
| 119 | copy_loc="firmware_release.log" |
| 120 | run_command "$command" "$copy_loc" "$desc" |
| 121 | } |
| 122 | |
| 123 | function get_uname() |
| 124 | { |
| 125 | desc="uname" |
| 126 | command="uname -a" |
| 127 | copy_loc="uname.log" |
| 128 | run_command "$command" "$copy_loc" "$desc" |
| 129 | } |
| 130 | |
| 131 | function get_uptime() |
| 132 | { |
| 133 | desc="uptime" |
| 134 | command="uptime" |
| 135 | copy_loc="uptime.log" |
| 136 | run_command "$command" "$copy_loc" "$desc" |
| 137 | } |
| 138 | |
| 139 | function get_disk_usage() |
| 140 | { |
| 141 | desc="Disk Usage" |
| 142 | command="df -hT" |
| 143 | copy_loc="disk_usage.log" |
| 144 | run_command "$command" "$copy_loc" "$desc" |
| 145 | } |
| 146 | |
| 147 | function get_journal() |
| 148 | { |
| 149 | desc="Journal log" |
| 150 | command="journalctl -o json-pretty -r" |
| 151 | copy_loc="journal.log" |
| 152 | run_command "$command" "$copy_loc" "$desc" |
| 153 | } |
| 154 | |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 155 | function get_proc_journal() |
| 156 | { |
| 157 | desc="Process Journal log" |
| 158 | command="journalctl -o verbose _PID=$core_pid" |
| 159 | copy_loc="proc_journal.log" |
| 160 | run_command "$command" "$copy_loc" "$desc" |
| 161 | } |
| 162 | |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 163 | function get_host_info() |
| 164 | { |
| 165 | desc="Host information" |
| 166 | command="hostnamectl status" |
| 167 | copy_loc="hostnamectl.log" |
| 168 | run_command "$command" "$copy_loc" "$desc" |
| 169 | } |
| 170 | |
| 171 | function get_failed_services() |
| 172 | { |
| 173 | desc="Failed Services" |
| 174 | command="systemctl --failed" |
| 175 | copy_loc="failed_services.log" |
| 176 | run_command "$command" "$copy_loc" "$desc" |
| 177 | } |
| 178 | |
| 179 | function get_obmc_console() |
| 180 | { |
| 181 | desc="OBMC Console" |
| 182 | command="cat /var/log/obmc-console.log" |
| 183 | copy_loc="obmc_console.log" |
| 184 | run_command "$command" "$copy_loc" "$desc" |
| 185 | } |
| 186 | |
| 187 | function get_cpuinfo() |
| 188 | { |
| 189 | desc="cpuinfo" |
| 190 | command="cat /proc/cpuinfo" |
| 191 | copy_loc="cpuinfo.log" |
| 192 | run_command "$command" "$copy_loc" "$desc" |
| 193 | } |
| 194 | |
| 195 | function get_meminfo() |
| 196 | { |
| 197 | desc="meminfo" |
| 198 | command="cat /proc/meminfo" |
| 199 | copy_loc="meminfo.log" |
| 200 | run_command "$command" "$copy_loc" "$desc" |
| 201 | } |
| 202 | |
| 203 | function get_top() |
| 204 | { |
| 205 | desc="top" |
| 206 | command="top -n 1 -b" |
| 207 | copy_loc="top.log" |
| 208 | run_command "$command" "$copy_loc" "$desc" |
| 209 | } |
| 210 | |
| 211 | function bmc_state() |
| 212 | { |
| 213 | desc="BMC State" |
| 214 | command="busctl get-property \ |
| 215 | xyz.openbmc_project.State.BMC \ |
| 216 | /xyz/openbmc_project/state/bmc0 \ |
| 217 | xyz.openbmc_project.State.BMC \ |
| 218 | CurrentBMCState" |
| 219 | copy_loc="BMCState.log" |
| 220 | run_command "$command" "$copy_loc" "$desc" |
| 221 | } |
| 222 | |
| 223 | function host_state() |
| 224 | { |
| 225 | desc="Host State" |
| 226 | command="busctl get-property \ |
| 227 | xyz.openbmc_project.State.Host \ |
| 228 | /xyz/openbmc_project/state/host0 \ |
| 229 | xyz.openbmc_project.State.Host \ |
| 230 | CurrentHostState" |
| 231 | copy_loc="HostState.log" |
| 232 | run_command "$command" "$copy_loc" "$desc" |
| 233 | } |
| 234 | |
| 235 | function chassis_state() |
| 236 | { |
| 237 | desc="Chassis State" |
| 238 | command="busctl get-property \ |
| 239 | xyz.openbmc_project.State.Chassis \ |
| 240 | /xyz/openbmc_project/state/chassis0 \ |
| 241 | xyz.openbmc_project.State.Chassis \ |
| 242 | CurrentPowerState" |
| 243 | copy_loc="HostState.log" |
| 244 | run_command "$command" "$copy_loc" "$desc" |
| 245 | } |
| 246 | |
| 247 | function get_esel() |
| 248 | { |
| 249 | desc="eSEL" |
| 250 | |
| 251 | entries=$(busctl --list --no-pager tree \ |
| 252 | xyz.openbmc_project.Logging | grep \ |
| 253 | '/xyz/openbmc_project/logging/entry/') |
| 254 | |
| 255 | #check for eSEL entries. |
| 256 | if [ -z "$entries" ]; then |
| 257 | log_info "No $desc entries" |
| 258 | return 0 |
| 259 | fi |
| 260 | |
| 261 | command="busctl --list --no-pager tree \ |
| 262 | xyz.openbmc_project.Logging | grep \ |
| 263 | '/xyz/openbmc_project/logging/entry/' \ |
| 264 | | xargs -I {} busctl --verbose --no-pager \ |
| 265 | call xyz.openbmc_project.Logging {} \ |
| 266 | org.freedesktop.DBus.Properties GetAll s \ |
| 267 | xyz.openbmc_project.Logging.Entry" |
| 268 | copy_loc="eSEL.log" |
| 269 | run_command "$command" "$copy_loc" "$desc" |
| 270 | } |
| 271 | |
Jayanth Othayoth | e61aee3 | 2017-08-09 07:05:06 -0500 | [diff] [blame] | 272 | function move_optional_file() |
| 273 | { |
| 274 | desc="Move Optional file" |
| 275 | |
| 276 | mv $optional_file $name_dir |
| 277 | if [ $? -ne 0 ]; then |
| 278 | log_error "Failed to move file $optional_file" |
| 279 | return 1 |
| 280 | fi |
| 281 | if check_size "$name_dir/$(basename "$optional_file")"; then |
| 282 | log_info "Moving file $file_name" |
| 283 | else |
| 284 | log_warning "Skipping $file_name move" |
| 285 | fi |
| 286 | } |
| 287 | |
Jayanth Othayoth | 0862c48 | 2017-08-09 07:02:36 -0500 | [diff] [blame] | 288 | # @brief Run the requested command and save the output |
| 289 | # into temporary location for successful size check |
| 290 | |
| 291 | function run_command() |
| 292 | { |
| 293 | command="$1" |
| 294 | copy_loc="$2" |
| 295 | desc="$3" |
| 296 | |
| 297 | eval $command >> "$name_dir/$copy_loc" |
| 298 | if [ $? -ne 0 ]; then |
| 299 | log_error "Failed to collect $desc" |
| 300 | return 1 |
| 301 | fi |
| 302 | |
| 303 | if check_size "$name_dir/$copy_loc"; then |
| 304 | log_info "Collected $desc" |
| 305 | else |
| 306 | log_warning "Skipping $desc" |
| 307 | fi |
| 308 | } |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 309 | |
Jayanth Othayoth | acf46e3 | 2017-08-09 07:15:18 -0500 | [diff] [blame] | 310 | # @brief Initiate data collection based on the type. |
| 311 | # @return 0 on success, error code otherwise |
| 312 | function collect_data() |
| 313 | { |
| 314 | case $dump_type in |
| 315 | $TYPE_USER) |
| 316 | runlevel_user |
| 317 | capture_data "${command_list[@]}" |
| 318 | ;; |
| 319 | $TYPE_CORE) |
| 320 | set_pid |
| 321 | runlevel_core |
| 322 | capture_data "${command_list[@]}" |
| 323 | ;; |
| 324 | $SUMMARY_DUMP) |
| 325 | #No data collection is required. |
| 326 | ;; |
| 327 | *) # unknown option |
| 328 | log_error "Skipping: Unknown dump type: $dump_type" |
| 329 | ;; |
| 330 | esac |
| 331 | } |
| 332 | |
Jayanth Othayoth | 47a1416 | 2017-08-09 07:10:39 -0500 | [diff] [blame] | 333 | # @brief get pid from the file |
| 334 | # dreport "core" type user provides core file as optional file parameter. |
| 335 | # As per coredump source code systemd-coredump uses below format |
| 336 | # https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c |
| 337 | # /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “. |
| 338 | # <process ID>.%s000000" |
| 339 | # Added additional check to make sure that user provided in |
| 340 | # systemd-coredump format. |
| 341 | function set_pid() |
| 342 | { |
| 343 | #from coredump.c file. |
| 344 | CORE_BASE_STRING="/var/lib/systemd/coredump/core" |
| 345 | |
| 346 | #Escape bash characters in file name |
| 347 | file=$(printf %q "$optional_file") |
| 348 | |
| 349 | string=$(echo $file | awk -F . '{ print $1}') |
| 350 | if [ "$CORE_BASE_STRING" = "$string" ]; then |
| 351 | #matching systemd-coredump core file format. |
| 352 | core_pid=$(echo $file | awk -F . '{ print $5}') |
| 353 | fi |
| 354 | } |
| 355 | |
Jayanth Othayoth | f1e02d3 | 2017-08-09 07:06:45 -0500 | [diff] [blame] | 356 | # @brief Capture debug data based on the input command array. |
| 357 | # and stores in to global temporary name specific location. |
| 358 | # @param $1 Source array |
| 359 | # @return 0 on success, error code otherwise |
| 360 | function capture_data() |
| 361 | { |
| 362 | source=("$@") |
| 363 | |
| 364 | for ((i=0;i<${#source[@]};i+=1)); do |
| 365 | ${source[i]} |
| 366 | done |
| 367 | |
| 368 | return 0 |
| 369 | } |
| 370 | |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 371 | # @brief Calculate file or directory compressed size based on input |
| 372 | # and check whether the size in the the allowed size limit. |
| 373 | # Remove the file or directory from the name_dir |
| 374 | # if the check fails. |
| 375 | # @param $1 Source file or directory |
| 376 | # @return 0 on success, error code if size exceeds the limit. |
| 377 | # Limitation: compress and tar will have few bytes size difference |
Jayanth Othayoth | c2ece2d | 2017-08-09 06:57:12 -0500 | [diff] [blame] | 378 | function check_size() |
| 379 | { |
| 380 | source=$1 |
| 381 | |
| 382 | #No size check required incase dump_size is set to unlimited |
| 383 | if [ $dump_size = $UNLIMITED ]; then |
| 384 | return 0 |
| 385 | fi |
| 386 | |
| 387 | #get the file or directory size |
| 388 | if [[ -d $source ]] && [[ -n $source ]]; then |
| 389 | tar -cf "$source.tar" -C \ |
| 390 | $(dirname "$source") $(basename "$source") |
| 391 | size=$(stat -c%s "$source.tar") |
| 392 | rm "$source.tar" |
| 393 | else |
| 394 | size=$(stat -c%s "$source") |
| 395 | fi |
| 396 | |
| 397 | if [ $((size + cur_dump_size)) -gt $dump_size ]; then |
| 398 | #Exceed the allowed limit, |
| 399 | #tar and compress the files and check the size |
| 400 | tar -Jcf "$name_dir.tar.xz" -C \ |
| 401 | $(dirname "$name_dir") $(basename "$name_dir") |
| 402 | size=$(stat -c%s "$name_dir.tar.xz") |
| 403 | if [ $size -gt $dump_size ]; then |
| 404 | #Remove the the specific data from the name_dir and contniue |
| 405 | rm "$source" "$name_dir.tar.xz" |
| 406 | return $RESOURCE_UNAVAILABLE |
| 407 | else |
| 408 | rm "$name_dir.tar.xz" |
| 409 | fi |
| 410 | fi |
| 411 | |
| 412 | #Remove the compressed file from the name directory |
| 413 | cur_dump_size=$((size + cur_dump_size)) |
| 414 | return $SUCCESS |
| 415 | } |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 416 | |
Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame] | 417 | # @brief Initial version of the summary log |
| 418 | init_summary() |
| 419 | { |
| 420 | log_summary "Name: $name.tar.xz" |
| 421 | log_summary "Epochtime: $EPOCHTIME" |
| 422 | log_summary "ID: $dump_id" |
| 423 | log_summary "Type: $dump_type" |
| 424 | log_summary "Optional file: $optional_file" |
| 425 | } |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 426 | |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 427 | # @brief Check the validity of user inputs and initialize global |
| 428 | # variables. Create directory for temporary data collection |
| 429 | # @return 0 on success, error code otherwise |
| 430 | |
| 431 | function initialize() |
| 432 | { |
| 433 | #Dump file name |
| 434 | if [ -z $name ]; then |
| 435 | name=$"obmcdump_"$dump_id"_$EPOCHTIME" |
| 436 | fi |
| 437 | |
| 438 | #Create temporary data directory. |
| 439 | mkdir -p "$TMP_DIR/$name" |
| 440 | if [ $? -ne 0 ]; then |
| 441 | echo "Error: Failed to create the temporary directory." |
| 442 | return $RESOURCE_UNAVAILABLE; |
| 443 | fi |
| 444 | |
| 445 | #name directory |
| 446 | name_dir="$TMP_DIR/$name" |
| 447 | |
| 448 | #dreport log file |
| 449 | dreport_log="$name_dir/$DREPORT_LOG" |
| 450 | |
| 451 | #summary log file |
| 452 | summary_log="$name_dir/$SUMMARY_LOG" |
| 453 | |
| 454 | #Type |
| 455 | if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then |
| 456 | log_error "Invalid -type, Only summary log is available" |
| 457 | dump_type=$SUMMARY_DUMP |
| 458 | fi |
| 459 | |
| 460 | #Size |
| 461 | #Check the input is integer. |
| 462 | if [ "$dump_size" -eq "$dump_size" ] 2>/dev/null; then |
| 463 | #Converts in to bytes. |
| 464 | dump_size="$((dump_size * 1024))" |
| 465 | else |
| 466 | dump_size=$UNLIMITED |
| 467 | fi |
| 468 | |
| 469 | return $SUCCESS |
| 470 | } |
| 471 | |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 472 | # @brief Packaging the dump and transferring to dump location. |
| 473 | function package() |
| 474 | { |
| 475 | mkdir -p "$dump_dir" |
| 476 | if [ $? -ne 0 ]; then |
| 477 | log_error "Could not create the destination directory $dump_dir" |
| 478 | dest_dir=$TMP_DIR |
| 479 | fi |
| 480 | |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 481 | #tar and compress the files. |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 482 | tar -Jcf "$name_dir.tar.xz" -C \ |
| 483 | $(dirname "$name_dir") $(basename "$name_dir") |
| 484 | |
| 485 | if [ $? -ne 0 ]; then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 486 | echo $($TIME_STAMP) "Could not create the compressed tar file" |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 487 | rm -r "$name_dir" |
| 488 | return $INTERNAL_FAILURE |
| 489 | fi |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 490 | |
| 491 | #remove the temporary name specific directory |
| 492 | rm -r "$name_dir" |
| 493 | |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 494 | echo $($TIME_STAMP) "Report is available in $dump_dir" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 495 | |
| 496 | if [ "$TMP_DIR" == "$dump_dir" ]; then |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 497 | return $SUCCESS |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 498 | fi |
| 499 | |
| 500 | #copy the compressed tar file into the destination |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 501 | cp "$name_dir.tar.xz" "$dump_dir" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 502 | if [ $? -ne 0 ]; then |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 503 | echo "Failed to copy the $name_dir.tar.xz to $dump_dir" |
| 504 | rm "$name_dir.tar.xz" |
| 505 | return $INTERNAL_FAILURE |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 506 | fi |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 507 | |
| 508 | #Remove the temporary copy of the file |
| 509 | rm "$name_dir.tar.xz" |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 510 | } |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 511 | |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 512 | # @brief log the error message |
| 513 | # @param error message |
| 514 | function log_error() |
| 515 | { |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 516 | echo $($TIME_STAMP) "ERROR: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 517 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 518 | echo $($TIME_STAMP) "ERROR: $@" >&2 |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 519 | fi |
| 520 | } |
| 521 | |
| 522 | # @brief log warning message |
| 523 | # @param warning message |
| 524 | function log_warning() |
| 525 | { |
| 526 | if ((verbose == TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 527 | echo $($TIME_STAMP) "WARNING: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 528 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 529 | echo $($TIME_STAMP) "WARNING: $@" >&2 |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 530 | fi |
| 531 | fi |
| 532 | } |
| 533 | |
| 534 | # @brief log info message |
| 535 | # @param info message |
| 536 | function log_info() |
| 537 | { |
| 538 | if ((verbose == TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 539 | echo $($TIME_STAMP) "INFO: $@" >> $dreport_log |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 540 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 541 | echo $($TIME_STAMP) "INFO: $@" >&1 |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 542 | fi |
| 543 | fi |
| 544 | } |
| 545 | |
| 546 | # @brief log summary message |
| 547 | # @param message |
| 548 | function log_summary() |
| 549 | { |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 550 | echo $($TIME_STAMP) "$@" >> $summary_log |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 551 | if ((quiet != TRUE)); then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 552 | echo $($TIME_STAMP) "$@" >&1 |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 553 | fi |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 554 | } |
| 555 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 556 | # @brief Main function |
| 557 | function main() |
| 558 | { |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 559 | #initialize the global variables and |
| 560 | #create temporary storage locations |
| 561 | initialize |
| 562 | result=$? |
| 563 | if [[ ${result} -ne $SUCCESS ]]; then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 564 | echo $($TIME_STAMP) "Error: Failed to initialize, Exiting" |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 565 | exit; |
| 566 | fi |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 567 | |
Jayanth Othayoth | e20d5e0 | 2017-08-09 06:48:45 -0500 | [diff] [blame] | 568 | #Initilize the summary log |
| 569 | init_summary |
| 570 | |
Jayanth Othayoth | acf46e3 | 2017-08-09 07:15:18 -0500 | [diff] [blame] | 571 | #collect data based on the type. |
| 572 | collect_data |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 573 | |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 574 | package #package the dump |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 575 | result=$? |
| 576 | if [[ ${result} -ne $SUCCESS ]]; then |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 577 | echo $($TIME_STAMP) "Error: Failed to package, Exiting" |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 578 | else |
Jayanth Othayoth | ea4b5e5 | 2017-08-09 07:31:24 -0500 | [diff] [blame] | 579 | echo $($TIME_STAMP) "Sucessfully completed" |
Jayanth Othayoth | 8d0446e | 2017-08-09 07:26:45 -0500 | [diff] [blame] | 580 | exit; |
| 581 | fi |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | TEMP=`getopt -o n:d:i:t:s:f:vVqh \ |
| 585 | --long name:,dir:,dumpid:,type:,size:,file:,verbose,version,quiet,help \ |
| 586 | -- "$@"` |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 587 | |
| 588 | if [ $? -ne 0 ] |
| 589 | then |
| 590 | echo "Error: Invalid options" |
| 591 | exit 1 |
| 592 | fi |
| 593 | |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 594 | eval set -- "$TEMP" |
| 595 | |
| 596 | while [[ $# -gt 1 ]]; do |
| 597 | key="$1" |
| 598 | case $key in |
| 599 | -n|--name) |
| 600 | name=$2 |
| 601 | shift 2;; |
| 602 | -d|--dir) |
Jayanth Othayoth | ff0699d | 2017-07-26 07:53:03 -0500 | [diff] [blame] | 603 | dump_dir=$2 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 604 | shift 2;; |
| 605 | -i|--dumpid) |
| 606 | dump_id=$2 |
| 607 | shift 2;; |
| 608 | -t|--type) |
| 609 | dump_type=$2 |
| 610 | shift 2;; |
| 611 | -s|--size) |
| 612 | dump_size=$2 |
| 613 | shift 2;; |
| 614 | -f|--file) |
Jayanth Othayoth | 230e9a3 | 2017-08-09 06:39:59 -0500 | [diff] [blame] | 615 | optional_file=$2 |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 616 | shift 2;; |
| 617 | -v|—-verbose) |
| 618 | verbose=$TRUE |
| 619 | shift;; |
| 620 | -V|--version) |
| 621 | shift;; |
| 622 | -q|—-quiet) |
| 623 | quiet=$TRUE |
| 624 | shift;; |
| 625 | -h|--help) |
| 626 | echo "$help" |
| 627 | exit;; |
| 628 | *) # unknown option |
Jayanth Othayoth | 6d3ee1c | 2017-07-26 05:18:31 -0500 | [diff] [blame] | 629 | log_error "Unknown argument: $1" |
| 630 | log_info "$help" |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 631 | exit 1;; |
| 632 | esac |
Jayanth Othayoth | 9e95f4b | 2017-07-24 06:42:24 -0500 | [diff] [blame] | 633 | done |
Jayanth Othayoth | 7b77487 | 2017-07-26 05:02:53 -0500 | [diff] [blame] | 634 | |
| 635 | main #main program |
| 636 | exit $? |