Sending an email if system disk space is low

Hello i have a working script that sends me an email if system disk space is above 98%.
I would like to add an extra command to the if condition. If disk space is above 98% it would also add an output of

cd /var/log && du -sBM * |sort -n 2>&1 |grep -v -e "0M" -e "1M"

command to the email.

I'm new at scripting and couldn't figure this out :confused:

My working script:

#!/usr/bin/perl

$mailfrom = "email\@mydomain";
$mailto = "email\@mydomain";
$above = 98;

use FileHandle;

@check=`df -h | grep -vE '^Filesystem|none|cdrom'`;

##### Checks for lines higher than $above
foreach (@check) {
    if (m/(\d+\%)/ and $1 >=$above) {
	cd /var/log && du -sBM * |sort -n 2>&1 |grep -v -e "0M" -e "1M";
        push @New,$_;
    }
}

##### If there is anything found, send mail
if (@New) {
    $mail = new FileHandle;
    $mail->open("| /usr/sbin/sendmail -t") || die "Cannot open: $!";
    $mail->print("From: $mailfrom\n");
    $mail->print("To: $mailto\n");
    $mail->print("Subject: diara.ee sda1 disk space low\n\n");
    $mail->print(@New, "\n");
    $mail->close();
}

Change the email part to below

if (@New) {
    my $data = qx(cd /var/log && du -sBM * |sort -n 2>&1 |grep -v -e "0M" -e "1M");
    $mail = new FileHandle;
    $mail->open("| /usr/sbin/sendmail -t") || die "Cannot open: $!";
    $mail->print("From: $mailfrom\n");
    $mail->print("To: $mailto\n");
    $mail->print("Subject: diara.ee sda1 disk space low\n\n");
    $mail->print(@New, "\n");
    $mail->print($data, "\n");
    $mail->close();
}
1 Like

And it works :slight_smile:
Thanks for the mastery.