Skip to content

Instantly share code, notes, and snippets.

@starkers
Created May 13, 2016 15:13
Show Gist options
  • Save starkers/ecd40e20a786bc8d3480771af63dc8d9 to your computer and use it in GitHub Desktop.
Save starkers/ecd40e20a786bc8d3480771af63dc8d9 to your computer and use it in GitHub Desktop.
simple monitoring of a directorys size, no rocket science
#!/usr/bin/env bash
# DESC: simple monitoring of directory growth.. does a du and informs you the diff on a loop
# defaults to 300 second loop
# USAGE: this_script.sh "/some/directory/to/watch" is the directory to watch
# sample output:
#Fri May 13 13:12:06 BST 2016: thing grew by 132
#Fri May 13 13:17:07 BST 2016: thing grew by 83
#Fri May 13 13:37:07 BST 2016: thing shunk by 1575
#Fri May 13 13:42:07 BST 2016: thing grew by 596
#Fri May 13 13:47:07 BST 2016: thing grew by 536
#Fri May 13 13:52:07 BST 2016: thing grew by 368
#Fri May 13 13:57:07 BST 2016: thing grew by 3
#Fri May 13 14:02:07 BST 2016: thing grew by 7
#Fri May 13 14:07:07 BST 2016: thing grew by 7
#must be a dir
DEST="$1"
if [ ! -d "$1" ]; then
echo "err.. please specify a dir"
exit 1
fi
# Time between loops, as this is a dumb script the real time will include the time it takes "du" to stat also
TIME=300
get_size(){
if [ -d "$1" ]; then
du -sm "$1" | awk '{print $1}'
else
echo "sorry, $1 does not exist or is not a dir"
exit 1
fi
}
check_time(){
NOW="$(date)"
}
SIZE="$(get_size "$DEST")"
while true ; do
OLD_SIZE=$SIZE
check_time
SIZE="$(get_size "$DEST")"
DIFF="$(expr $SIZE - $OLD_SIZE)"
if [ $DIFF -gt 0 ] ; then
echo "$NOW: thing grew by $DIFF"
fi
if [ $DIFF -lt 0 ] ; then
#strip the - from "$DIFF"
DIFF="$(sed "s+-++g" <<<"$DIFF")"
echo "$NOW: thing shunk by $DIFF"
fi
# if [ $DIFF == 0 ] ; then
# echo no change
# fi
# echo "$NOW: $SIZE"
sleep $TIME
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment