cachestats.sh improve wildcard handling
This commit is contained in:
parent
45a7b4d491
commit
3aba7e1e2b
@ -3,25 +3,30 @@
|
|||||||
# cachestats for dm-cache
|
# cachestats for dm-cache
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
# Copyright 2023 Forza <forza@tnonline.net>
|
# Copyright 2023-2024 Forza <forza@tnonline.net>
|
||||||
|
|
||||||
|
|
||||||
# cacheatats usage
|
# Help text
|
||||||
usage() {
|
usage() {
|
||||||
echo "Usage: $(basename "${0}") [DEVICE_NAME or PATH] [DEVICE_NAME or PATH] ..."
|
echo "Usage: $(basename "$0") [-v|--verbose] [DEVICE_NAME or PATH] [DEVICE_NAME or PATH] ..."
|
||||||
|
echo "Options:"
|
||||||
echo " -h, --help Display this help message"
|
echo " -h, --help Display this help message"
|
||||||
|
echo " -v, --verbose Display detailed information"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check for help option or no arguments
|
# Check for help option or no arguments
|
||||||
if [ "$#" -eq 0 ]; then
|
if [ "$#" -eq 0 ]; then
|
||||||
usage
|
usage
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if [ "${1:0:1}" == "-" ]; then
|
if [[ "${1:0:1}" == "-" ]]; then
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h|--help)
|
-h|--help)
|
||||||
usage
|
usage
|
||||||
exit 1
|
exit 0
|
||||||
|
;;
|
||||||
|
-v|--verbose)
|
||||||
|
VERBOSE=1
|
||||||
|
shift
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Invalid option: $1"
|
echo "Invalid option: $1"
|
||||||
@ -31,13 +36,15 @@ if [ "${1:0:1}" == "-" ]; then
|
|||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Declare used variables
|
# Declare variables
|
||||||
declare -A data # associative array
|
declare -A data
|
||||||
declare -a dmstatus # indexed array
|
declare -a dmstatus
|
||||||
declare -i sizess # integer variable
|
declare -a expanded_devices
|
||||||
|
declare -i sizess
|
||||||
declare status_output
|
declare status_output
|
||||||
declare device_name_or_path
|
declare device_name_or_path
|
||||||
declare device_name
|
declare device_name
|
||||||
|
declare separator_needed
|
||||||
|
|
||||||
# Function to convert bytes to IEC units
|
# Function to convert bytes to IEC units
|
||||||
to_iec() {
|
to_iec() {
|
||||||
@ -72,7 +79,7 @@ debug_output(){
|
|||||||
# <#core args> <core args>* <policy name> <#policy args> <policy args>*
|
# <#core args> <core args>* <policy name> <#policy args> <policy args>*
|
||||||
# <cache metadata mode>
|
# <cache metadata mode>
|
||||||
|
|
||||||
# Loop through throuh all fields to list and print their values
|
# Loop through through all fields to list and print their values
|
||||||
if (( DEBUG > 0 )) ; then
|
if (( DEBUG > 0 )) ; then
|
||||||
printf "\nDEBUG INFO\n"
|
printf "\nDEBUG INFO\n"
|
||||||
printf "========\n"
|
printf "========\n"
|
||||||
@ -95,8 +102,29 @@ debug_output(){
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Expand wildcard patterns for devices
|
||||||
|
for arg in "$@"; do
|
||||||
|
if [[ "$arg" == /* ]]; then
|
||||||
|
# Full path provided, use as-is
|
||||||
|
expanded_devices+=("$arg")
|
||||||
|
else
|
||||||
|
# Assume partial name; expand in /dev/mapper
|
||||||
|
# shellcheck disable=SC2231
|
||||||
|
for device in /dev/mapper/${arg}*; do
|
||||||
|
if [[ -e $device ]]; then
|
||||||
|
expanded_devices+=("$device")
|
||||||
|
else
|
||||||
|
echo "No matching devices for pattern: $arg" >&2
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Add separator if there is more than one device
|
||||||
|
separator_needed=$(( ${#expanded_devices[@]} > 1 ))
|
||||||
|
|
||||||
# Loop through all devices and print their info
|
# Loop through all devices and print their info
|
||||||
for device_name_or_path in "${@}"; do
|
for device_name_or_path in "${expanded_devices[@]}"; do
|
||||||
|
|
||||||
# Strip path part from device name
|
# Strip path part from device name
|
||||||
device_name=$(basename "${device_name_or_path}")
|
device_name=$(basename "${device_name_or_path}")
|
||||||
@ -148,23 +176,30 @@ for device_name_or_path in "${@}"; do
|
|||||||
data["status"]="${dmstatus[24]//-/OK}"
|
data["status"]="${dmstatus[24]//-/OK}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Print dm-cache data
|
||||||
|
if (( separator_needed )); then
|
||||||
|
printf "\n\n********************** DEVICE %s **********************\n" "${data[name]}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Print debug information
|
# Print debug information
|
||||||
debug_output
|
debug_output
|
||||||
|
|
||||||
# Print dm-cache data
|
printf "\nDEVICE\n========\n"
|
||||||
printf "\nDEVICE\n"
|
|
||||||
printf "========\n"
|
|
||||||
printf "%-*s%s\n" "26" "Device-mapper name: " "/dev/mapper/${data[name]}"
|
printf "%-*s%s\n" "26" "Device-mapper name: " "/dev/mapper/${data[name]}"
|
||||||
printf "%-*s%s\n" "26" "Origin size: " "$(to_iec $(( data[origin_length] - data[origin_start] )) )"
|
printf "%-*s%s\n" "26" "Origin size: " "$(to_iec $(( data[origin_length] - data[origin_start] )) )"
|
||||||
|
|
||||||
|
if [[ $VERBOSE -eq 1 ]]; then
|
||||||
printf "%-*s%s\n" "26" "Discards: " "${data[discard_passdown]}"
|
printf "%-*s%s\n" "26" "Discards: " "${data[discard_passdown]}"
|
||||||
|
fi
|
||||||
|
|
||||||
printf "\n"
|
printf "\n"
|
||||||
printf "CACHE\n"
|
printf "CACHE\n========\n"
|
||||||
printf "========\n"
|
|
||||||
printf "%-*s%s\n" "26" "Size / Usage: " "$(to_iec $(( data[total_cache_blocks] * data[cache_block_size] ))) / $(to_iec $(( data[used_cache_blocks] * data[cache_block_size] ))) ($(( 100 * data[used_cache_blocks] / data[total_cache_blocks] )) %)"
|
printf "%-*s%s\n" "26" "Size / Usage: " "$(to_iec $(( data[total_cache_blocks] * data[cache_block_size] ))) / $(to_iec $(( data[used_cache_blocks] * data[cache_block_size] ))) ($(( 100 * data[used_cache_blocks] / data[total_cache_blocks] )) %)"
|
||||||
printf "%-*s%s\n" "26" "Read Hit Rate: " "${data[read_hits]} / $(( data[read_misses] + data[read_hits] )) ($(( 100 * data[read_hits] / (data[read_hits] + data[read_misses] ) )) %)"
|
printf "%-*s%s\n" "26" "Read Hit Rate: " "${data[read_hits]} / $(( data[read_misses] + data[read_hits] )) ($(( 100 * data[read_hits] / (data[read_hits] + data[read_misses] ) )) %)"
|
||||||
printf "%-*s%s\n" "26" "Write Hit Rate: " "${data[write_hits]} / $(( data[write_misses] + data[write_hits] )) ($(( 100 * data[write_hits] / ( data[write_hits] + data[write_misses] ) )) %)"
|
printf "%-*s%s\n" "26" "Write Hit Rate: " "${data[write_hits]} / $(( data[write_misses] + data[write_hits] )) ($(( 100 * data[write_hits] / ( data[write_hits] + data[write_misses] ) )) %)"
|
||||||
printf "%-*s%s\n" "26" "Dirty: " "$(to_iec "${data[dirty_cache]}")"
|
printf "%-*s%s\n" "26" "Dirty: " "$(to_iec "${data[dirty_cache]}")"
|
||||||
|
|
||||||
|
if [[ $VERBOSE -eq 1 ]]; then
|
||||||
printf "%-*s%s\n" "26" "Block Size: " "$(to_iec "${data[cache_block_size]}")"
|
printf "%-*s%s\n" "26" "Block Size: " "$(to_iec "${data[cache_block_size]}")"
|
||||||
printf "%-*s%s\n" "26" "Promotions / Demotions: " "${data[promotions]} / ${data[demotions]}"
|
printf "%-*s%s\n" "26" "Promotions / Demotions: " "${data[promotions]} / ${data[demotions]}"
|
||||||
printf "%-*s%s\n" "26" "Migration Threshold: " "$(to_iec "${data[migration_threshold]}")"
|
printf "%-*s%s\n" "26" "Migration Threshold: " "$(to_iec "${data[migration_threshold]}")"
|
||||||
@ -177,7 +212,8 @@ for device_name_or_path in "${@}"; do
|
|||||||
printf "METADATA\n"
|
printf "METADATA\n"
|
||||||
printf "========\n"
|
printf "========\n"
|
||||||
printf "%-*s%s\n" "26" "Size / Usage: " "$(to_iec $(( data[total_metadata_blocks] * data[metadata_block_size] ))) / $(to_iec $(( data[used_metadata_blocks] * data[metadata_block_size] ))) ($(( 100 * data[used_metadata_blocks] / data[total_metadata_blocks] )) %)"
|
printf "%-*s%s\n" "26" "Size / Usage: " "$(to_iec $(( data[total_metadata_blocks] * data[metadata_block_size] ))) / $(to_iec $(( data[used_metadata_blocks] * data[metadata_block_size] ))) ($(( 100 * data[used_metadata_blocks] / data[total_metadata_blocks] )) %)"
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
echo "Device not found or no valid status output."
|
echo "Device ${device_name_or_path} not found or no valid status output."
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
Loading…
Reference in New Issue
Block a user