| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
 | 2 |  | 
 | 3 | # @brief Execute the command and save the output into the dreport | 
 | 4 | #        packaging, if it is in the user allowed dump size limit. | 
 | 5 | # @param $1 Command to be executed. | 
 | 6 | # @param $2 Save file name. | 
 | 7 | # @param $3 Plugin description used for logging. | 
 | 8 | function add_cmd_output() | 
 | 9 | { | 
 | 10 |     command="$1" | 
 | 11 |     file_name="$2" | 
 | 12 |     desc="$3" | 
 | 13 |  | 
 | 14 |     eval $command >> "$name_dir/$file_name" | 
 | 15 |     if [ $? -ne 0 ]; then | 
 | 16 |         log_error "Failed to collect $desc" | 
 | 17 |         return 1 | 
 | 18 |     fi | 
 | 19 |  | 
 | 20 |     if check_size "$name_dir/$file_name"; then | 
 | 21 |         log_info "Collected $desc" | 
 | 22 |     else | 
 | 23 |         log_warning "Skipping $desc" | 
 | 24 |     fi | 
 | 25 | } | 
 | 26 |  | 
 | 27 | # @brief Copy the file or directory into the dreport packaging, | 
 | 28 | #        if it is in the user allowed dump size limit. | 
 | 29 | # @param $1 Copy file or directory name. | 
 | 30 | # @param $2 Plugin description used for logging. | 
 | 31 | function add_copy_file() | 
 | 32 | { | 
 | 33 |     file_name="$1" | 
 | 34 |     desc="$2" | 
 | 35 |  | 
 | 36 |     cp -r $file_name $name_dir | 
 | 37 |     if [ $? -ne 0 ]; then | 
 | 38 |         log_error "Failed to copy $desc $file_name" | 
 | 39 |         return $RESOURCE_UNAVAILABLE | 
 | 40 |     fi | 
 | 41 |     if check_size "$name_dir/$(basename "$file_name")"; then | 
 | 42 |         log_info "Copied $desc $file_name" | 
 | 43 |         return $SUCCESS | 
 | 44 |     else | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 45 |         log_warning "Skipping copy $desc $file_name" | 
| Jayanth Othayoth | a96d0b2 | 2022-02-17 00:50:28 -0600 | [diff] [blame^] | 46 |         return $RESOURCE_UNAVAILABLE | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 47 |     fi | 
 | 48 | } | 
| Marri Devender Rao | 5ba7176 | 2019-06-06 07:32:39 -0500 | [diff] [blame] | 49 | # @brief Copy the symbolic link file to the dreport packaging, | 
 | 50 | #        if it is in the user allowed dump size limit. | 
 | 51 | # @param $1 symbolic link file name | 
 | 52 | # @param $2 Plugin description used for logging. | 
 | 53 | function add_copy_sym_link_file() | 
 | 54 | { | 
 | 55 |     file_name="$1" | 
 | 56 |     desc="$2" | 
 | 57 |  | 
 | 58 |     cp $file_name $name_dir | 
 | 59 |     if [ $? -ne 0 ]; then | 
 | 60 |         log_error "Failed to copy $desc $file_name" | 
 | 61 |         return $RESOURCE_UNAVAILABLE | 
 | 62 |     fi | 
 | 63 |     if check_size "$name_dir/$(basename "$file_name")"; then | 
 | 64 |         log_info "Copied $desc $file_name" | 
 | 65 |         return $SUCCESS | 
 | 66 |     else | 
 | 67 |         log_warning "Skipping copy $desc $file_name" | 
 | 68 |         return $RESOURCE_UNAVAILABLE | 
 | 69 |     fi | 
 | 70 | } | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 71 |  | 
 | 72 | # @brief Calculate file or directory compressed size based on input | 
 | 73 | #        and check whether the size in the allowed size limit. | 
 | 74 | #        Remove the file or directory from the name_dir | 
 | 75 | #        if the check fails. | 
 | 76 | # @param $1 Source file or directory | 
 | 77 | # @return 0 on success, error code if size exceeds the limit. | 
 | 78 | # Limitation: compress and tar will have few bytes size difference | 
 | 79 | function check_size() | 
 | 80 | { | 
 | 81 |     source=$1 | 
 | 82 |  | 
| Gunnar Mills | b223b66 | 2018-04-08 15:02:06 -0500 | [diff] [blame] | 83 |     #No size check required in case dump_size is set to unlimited | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 84 |     if [ $dump_size = $UNLIMITED ]; then | 
 | 85 |         return 0 | 
 | 86 |     fi | 
 | 87 |  | 
 | 88 |     #get the file or directory size | 
 | 89 |     if [[ -d $source ]] && [[ -n $source ]]; then | 
 | 90 |         tar -cf "$source.tar" -C \ | 
 | 91 |                  $(dirname "$source") $(basename "$source") | 
 | 92 |         size=$(stat -c%s "$source.tar") | 
 | 93 |         rm "$source.tar" | 
 | 94 |     else | 
 | 95 |         size=$(stat -c%s "$source") | 
 | 96 |     fi | 
 | 97 |  | 
 | 98 |     if [ $((size + cur_dump_size)) -gt $dump_size ]; then | 
 | 99 |         #Exceed the allowed limit, | 
 | 100 |         #tar and compress the files and check the size | 
 | 101 |         tar -Jcf "$name_dir.tar.xz" -C \ | 
 | 102 |                   $(dirname "$name_dir") $(basename "$name_dir") | 
 | 103 |         size=$(stat -c%s "$name_dir.tar.xz") | 
 | 104 |         if [ $size -gt $dump_size ]; then | 
 | 105 |             #Remove the the specific data from the name_dir and continue | 
 | 106 |             rm "$source" "$name_dir.tar.xz" | 
 | 107 |             return $RESOURCE_UNAVAILABLE | 
 | 108 |         else | 
 | 109 |             rm "$name_dir.tar.xz" | 
 | 110 |         fi | 
 | 111 |     fi | 
 | 112 |  | 
 | 113 |     cur_dump_size=$((size + cur_dump_size)) | 
 | 114 |     return $SUCCESS | 
 | 115 | } | 
 | 116 |  | 
 | 117 | # @brief log the error message | 
 | 118 | # @param error message | 
 | 119 | function log_error() | 
 | 120 | { | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 121 |    echo $($TIME_STAMP) "ERROR: $*" >> $dreport_log | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 122 |    if ((quiet != TRUE)); then | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 123 |       echo $($TIME_STAMP) "ERROR: $*" >&2 | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 124 |    fi | 
 | 125 | } | 
 | 126 |  | 
 | 127 | # @brief log warning message | 
 | 128 | # @param warning message | 
 | 129 | function log_warning() | 
 | 130 | { | 
 | 131 |     if ((verbose == TRUE)); then | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 132 |         echo $($TIME_STAMP) "WARNING: $*" >> $dreport_log | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 133 |         if ((quiet != TRUE)); then | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 134 |             echo $($TIME_STAMP) "WARNING: $*" >&2 | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 135 |         fi | 
 | 136 |     fi | 
 | 137 | } | 
 | 138 |  | 
 | 139 | # @brief log info message | 
 | 140 | # @param info message | 
 | 141 | function log_info() | 
 | 142 | { | 
 | 143 |     if ((verbose == TRUE)); then | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 144 |         echo $($TIME_STAMP) "INFO: $*" >> $dreport_log | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 145 |         if ((quiet != TRUE)); then | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 146 |             echo $($TIME_STAMP) "INFO: $*" >&1 | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 147 |         fi | 
 | 148 |     fi | 
 | 149 | } | 
 | 150 |  | 
 | 151 | # @brief log summary message | 
 | 152 | # @param message | 
 | 153 | function log_summary() | 
 | 154 | { | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 155 |     echo $($TIME_STAMP) "$*" >> $summary_log | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 156 |     if ((quiet != TRUE)); then | 
| Dhruvaraj Subhashchandran | fdc0c3a | 2020-09-14 01:38:27 -0500 | [diff] [blame] | 157 |         echo $($TIME_STAMP) "$*" >&1 | 
| Jayanth Othayoth | 9d817ba | 2017-10-15 05:24:39 -0500 | [diff] [blame] | 158 |     fi | 
 | 159 | } |