Home > bash, MySQL, RHEL, Slackware > Linux bash: check the MySQL database disk usage

Linux bash: check the MySQL database disk usage

#!/bin/sh
# get.database.sizes.sh
# by dragkh
# Wed, 02 Mar 2011 17:59:44 +0100
# it will work on linux boxes with working mysql 
ps axu \
| grep datadir \
| grep mysql \
| grep -v grep \
| grep var \
| sed 's/^.*--datadir=//; s/ .*$//'   \
| sort \
| uniq  \
| while read crap
do 
echo ""
sized=$(du -s $crap | awk '{print $1}')
avail=$(df | grep $(dirname $crap) | awk '{print $2}')
echo -e  "$crap\t$sized\t$avail" | awk '{printf ("%s:\tUsed: %6.2fG \tDISK: %6.2fG\t Usage: %6.2f%% \n",$1,$2/1024/1024,$3/1024/1024,($2/$3)*100)}'
find $crap/ -maxdepth  1 -type d ! -type l  ! -path "$crap/"   -printf '"%p"\n'  | xargs  --no-run-if-empty  du -s | sort -nk 1,9 | awk -vavail=$avail '{crapy="";for(i=2; i<=NF; i++) {crapy=crapy" "$i;};printf (" -- %s\tUsed: %6.2fG \tDISK:%6.2fG Usage:%6.2f%%\n",crapy,$1/1024/1024,avail/1024/1024,($1/avail)*100)}'
done

result looks like

root@xxxx:[Wed Mar 02 16:54:59]:
/var/lib/mysql/aaaa/datafiles:               Used:  26.43G   DISK:196.86G       Usage:  13.43% 
 --  /var/lib/mysql/aaaa/datafiles/A        Used:   0.00G   DISK:196.86G        Usage:  0.00%
 --  /var/lib/mysql/aaaa/datafiles/B        Used:   0.00G   DISK:196.86G        Usage:  0.00%
 --  /var/lib/mysql/aaaa/datafiles/mysql  Used:   0.00G   DISK:196.86G        Usage:  0.00%
 --  /var/lib/mysql/aaaa/datafiles/C        Used:   0.01G   DISK:196.86G        Usage:  0.01%
 --  /var/lib/mysql/aaaa/datafiles/D        Used:   8.95G   DISK:196.86G        Usage:  4.55%
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: