Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

A script where we need to come up with a way for the disk space monitoring to be smarter. On smaller file systems, we still want to alert at 90% full, but on larger file sytems, we should only alert when there is only a few GB free.

Can someone please shed some light on this and do the needful?

share|improve this question
add comment

2 Answers

There is multiple options, but you need modify below script as per your requirement.

#!/usr/bin/awk -f
BEGIN{
ADMIN="root@localhost"
threshold=20
"date" | getline date
"uname -n" | getline hostname

    while("LC_ALL=C df -Ph" | getline){
        used=$5
            if($1 != "Filesystem" && int(used) >= threshold){
            print "Running out of space: "$1,used" used on "hostname" as on: "date
            print "mail -s \"Alert: Almost out of disk space: " $1,used" used\" "ADMIN" >/dev/null" | "sh"
            close("sh");
            }
    }
}

You can refer below links http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html

http://mmonit.com/monit/

http://www.linuxjournal.com/content/tech-tip-send-email-alert-when-your-disk-space-gets-low

share|improve this answer
2  
Parsing the lines of df will break as soon as the mount reaches a certain amount of characters that df will print it on multiple lines. –  gertvdijk Dec 24 '12 at 8:23
1  
@gertvdijk No, it's ok with df -P, it's designed to be easily parsable (except when device names can contain whitespace). –  Gilles Dec 24 '12 at 17:35
    
@Gilles Good to know that, thanks! –  gertvdijk Dec 24 '12 at 18:13
add comment

You could (ab)use the Nagios plugin check_disk for this. The advantage is that this piece of software is lightweight, well-tested and probably available by your package management already (Debian/Ubuntu package nagios-plugins-basic). Example usage:

check_disk -w 10% -c 3 -u GB -p / -p /mnt

In this example, it checks two mointpoints / and /mnt. If it exits with a exit status of 2, your disk has less than 3GB available, if it exits with status 1, it has less than 10% available and if it exits with a status of 0, then neither of the values is reached. Checking for exit status in Bash is trivial, i.e. $?.

share|improve this answer
    
Thanks for your advice.yes we can do it with check_disk nagios plugin. But I would like to write it in shell scripting. –  user29218 Dec 24 '12 at 9:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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