blob: 15a9d71b1bc373b26b2363488f43e05b2222c240 [file] [log] [blame]
#!/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"
rm -f "$name_dir/$file_name"
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 -Lr $file_name $name_dir
if [ $? -ne 0 ]; then
log_error "Failed to copy $desc $file_name"
rm -fr "$name_dir/$file_name"
return $RESOURCE_UNAVAILABLE
fi
if check_size "$name_dir/$(basename "$file_name")"; then
log_info "Copied $desc $file_name"
return $SUCCESS
else
log_warning "Skipping copy $desc $file_name"
return $RESOURCE_UNAVAILABLE
fi
}
# @brief Copy the symbolic link file to the dreport packaging,
# if it is in the user allowed dump size limit.
# @param $1 symbolic link file name
# @param $2 Plugin description used for logging.
function add_copy_sym_link_file()
{
file_name="$1"
desc="$2"
cp $file_name $name_dir
if [ $? -ne 0 ]; then
log_error "Failed to copy $desc $file_name"
rm -fr "$name_dir/$file_name"
return $RESOURCE_UNAVAILABLE
fi
if check_size "$name_dir/$(basename "$file_name")"; then
log_info "Copied $desc $file_name"
return $SUCCESS
else
log_warning "Skipping copy $desc $file_name"
return $RESOURCE_UNAVAILABLE
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 in case 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
}
# Redundant OS and Min FW Level
declare -x REDUNDANT_FW_VERSION=""
declare -x MIN_FW_VERSION_LEVEL=""
# @brief Get Redundant OS and Min FW version
function populate_redundant_os_n_min_fw_info()
{
# Declare necessary dbus interfaces
dbus_object="xyz.openbmc_project.Software.BMC.Updater"
dbus_object_software="/xyz/openbmc_project/software/"
grep_command="grep $dbus_object_software"
dbus_tree_command="busctl --list --no-pager tree $dbus_object | $grep_command"
dbus_property_command="busctl get-property"
dbus_object_priority_method="xyz.openbmc_project.Software.RedundancyPriority"
dbus_object_priority="Priority"
dbus_object_version_method="xyz.openbmc_project.Software.Version"
dbus_object_version="Version"
dbus_object_min_version_method="xyz.openbmc_project.Software.MinimumVersion"
dbus_object_min_version="MinimumVersion"
# Declare an array to store the results of dbus command
read_array=()
IFS=$'\n' read -r -d '' -a read_array < <( eval "$dbus_tree_command" && printf '\0' )
array_length=${#read_array[@]}
# If there is only one FW image on the BMC,
# then there is no question of redundant FW
# but we can still try to get the min FW info from it
if [ "$array_length" -lt 2 ]; then
firmware=$(echo "${read_array[0]}" | xargs)
dbus_command="$dbus_property_command $dbus_object $firmware \
$dbus_object_min_version_method $dbus_object_min_version"
MIN_FW_VERSION_LEVEL=$(eval "$dbus_command" | cut -d' ' -f 2-)
return "$SUCCESS"
fi
firmware1=$(echo "${read_array[0]}" | xargs)
firmware2=$(echo "${read_array[1]}" | xargs)
# Get the priority of the first firmware image.
# The one with the highest prirority amongst the two is the backup one
# And the one with the lowest priority amongst the two is the active one
dbus_command="$dbus_property_command $dbus_object $firmware1 \
$dbus_object_priority_method $dbus_object_priority"
firmware1_priority=$(eval "$dbus_command" | cut -d' ' -f 2)
# If FW1 is the redundant one then get the redundant info from FW1
# and get the min FW version info from FW2 or vice-versa
firmware_primary=""
firmware_secondary=""
if [ 1 -eq "$firmware1_priority" ]; then
firmware_primary="$firmware2"
firmware_secondary="$firmware1"
else
firmware_primary="$firmware1"
firmware_secondary="$firmware2"
fi
dbus_command="$dbus_property_command $dbus_object $firmware_secondary \
$dbus_object_version_method $dbus_object_version"
REDUNDANT_FW_VERSION=$(eval "$dbus_command" | cut -d' ' -f 2-)
dbus_command="$dbus_property_command $dbus_object $firmware_primary \
$dbus_object_min_version_method $dbus_object_min_version"
MIN_FW_VERSION_LEVEL=$(eval "$dbus_command" | cut -d' ' -f 2-)
}