Check disk space with Bash
Now I’m working mainly with Linux and MySQL I’ve had to learn how to accomplish basic tasks in entirely new ways. As a DBA I like to keep an eye on disk space. I wanted something like my Check disk space with Powershell script, but this only works with Windows, so naturally I turned to Bash. Here’s a very basic solution that will allow you to check the disk space on multiple Linux servers quickly.
Save the script below to your home directory called chk_dsk_space.sh and mark this as executable. Open a terminal and enter ./chk_dsk_space.sh. You will be prompted for the password for each server before it displays disk usage information.
#!/bin/bash
# Set computer names here to check
computers="user@server1 user@server2 user@server3"
# Work through each computer in the array
for c in $computers
do
echo "================================================="
echo "= $c"
echo "================================================="
command="ssh $c 'df -h'"
eval $command
done
Here’s some sample output.
rhys@linux-n0sm:~> ./chk_dsk_space.sh
=================================================
= rhys@server1
=================================================
Password:
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 6.7G 4.2G 2.2G 67% /
udev 1.7G 444K 1.7G 1% /dev
/dev/sda7 8.9G 417M 8.0G 5% /home
/dev/sda2 75G 71G 4.1G 95% /windows/C
/dev/sda3 55G 47G 7.6G 86% /windows/D
=================================================
= user@server2
=================================================
Password:
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 6.7G 4.2G 2.2G 67% /
udev 1.7G 444K 1.7G 1% /dev
/dev/sda7 8.9G 417M 8.0G 5% /home
/dev/sda2 75G 71G 4.1G 95% /windows/C
/dev/sda3 55G 47G 7.6G 86% /windows/D
=================================================
= user@server3
=================================================
Password:
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 6.7G 4.2G 2.2G 67% /
udev 1.7G 444K 1.7G 1% /dev
/dev/sda7 8.9G 417M 8.0G 5% /home
/dev/sda2 75G 71G 4.1G 95% /windows/C
/dev/sda3 55G 47G 7.6G 86% /windows/D
Not as elegant as my Powershell script but it’s functional and saves me a little time each day. I’m not finding Bash as easy to work with as Powershell but that’s probably more due to my lack of experience than anything else. There’s an Open Source implementation of Powershell I’ve been thinking of checking out, it doesn’t implement the Windows specific cmdlets, but it’s been good to look at another way of doing things with Bash.