3

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.

\#!/usr/bin/perl
@hdd = grep {$_ >50} (\`df -H`=~/(\S+%)/g);
@ram = \`free | grep Mem | awk '{print \$3/\$2 * 100.0}'`;
use MIME::Lite;
$to = '[email protected]';
$from = '[email protected]';
$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
                 );
 $msg->send;

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

1 Answer 1

5

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";
3
  • 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, 2015 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, 2015 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, 2015 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .