Hi
I'm looking for a bash script to send a mail alert if /var partition exceeds 90%.
send a mail alert if /var partition exceeds 95%
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
send a mail alert if /var partition exceeds 95%
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: send a mail alert if /var partition exceeds 95%
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
Re: send a mail alert if /var partition exceeds 95%
Simple shell script snippet:
To receive a system mail, install bsd-mailx and exim4. Configure exim4 with the local server settings (basically just accept the defaults).
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%.
Code: Select all
if [ `df | grep /var$ | awk '{print $5}' | sed 's|%||'` -ge 95 ]; then
echo "Oh oh"
fi
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
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%.