Skip to content

Instantly share code, notes, and snippets.

@rabin-io
Created January 7, 2024 16:47
Show Gist options
  • Save rabin-io/5ced5f1726feefdecbe3fb1b4375927b to your computer and use it in GitHub Desktop.
Save rabin-io/5ced5f1726feefdecbe3fb1b4375927b to your computer and use it in GitHub Desktop.
Monitor kubevirt live migration progress in the terminal
#!/usr/bin/env bash
# usage
# kubevirt-monitor-live-migration.sh my-namespace/my-vm
#
namespace_vm=${1?vm name}
vm=${namespace_vm##*/}
namespace=${namespace_vm%%/*}
bar_size=40
bar_char_done="#"
bar_char_todo="-"
bar_percentage_scale=2
#export KUBECONFIG=/tmp/kubeconfig
function show_progress {
current="$1"
total="$2"
# calculate the progress in percentage
percent=$(bc <<< "scale=$bar_percentage_scale; 100 * $current / $total" )
# The number of done and todo characters
done=$(bc <<< "scale=0; $bar_size * $percent / 100" )
todo=$(bc <<< "scale=0; $bar_size - $done" )
# build the done and todo sub-bars
done_sub_bar=$(printf "%${done}s" | tr " " "${bar_char_done}")
todo_sub_bar=$(printf "%${todo}s" | tr " " "${bar_char_todo}")
# output the bar
echo -ne "\rProgress : [${done_sub_bar}${todo_sub_bar}] ${percent}%"
if [[ ${total} -eq ${current} ]]; then
echo -e "\nDONE"
fi
}
lastDataProcessed=0
# Read events from stdin
#oc logs -f -n "${namespace}" -l "vm.kubevirt.io/name=${vm},!kubevirt.io/migrationJobUID" \
oc logs -f -n "${namespace}" -l "vm.kubevirt.io/name=${vm}" \
| while read -r event; do
# Reset values
TimeElapsed=0
DataProcessed=0
# DataRemaining=''
DataTotal=0
MemoryProcessed=0
MemoryRemaining=0
MemoryTotal=0
# MemoryBandwidth=''
# DirtyRate=''
# Iteration=''
# PostcopyRequests=''
# ConstantPages=''
# NormalPages=''
# NormalData=''
# ExpectedDowntime=''
# DiskMbps=''
message=$(echo "${event}" | jq -r .msg)
if [[ ${message} != "Migration info for"* ]] ; then
# echo noop
continue
fi
stats=$(echo "${message}" | sed -E 's/Migration info for [a-f0-9-]+: //' | sed -e 's/ /\n/g' -e 's/:/=/g')
eval "${stats}"
# Clean the data
DataProcessed="${DataProcessed%MiB}"
DataTotal=${DataTotal%MiB}
TimeElapsed=${TimeElapsed%ms}
MemoryProcessed=${MemoryProcessed%MiB}
MemoryRemaining=${MemoryRemaining%MiB}
MemoryTotal=${MemoryTotal%MiB}
diffDataProcessed=$((DataProcessed - lastDataProcessed))
diffTimeElapsed=$(bc <<< "scale=$bar_percentage_scale; (${TimeElapsed} - ${lastTimeElapsed}) / 1000")
lastDataProcessed=${DataProcessed%MiB}
lastTimeElapsed=${TimeElapsed}
mps=$(bc <<< "scale=$bar_percentage_scale; $diffDataProcessed/$diffTimeElapsed")
echo " rate $mps MiB/sec"
# # Draw the progress bar
# show_progress "${DataProcessed}" "${DataTotal}"
show_progress "$((MemoryTotal - MemoryRemaining))" "${MemoryTotal}"
done
# Add a new line after the progress bar is complete
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment