LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automatically monitor free space (https://www.linuxquestions.org/questions/linux-newbie-8/automatically-monitor-free-space-742060/)

anon091 07-22-2009 04:23 PM

Automatically monitor free space
 
Is there a way to have the servers send me an email when any of the partitions drop below a certain amount of free disk space?

i92guboj 07-22-2009 04:44 PM

If you don't need real time, you can design a script that will parse the output of df, and check if the free space is lesser than a given number or percentage, and if affirmative mail you. Then you can put it as a cron job.

You could as well mount the remote drive via nfs, sshfs, samba or whatever fits you, and just monitor it locally with gkrellm, conky or something else.

fordeck 07-22-2009 05:31 PM

Here is one idea that may get going in the right direction. Create a script similar to the following listing:

#!/bin/bash

EMAIL=user@example.com
SUBJECT="WARNING: Disk space low"


df -h | sed 's/%//' | awk '{if ($5 > 19) print $0 }' | mail -s "$SUBJECT" $EMAIL



Change the 19 in the awk statement to be your threshold or upper limit. I used 19 to test an existing partition. Then if this works for you, just setup a cron job to run as often as you need.

Regards,

Fordeck

anon091 07-23-2009 08:33 AM

What's the 19 signify? percentage free space or MB or something?

ncsuapex 07-23-2009 08:50 AM

be forewarned that if you use a script on the machine to check for diskspace using a cron job that if the disk space fills up in between cron jobs and the partition is the / partition you will not get an alert. Ive had instances where the / filled up and the script apparently needs some tmp space to execute the script and if there is no space left the script will not run. If you have more than one server you can either run a cron job from another server, setting up ssh keys of course or you can use something like nagios to monitor disk space.

anon091 07-23-2009 09:25 AM

I think my / have a good amount of space where I won't have to worry about them filling up. I'm more concerned with the couple data partitions I have. but that's good to know though, I never thought of that.

fordeck 07-23-2009 06:06 PM

Here is an example of the output of the df -h command:

Code:

[user@fs2 ~]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda6              38G  8.0G  28G  23% /
/dev/sda1            190M  33M  148M  18% /boot
tmpfs                752M    0  752M  0% /dev/shm
/dev/sda7            4.8G  145M  4.4G  4% /tmp

Now if you look at the columns of information, the 5th column is the percentage used. What the script does is compare $5 or the 5th column to see if it greater than 19 in this case

$5 > 19

Where 19 is the percentage that I chose as my upper limit for the sake of testing. Because the /dev/sda6 which is mounted on / is at 23% it will trigger an email. You of course will have a number that you want to use for that upper limit rather than 19. The sed command simply removes the % symbol and $5 simply refers to the 5th column of data to see if it is greater than 19.

Code:

#!/bin/bash

EMAIL=user@example.com
SUBJECT="WARNING: Disk space low"


df -h | sed 's/%//' | awk '{if ($5 > 19) print $0 }' | mail -s "$SUBJECT" $EMAIL

Let me know if you have any other questions.

Regards,

Fordeck

anon091 07-24-2009 07:59 AM

OK, I can actually make sense of what that line does now, thanks!

anon091 07-24-2009 08:00 AM

Now I just gotta figure out what to do to setup email on the server haha.

ncsuapex 07-24-2009 08:59 AM

all you should need is sendmail and to make sure its running.

anon091 07-24-2009 09:11 AM

thanks!

fordeck 07-24-2009 11:37 AM

To see if sendmail is installed you could run the following command:

rpm -qa | grep sendmail

If it is installed you should see something similar to the this:

Code:

[user@fs2 ~]# rpm -qa | grep sendmail
sendmail-8.13.8-2.el5
sendmail-cf-8.13.8-2.el5

If it is installed then check to see if it is set to start up automatically by running the following command:

chkconfig --list | grep sendmail

This will show output similar to:

Code:

[user@fs2 ~]# chkconfig --list | grep sendmail
sendmail              0:off        1:off        2:on        3:on        4:on        5:on        6:off

The above output is from a CentOS 5.3 Final server that has sendmail running on it. If you are still unable to get the disk space script to send an email you may have to look at what your firewall could possibly be blocking. Post any other questions you might have.

Regards,

Fordeck

anon091 07-24-2009 03:48 PM

thanks, i'll check it out


All times are GMT -5. The time now is 09:19 PM.