Script to send an alert mail once the disk space is above 90% sends disk full even when the disk isn't full, how to resolve this issue?

Joined
Apr 16, 2023
Messages
149
Reaction score
16
Credits
1,460
Code:
    #!/bin/bash
   
    df -m > myfile
    server_ip_address=$(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1)
   
    if awk '$2 > 10000 && $5 > 90' myfile ; then
        echo "Disk Full in $server_ip_address"
    else
        echo "Nothing wrong with the server"
    fi

When I execute the script, always get disk full as output?
Output of df -m looks like this:
Code:
    Filesystem              1M-blocks   Used Available Use% Mounted on
    /dev/mapper/centos-root    949305 512827    436479  55% /
    devtmpfs                     3811      0      3811   0% /dev
    tmpfs                        3823      0      3823   0% /dev/abc
    tmpfs                        3823     18      3806   1% /run
    tmpfs                        3823      0      3823   0% /sys/fs/cgroup
    /dev/sda2                    1014    175       840  18% /boot
    /dev/sda1                    1022     12      1011   2% /boot/efi
    tmpfs                         765      0       765   0% /run/user/2000

The outut of df -m differs from server to server. I am thus comparing if Size>10GB and Use%>90%. As some disk with size<10GB may not be important to be under 90% disk usage.


Update 1:

GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

OS is centos 7

Problem with the current code:

I need to compare from the second row to last row columns, but my current code is comparing the first rows columns, that's why I suppose it's not working. How to resolve the issue?
 
Last edited:


I use this code to check for the list of disks that have >90% use:
Code:
df -m | awk '$1 ~ "/dev/.*" { use=substr($5, 1, length($5)-1); if(int(use)>90) print $1 "(" use "%)"; }'

Only disks from /dev/... are included (not tmpfs, etc.) but can be modified for any other disk name pattern. You can easily modify it for your case.
Check also if col 5 is the percentage field because in my Ubuntu it is field 6, so $5 needs to be modified also to match the usage field number. Print statement can print any format message, only the disk name, the usage, both or something else.

EDIT: For your script first the header line is used also in the processing and gives you false information, you use also "Use%" to compare it with 90. Another problem is that $5 is a string, 90 is an integer, so you compare string with integer. Use int($5) > 90 and this will eliminate the need of skipping the header because int("Use%")=0 and will not trigger the verification. Also not sure why you need 1M-blocks when the percentage use is enough.
 
Last edited:
In Mint Cinnamon I just type..."disks" in the search box hit Enter and select my SSD or this command...
Code:
 inxi -po

m1201.gif
 
It's better to use monitoring software for this, such as Nagios.
 
I need to compare from the second row to last row columns, but my current code is comparing the first rows columns, that's why I suppose it's not working. How to resolve the issue?

The condition in the "if" statement leads to displaying "disk full" even when the storage hasn't reached 90% yet. Instead of considering only the partition with over 90%, it considers all lines, and that's incorrect. I've slightly modified your script. Please test it.

Code:
#!/bin/bash

df -m > myfile

partitions=$(awk '$5+0 > 90 {gsub(/%/, "", $5); print $1}' myfile)

server_ip_address=$(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1)

if [ -n "$partitions" ]; then
    for partition in $partitions; do
        echo "Disk Full on $server_ip_address"
    done
else
    echo "Nothing wrong with the server"
fi
 

Staff online

Members online


Latest posts

Top