Menu
in ,

How to monitoring Linux server disk usage and firing email when low

df command

df command

To simply monitoring your Linux server disk usage, we'll need to collect a small piece of information, the disk usage percentage, then check the threshold, and so firing alert email when finding it as low.

Get the disk usage percentage:

when running df -h disk filesystem command with human-readable -h parameter, we will get outputs like below.

[root@li560-107 ~]
# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           1.9G  8.5M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda         75G   42G   30G  59% /
tmpfs           374M     0  374M   0% /run/user/0

Now to get the usage percentage of the /dev/sda disk which is: 59% here in this example, we can run

[root@li560-107 ~]# df -h | sed -n 6p | awk '{print $5}'
59%

OR running with sed 's/%//g' to skip % sign.

[root@li560-107 ~]# df / | grep / | awk '{ print $5}' | sed 's/%//g'
59

Write our simple bash script:

We can set our threshold value to 90% , and so write our simple bash script file with targeting email address email@example.com.
If you need help to setting up the Linux server main Email serveice you can check this article about basic Postfix settings .

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Hell, Server disk space alert' email@example.com << EOF
Lookout the /dev/sda partition currently Used: $CURRENT% of available space.
EOF
fi

Now we can run our simple monitoring bash script file daily by append @daily /path/to/file/diskmon.sh and gives the file +x permission.

Resources:

Leave a Reply

Exit mobile version