send a mail alert if /var partition exceeds 95%

Forum rules
Before you post please read this

send a mail alert if /var partition exceeds 95%

Postby ccc on Mon Apr 21, 2014 5:49 am

Hi

I'm looking for a bash script to send a mail alert if /var partition exceeds 90%.
ccc
Level 2
Level 2
 
Posts: 98
Joined: Sat Jul 23, 2011 7:40 pm

Linux Mint is funded by ads and donations.
 

Re: send a mail alert if /var partition exceeds 95%

Postby michael-j-lacey on Fri Jun 13, 2014 9:26 am

I don't really do bash (sorry) but here's a Perl script that should give you a good start

Code: Select all
#!/usr/bin/perl

open(F,'df -k|') or die "df command didn't run\n";             # run the df -k command and process each line
while(<F>){
        next unless /\/var$/;                                             # because I'm only interested in the var partition
        chomp;                                                               # get rid of any trailing new-line character
        ($fs,$blocks,$used,$avail,$pc_used,$mp) = split;  # split output of df command into fields
        $pc_used =~ /%/; $pc_used =$`;                          # get rid of the % character
        print "$pc_used\n" if $pc_used > 90;                     # this line, and the line below, is only executed if the percentage used is greater than 90%
        system("") if $pc_used > 90;                                # replace the "" with a bash command to send some mail
}
close (F);

# below the __END_ line is example output from the df -k command on my system
#  this is included so that I can refer to it when writing the script
__END__
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1      717150616 4969392 675729028   1% /
/dev/sda2      717150616 4969392 675729028   1% /var
udev               10240       0     10240   0% /dev
tmpfs             372668     892    371776   1% /run
tmpfs               5120       0      5120   0% /run/lock
tmpfs            1516400    1056   1515344   1% /run/shm
none                   4       0         4   0% /sys/fs/cgroup
michael-j-lacey
Level 1
Level 1
 
Posts: 2
Joined: Fri Jun 13, 2014 7:37 am

Re: send a mail alert if /var partition exceeds 95%

Postby woodsman on Fri Jun 13, 2014 1:44 pm

Simple shell script snippet:
Code: Select all
if [ `df | grep /var$ | awk '{print $5}' | sed 's|%||'` -ge 95 ]; then
  echo "Oh oh"
fi


To receive a system mail, install bsd-mailx and exim4. Configure exim4 with the local server settings (basically just accept the defaults).

Code: Select all
if [ `df | grep /var$ | awk '{print $5}' | sed 's|%||'` -ge 95 ]; then
  echo "Oh oh, /var is getting full!" | mail -s "Partition Full Alert" root@localhost
fi


You can change root@localhost to your normal user account or you can configure exim4 to automatically forward root mails to an alternate localhost address.

To view the mail with a graphical mail client, configure the client to check system mails. Systems mails with mailx use mbox.

If you do not keep your mail client running all the time, configure the mail-notification daemon to alert you of an incoming system mail.

Without a graphical email client, open a terminal and type 'mail' to read the system mails. Press '1' to read the mail, 'd' to delete and 'q' to quit. The system mails are stored in /var/mail/{$USER}.

You can use conky to monitor drive partitions. I do this on my systems. I use the color orange to alert me when the storage space exceeds 90%. :)
woodsman
Level 4
Level 4
 
Posts: 205
Joined: Tue May 11, 2010 10:22 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: Mik3e and 1 guest