0

I wrote a simple bash script to check disk usage and to send an email alert for all partitions over 85%. I want the output to look like this:

"/dev/mapper/rootvg-root is at 87%. An email has been sent to your administrator."

However, the current script prints every partition and the percent used doesn't match the right partition. Code below with output:


#!/bin/bash
admin="[email protected]"
threshold="85"

util=`df -h | tail -n +2 | awk '{print $5}' | sed 's/.$//'`
part=`df -h | tail -n +2 | head -$1 | awk '{print $1}'`

for i in $util;
do

if [ $i -ge $threshold ]; then

echo "$part is currently at ${i}%. An email has been sent to your             administrator." 
mail -s "Critical Alert: $part is currently at ${i}%. Clear space immeadiately!" $admin

fi
done

Output:

/dev/mapper/rootvg-root
devtmpfs
tmpfs
/dev/mapper/rootvg-var
/dev/mapper/rootvg-u01
/dev/mapper/rootvg-home
/dev/mapper/rootvg-apps
/dev/mapper/rootvg-tmp is currently at 92%. An email has been sent to your administrator.

/dev/mapper/rootvg-tmp is not at 92%, /dev/mapper/rootvg-root is and the other partitions below 85% should not be showing.

1 Answer 1

1
#!/bin/bash
admin="[email protected]"
threshold="85"

df -l --output=source,pcent | while IFS=" %" read -r part i; do
    if [ $i -ge $threshold ] 2>/dev/null; then
        echo "$part is currently at ${i}%. An email has been sent to your             administrator." 
        mail -s "Critical Alert: $part is currently at ${i}%. Clear space immeadiately!" $admin
    fi
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.