WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Simple disk activity monitor script  (Read 4109 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Simple disk activity monitor script
« on: November 19, 2013, 03:39:09 AM »
This script displays disk read and write activity as a percentage of time. Drives/partitions to be monitored can be selected
via the  DRIVES  variable. The rate at which the screen is updated can be set with the  UPDATERATE  variable. When
launched, the script launches and runs in a separate terminal which it sizes to match the number of drives/partitions
being monitored. The script relies on  aterm  and  coreutils.tcz  for the date command. Due to the way  /proc/diskstats
reports time spent reading and writing, numbers greater than 100% can be reported under heavy activity. This link
lists the fields in  /proc/diskstats:
https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats
And this link describes the fields starting with field 4:
https://www.kernel.org/doc/Documentation/block/stat.txt
Code: [Select]
#!/bin/sh
# Quick and dirty script for monitoring disk activity. Written by Richard A. Rost Nov 19, 2013
# Drives and/or partitions to monitor
DRIVES="sda2 sda3 sdb sda"
# Number of seconds between screen updates
UPDATERATE=2
# Width in characters of the terminal this runs in
WIDTH=40
# Used to calculate required height of terminal, don't touch
LINES=1

alias awk="busybox awk"

ORIGINALCOPY="/proc/diskstats"
TMPCOPY="/tmp/diskstat.tmp"
OLDSTATS="/tmp/diskstat.old"
NEWSTATS="/tmp/diskstat.new"


if [ "$1" != "run" ]
then
TIMESTAMP=`date +"%s%N" | grep N`
if [ -n "$TIMESTAMP" ]
then
echo "This script requires the  date  command provided by  coreutils.tcz"
exit
fi
for i in $DRIVES
do
d=`grep -w $i $ORIGINALCOPY`
if [ -z "$d" ]
then
echo "Error: Drive  $i  not found"
exit
fi
let LINES=LINES+1
done
aterm -geometry "$WIDTH"x"$LINES" -sb -sl 0 -fade 100  -e "$0" run &
exit
fi

rm -f $OLDSTATS
cp -f $ORIGINALCOPY $TMPCOPY
TTHEN=`date +"%s%N" | cut -b1-13`
for i in $DRIVES
do
grep -w $i $TMPCOPY >> $OLDSTATS
done
echo -ne '\033[?25l'
clear
while true
do
sleep $UPDATERATE
cp -f $ORIGINALCOPY $TMPCOPY
TNOW=`date +"%s%N" | cut -b1-13`
ELAPSEDTIME=`awk 'BEGIN { print '"$TNOW"' - '"$TTHEN"' }'`
rm -f $NEWSTATS
for i in $DRIVES
do
grep -w $i $TMPCOPY >> $NEWSTATS
done
echo -ne '\033[H'
for i in $DRIVES
do
RESULT=`printf "%s\t" $i`
OLDTIME=`grep -w $i $OLDSTATS | awk '{print $7}'`
NEWTIME=`grep -w $i $NEWSTATS | awk '{print $7}'`
RESULT=$RESULT`awk 'BEGIN { printf "Read=%3d%%\t", 100 * ('"$NEWTIME"' - '"$OLDTIME"') / '"$ELAPSEDTIME"' }'`
OLDTIME=`grep -w $i $OLDSTATS | awk '{print $11}'`
NEWTIME=`grep -w $i $NEWSTATS | awk '{print $11}'`
RESULT=$RESULT`awk 'BEGIN { printf "Write=%3d%%\n", 100 * ('"$NEWTIME"' - '"$OLDTIME"') / '"$ELAPSEDTIME"' }'`
echo -e "$RESULT"
done
echo -ne `date +'%T'`
mv -f $NEWSTATS $OLDSTATS
TTHEN=$TNOW
done
The script and a small screen shot are attached.

                [EDIT] Fixed typo.
« Last Edit: November 19, 2013, 09:42:53 AM by Rich »