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 | } |
Swarnendu-R-C | 0df38f7 | 2024-07-19 06:38:32 -0500 | [diff] [blame^] | 163 | |
| 164 | # Redundant OS and Min FW Level |
| 165 | declare -x REDUNDANT_FW_VERSION="" |
| 166 | declare -x MIN_FW_VERSION_LEVEL="" |
| 167 | |
| 168 | # @brief Get Redundant OS and Min FW version |
| 169 | function populate_redundant_os_n_min_fw_info() |
| 170 | { |
| 171 | # Declare necessary dbus interfaces |
| 172 | dbus_object="xyz.openbmc_project.Software.BMC.Updater" |
| 173 | dbus_object_software="/xyz/openbmc_project/software/" |
| 174 | grep_command="grep $dbus_object_software" |
| 175 | dbus_tree_command="busctl --list --no-pager tree $dbus_object | $grep_command" |
| 176 | dbus_property_command="busctl get-property" |
| 177 | dbus_object_priority_method="xyz.openbmc_project.Software.RedundancyPriority" |
| 178 | dbus_object_priority="Priority" |
| 179 | dbus_object_version_method="xyz.openbmc_project.Software.Version" |
| 180 | dbus_object_version="Version" |
| 181 | dbus_object_min_version_method="xyz.openbmc_project.Software.MinimumVersion" |
| 182 | dbus_object_min_version="MinimumVersion" |
| 183 | |
| 184 | # Declare an array to store the results of dbus command |
| 185 | read_array=() |
| 186 | |
| 187 | IFS=$'\n' read -r -d '' -a read_array < <( eval "$dbus_tree_command" && printf '\0' ) |
| 188 | |
| 189 | array_length=${#read_array[@]} |
| 190 | |
| 191 | # If there is only one FW image on the BMC, |
| 192 | # then there is no question of redundant FW |
| 193 | # but we can still try to get the min FW info from it |
| 194 | if [ "$array_length" -lt 2 ]; then |
| 195 | firmware=$(echo "${read_array[0]}" | xargs) |
| 196 | |
| 197 | dbus_command="$dbus_property_command $dbus_object $firmware \ |
| 198 | $dbus_object_min_version_method $dbus_object_min_version" |
| 199 | |
| 200 | MIN_FW_VERSION_LEVEL=$(eval "$dbus_command" | cut -d' ' -f 2-) |
| 201 | |
| 202 | return "$SUCCESS" |
| 203 | fi |
| 204 | |
| 205 | firmware1=$(echo "${read_array[0]}" | xargs) |
| 206 | firmware2=$(echo "${read_array[1]}" | xargs) |
| 207 | |
| 208 | # Get the priority of the first firmware image. |
| 209 | # The one with the highest prirority amongst the two is the backup one |
| 210 | # And the one with the lowest priority amongst the two is the active one |
| 211 | dbus_command="$dbus_property_command $dbus_object $firmware1 \ |
| 212 | $dbus_object_priority_method $dbus_object_priority" |
| 213 | |
| 214 | firmware1_priority=$(eval "$dbus_command" | cut -d' ' -f 2) |
| 215 | |
| 216 | # If FW1 is the redundant one then get the redundant info from FW1 |
| 217 | # and get the min FW version info from FW2 or vice-versa |
| 218 | firmware_primary="" |
| 219 | firmware_secondary="" |
| 220 | |
| 221 | if [ 1 -eq "$firmware1_priority" ]; then |
| 222 | firmware_primary="$firmware2" |
| 223 | firmware_secondary="$firmware1" |
| 224 | else |
| 225 | firmware_primary="$firmware1" |
| 226 | firmware_secondary="$firmware2" |
| 227 | fi |
| 228 | |
| 229 | dbus_command="$dbus_property_command $dbus_object $firmware_secondary \ |
| 230 | $dbus_object_version_method $dbus_object_version" |
| 231 | REDUNDANT_FW_VERSION=$(eval "$dbus_command" | cut -d' ' -f 2-) |
| 232 | |
| 233 | dbus_command="$dbus_property_command $dbus_object $firmware_primary \ |
| 234 | $dbus_object_min_version_method $dbus_object_min_version" |
| 235 | MIN_FW_VERSION_LEVEL=$(eval "$dbus_command" | cut -d' ' -f 2-) |
| 236 | } |