I created a .sh file to monitor 2 file paths and send the disksize to me. It runs and I don't get a mail. And the filesystems are >90%
#!/bin/bash
used=$(df -Ph | grep 'location1' | awk {'print $5'})
used1=$(df -Ph | grep '/location2' | awk {'print $5'})
max=80%
if [ ${used%?} -gt ${max%?} ]; then mail -s 'Disk space alert' [email protected];[email protected] << EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
if [ ${use1%?} -gt ${max%?} ]; then mail -s 'Disk space alert' [email protected]; [email protected] << EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);
EOF
fi
CodePudding user response:
Thanks All, I have been able to figure it out.
#!/bin/bash
used=$(df -Ph | grep 'location1' | awk '{print $5}' | sed 's/%//g' )
used1=$(df -Ph | grep 'location2' | awk '{print $5}' | sed 's/%//g' )
max=80%
if [ ${used%?} -gt ${max%?} ]; then
if [ ${use1%?} -gt ${max%?} ]; then
mail -s 'Disk space alert' [email protected] [email protected] << EOF
The Mount Point 'location2' on $(hostname) has used $used1 at $(date);
EOF
fi
fi
CodePudding user response:
You failed to include the EOF
marker so the rest of the code is hidden inside the here document. (The syntax coloring in your question should help you notice.)
As an aside, you want to avoid the useless grep
and fix your indentation. I'm also guessing you don't want the second if
to only trigger if the first one did.
#!/bin/bash
used=$(df -Ph | awk '/location1/ { print $5}')
used1=$(df -Ph | awk '/\/location2/ { print $5}')
max=80%
if [ ${used%?} -gt ${max%?} ]; then
mail -s 'Disk space alert' [email protected];[email protected] <<__EOF
The Mount Point "location1" on $(hostname) has used $used at $(date);
__EOF
fi
if [ ${use1%?} -gt ${max%?} ]; then
mail -s 'Disk space alert' [email protected]; [email protected] <<__EOF
The Mount Point "location2" on $(hostname) has used $used1 at $(date);
__EOF
fi
A somewhat more idiomatic solution with less code duplication would loop over the parameters.
#!/bin/bash
max=80
for mountpoint in location /location2; do
used=$(df -Ph "$mountpoint" | awk 'NR>1 { print $5}')
if [ ${used%?} -gt $max ]; then
mail -s 'Disk space alert' [email protected];[email protected] <<____EOF
The Mount Point "$mountpoint" on $(hostname) has used $used at $(date);
____EOF
fi
done