diff options
author | Jayanth Othayoth <ojayanth@in.ibm.com> | 2017-10-15 05:24:39 -0500 |
---|---|---|
committer | Jayanth Othayoth <ojayanth@in.ibm.com> | 2017-10-24 10:12:03 -0500 |
commit | 9d817ba6adab4f7fdd625c8c7f6c4b542a9a7cc2 (patch) | |
tree | 05efe3e15d95b68a4bc0fc323381c99643b58908 | |
parent | 8b7219f9bb1b5a988bcc6d2312b10670cee3c87d (diff) | |
download | phosphor-debug-collector-9d817ba6adab4f7fdd625c8c7f6c4b542a9a7cc2.tar.gz phosphor-debug-collector-9d817ba6adab4f7fdd625c8c7f6c4b542a9a7cc2.zip |
Initial version of the dreport common functions
Moved the common functions used by dreport and plugins into
new file.
Change-Id: Ia88fdae4dddc4ef61e3864437e2972807749502a
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
-rwxr-xr-x | tools/dreport.d/include.d/functions | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/tools/dreport.d/include.d/functions b/tools/dreport.d/include.d/functions new file mode 100755 index 0000000..a2af4da --- /dev/null +++ b/tools/dreport.d/include.d/functions @@ -0,0 +1,137 @@ +#!/usr/bin/env bash + +# @brief Execute the command and save the output into the dreport +# packaging, if it is in the user allowed dump size limit. +# @param $1 Command to be executed. +# @param $2 Save file name. +# @param $3 Plugin description used for logging. +function add_cmd_output() +{ + command="$1" + file_name="$2" + desc="$3" + + eval $command >> "$name_dir/$file_name" + if [ $? -ne 0 ]; then + log_error "Failed to collect $desc" + return 1 + fi + + if check_size "$name_dir/$file_name"; then + log_info "Collected $desc" + else + log_warning "Skipping $desc" + fi +} + +# @brief Copy the file or directory into the dreport packaging, +# if it is in the user allowed dump size limit. +# @param $1 Copy file or directory name. +# @param $2 Plugin description used for logging. +function add_copy_file() +{ + file_name="$1" + desc="$2" + + cp -r $file_name $name_dir + if [ $? -ne 0 ]; then + log_error "Failed to copy $desc $file_name" + return $RESOURCE_UNAVAILABLE + fi + if check_size "$name_dir/$(basename "$file_name")"; then + log_info "Copied $desc $file_name" + return $SUCCESS + else + return $RESOURCE_UNAVAILABLE + log_warning "Skipping copy $desc $file_name" + fi +} + +# @brief Calculate file or directory compressed size based on input +# and check whether the size in the allowed size limit. +# Remove the file or directory from the name_dir +# if the check fails. +# @param $1 Source file or directory +# @return 0 on success, error code if size exceeds the limit. +# Limitation: compress and tar will have few bytes size difference +function check_size() +{ + source=$1 + + #No size check required incase dump_size is set to unlimited + if [ $dump_size = $UNLIMITED ]; then + return 0 + fi + + #get the file or directory size + if [[ -d $source ]] && [[ -n $source ]]; then + tar -cf "$source.tar" -C \ + $(dirname "$source") $(basename "$source") + size=$(stat -c%s "$source.tar") + rm "$source.tar" + else + size=$(stat -c%s "$source") + fi + + if [ $((size + cur_dump_size)) -gt $dump_size ]; then + #Exceed the allowed limit, + #tar and compress the files and check the size + tar -Jcf "$name_dir.tar.xz" -C \ + $(dirname "$name_dir") $(basename "$name_dir") + size=$(stat -c%s "$name_dir.tar.xz") + if [ $size -gt $dump_size ]; then + #Remove the the specific data from the name_dir and continue + rm "$source" "$name_dir.tar.xz" + return $RESOURCE_UNAVAILABLE + else + rm "$name_dir.tar.xz" + fi + fi + + cur_dump_size=$((size + cur_dump_size)) + return $SUCCESS +} + +# @brief log the error message +# @param error message +function log_error() +{ + echo $($TIME_STAMP) "ERROR: $@" >> $dreport_log + if ((quiet != TRUE)); then + echo $($TIME_STAMP) "ERROR: $@" >&2 + fi +} + +# @brief log warning message +# @param warning message +function log_warning() +{ + if ((verbose == TRUE)); then + echo $($TIME_STAMP) "WARNING: $@" >> $dreport_log + if ((quiet != TRUE)); then + echo $($TIME_STAMP) "WARNING: $@" >&2 + fi + fi +} + +# @brief log info message +# @param info message +function log_info() +{ + if ((verbose == TRUE)); then + echo $($TIME_STAMP) "INFO: $@" >> $dreport_log + if ((quiet != TRUE)); then + echo $($TIME_STAMP) "INFO: $@" >&1 + fi + fi +} + +# @brief log summary message +# @param message +function log_summary() +{ + echo $($TIME_STAMP) "$@" >> $summary_log + if ((quiet != TRUE)); then + echo $($TIME_STAMP) "$@" >&1 + fi +} |