Last active
November 22, 2022 14:46
-
-
Save DaMatrix/8d80135edc6e69fa69e4c523ac6def6b to your computer and use it in GitHub Desktop.
bash: continuously updated iostat (like 'watch iostat') (inspired by https://jrs-s.net/2019/06/04/continuously-updated-iostat/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
shopt -s extglob | |
NEWLINE=$'\n' | |
ANSI_ERASE_ENTIRE_SCREEN=$'\e[2J' | |
ANSI_RESET_CURSOR_POSITION=$'\e[H' | |
ANSI_CLEAR="${ANSI_ERASE_ENTIRE_SCREEN}${ANSI_RESET_CURSOR_POSITION}" | |
die() { | |
tput rmcup | |
echo "die: ${1}" >&2 | |
exit 1 | |
} | |
#open a sub-terminal | |
tput smcup || die "couldn't enable secondary terminal buffer!" | |
trap 'tput rmcup' EXIT | |
#use default args if none are set | |
if [ $# -eq 0 ]; then | |
args=( -p device /dev/disk/by-path/!(*-part[0-9]*) "-hsx" ) | |
else | |
args=( "$@" ) | |
fi | |
S_COLORS=always iostat "${args[@]}" 1 | { | |
echo "${ANSI_CLEAR}waiting for data..." | |
#we use $REPLY rather than reading directly into a variable because it preserves whitespace | |
#read header line | |
read || die "couldn't read header!" | |
[ ! -z "$REPLY" ] || die "header, got '${REPLY}'" | |
header="$REPLY" | |
read || die "couldn't read trailing newline after header!" | |
[ -z "$REPLY" ] || die "expected a trailing newline after header, got '${REPLY}'" | |
#read a whole report and print it | |
while true; do | |
outbuf=() | |
#read an input block | |
while true; do | |
#read the first line - if it's empty, then we reached the end of the last block | |
read || die "couldn't read block start" | |
[ ! -z "$REPLY" ] || break | |
#keep printing the previous line and reading the next until we hit a newline, indicating | |
#the end of the current block | |
while [ ! -z "$REPLY" ]; do | |
outbuf+=( "$REPLY" ) | |
read || die "couldn't read next line in block" | |
done | |
#print final line of block (this is an empty string) | |
outbuf+=( "$REPLY" ) | |
done | |
#clear screen and print new buffer contents | |
printf '%s' "${ANSI_CLEAR}${header}${NEWLINE}" "${outbuf[@]/#/$NEWLINE}" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment