Skip to content

Instantly share code, notes, and snippets.

@agners
Created August 18, 2023 13:35
Show Gist options
  • Save agners/e8902e4a641c55c3ff2f0351097f0336 to your computer and use it in GitHub Desktop.
Save agners/e8902e4a641c55c3ff2f0351097f0336 to your computer and use it in GitHub Desktop.
Simple IO monitoring script usful on Home Assistant OS
#!/bin/sh
#
# IO monitoring for Linux
#
# Taken from armbianmonitor
# https://github.com/armbian/build/blob/master/packages/bsp/common/usr/bin/armbianmonitor
# Adjusted to rely on diskstats only.
#
MonitorIO() {
LastWrites=$(awk -F" " "/ $1 / {print \$8}" </proc/diskstats)
LastWriteMerges=$(awk -F" " "/ $1 / {print \$9}" </proc/diskstats)
LastWriteSectors=$(awk -F" " "/ $1 / {print \$10}" </proc/diskstats)
FirstTimeWrites=${LastWrites}
FirstTimeWriteMerges=${LastWriteMerges}
FirstTimeSectors=${LastWriteSectors}
TimeNow=$(date "+%s")
LastTimeChecked=${TimeNow}
FirstTimeChecked=${LastTimeChecked}
echo -e "$(LANG=C date)$(printf "%8s%8s%12s" "Count" "Merge" "Size") Start monitoring IOs"
while [ "$((${TimeNow} - ${FirstTimeChecked}))" -lt 300 ]; do
TimeNow=$(date "+%s")
CurrentWrites=$(awk -F" " "/ $1 / {print \$8}" </proc/diskstats)
if [ ${CurrentWrites} -gt ${LastWrites} ]; then
CurrentWriteMerges=$(awk -F" " "/ $1 / {print \$9}" </proc/diskstats)
CurrentWriteSectors=$(awk -F" " "/ $1 / {print \$10}" </proc/diskstats)
TotalWrites=$((${CurrentWrites} - ${LastWrites}))
TotalWriteMerges=$((${CurrentWriteMerges} - ${LastWriteMerges}))
BytesWritten=$(((${CurrentWriteSectors} - ${LastWriteSectors}) / 2))
echo -e "$(LANG=C date)$(printf "%8s%8s%12s" ${TotalWrites} ${TotalWriteMerges} ${BytesWritten}) kbytes written after $((${TimeNow} - ${LastTimeChecked})) sec"
LastTimeChecked=${TimeNow}
LastWrites=${CurrentWrites}
LastWriteMerges=${CurrentWriteMerges}
LastWriteSectors=${CurrentWriteSectors}
fi
sleep 1
done
TotalWrites=$((${CurrentWrites} - ${FirstTimeWrites}))
TotalWriteMerges=$((${CurrentWriteMerges} - ${FirstTimeWriteMerges}))
BytesWritten=$(((${CurrentWriteSectors} - ${FirstTimeSectors}) / 2))
echo -e "$(LANG=C date)$(printf "%8s%8s%12s" "Count" "Merge" "Size") End monitoring IOs"
echo -e "$(LANG=C date)$(printf "%8s%8s%12s" ${TotalWrites} ${TotalWriteMerges} ${BytesWritten}) kbytes written after $((${TimeNow} - ${FirstTimeChecked})) sec"
} # MonitorIO
MonitorIO "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment