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