-2
#!/bin/bash
threshold="1"
{
        >/tmp/output
        for fs in $(df -hk | awk '{print $6}' | sed '1 d'); do
                chk=$(df -hk ${fs} | sed '1 d' | awk '{print $5}' | awk -F\% '{print $1}')
                if [ "${chk}" -gt "${threshold}" ]; then
                        echo "$(hostname): Alert Fileystem ${fs} is above ${threshold}%." >>/tmp/output
                fi
        done
        cat /tmp/output| mailx -s "sub" abc@.com
}

Now if a fs crosses the threshold limit, we should get mail

closed as unclear what you're asking by Rui F Ribeiro, Christopher, Stephen Harris, Luciano Andress Martini, Toby Speight Feb 28 at 18:34

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    If the code works but you need improvement, you should go to Code Review Stackexchange. – Weijun Zhou Feb 27 at 7:17
  • I guess you don't want to send an email if /tmp/output is empty? – Freddy Feb 27 at 8:04
  • the code is working perfectly. I tested it, high space in the sense files with occupying more space in the filesystem and yes i dont want to send a mail if /tmp/output is empty – Sri Feb 27 at 8:44
  • You are not expecting a answer with how to write a script that shows the large files sorted in a filesystem. So why this title? – Luciano Andress Martini Feb 28 at 11:42
  • the question is now edited correctly can you review my question and clear the flag – Sri Mar 4 at 7:49
0

Just a suggestion how you could do it. See the comments in the script.

#!/bin/bash

# define constants on top so you can find and edit them easily
declare -ri threshold=1
declare -r mailsubject="[$(hostname)] FS Alert Exceed ${threshold}%"
declare -r mailbodystart="$(hostname) Filesystem Alert, threshold=${threshold}%\n"
declare -r mailto="abc@.com"

# use variable instead of output file
declare mailbody=""

for fs in $(df -hk | awk '{print $6}' | sed '1d'); do
        chk=$(df -hk ${fs} | sed '1d' | awk '{print $5}' | awk -F\% '{print $1}')
        if [ "$chk" -gt "$threshold" ]; then
                # append to mailbody
                mailbody="${mailbody}${chk}% ${fs}\n"
        fi
done

# send mail if mailbody is non-empty
if [ -n "$mailbody" ]; then
        # sort by filesystem size, largest first
        mailbody=$(echo -e "$mailbody" | sort -nr)
        echo -e "${mailbodystart}${mailbody}" | mailx -s "$mailsubject" "$mailto"
fi
  • I want to add Thanks and regards and under it my name in the mailbody, tried different combinations using /t and /n but didnt work, can you help me out – Sri Mar 25 at 9:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.