I am sharing a perl script here, which is supposed to send a mail with the HDD/RAM status in the message body: Also, the HDD out will happen only if the %HDD use is above 50%.

  1. How can I include the declared variables (here, @hdd and @ram) in the message body?

  2. How can I make the program terminate without sending the mail if the HDD usage is below 50%..? Or simply, the mail program should work if and only if the HDD % is above 50%.

In the below script, the HDD/RAM checking part is working fine, as well as the mail sending part is also working fine. Except, instead of the HDD/RAM values, "@hdd" and "@ram" are displayed directly in the received mail.

@hdd = grep {$_ >50} (\`df -H`=~/(\S+%)/g);
@ram = \`free | grep Mem | awk '{print \$3/\$2 * 100.0}'`;
use MIME::Lite;
$to = 'admin@company.co.in';
$from = 'alerts@company.co.in';
$subject = 'HDD / RAM alerts';
$message = 'The % HDD usage is @hdd and the % RAM usage is @ram';
$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message

Pardon me if the question is a little bit broad. But being beginner, please be kind enough to help it.


Use double-quote " instead of single-quote ' in the $message. You have blocked expansion of variables/arrays.

$message = "The % HDD usage is @hdd and the % RAM usage is @ram";
  • Perfect...!! The mail program should work if and only if the HDD % is above 50% else it should terminate without sending any mail. How this can be done? Kindly help this one too. – kiran bbnl May 26 '15 at 8:45
  • @kiranbbnl That is a completely separate question and answer combo, I'd ask that as a separate question. – Thomas Ward May 26 '15 at 11:47
  • @ThomasW. Thanks, but I got the answer. It is just about a if-then-else loop. The program compares the value with the threshold value, then executes the mailing loop and else terminates. I will post this as a Qn-Ans combo. Thanks again. – kiran bbnl May 26 '15 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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