Perl or Shell Script to read files

Hi,
 
Could anyone please help me with the code snippet for the below logic.

LOGIC:
 Need to grep a file from the path "/usr/qa/qaout.log' and open  and read  the file to grep for words like Error, Permission Denied, Could not send, No such file or directory.

If it encounters any one of these word it needs to send and  email with this log file as an attachment.

Thanks,
new_perl_userAsked:
Who is Participating?
 
Bryan ButlerCommented:
How about:
#!/usr/bin/perl -w
use strict;

sub send_me_email {
  use MIME::Lite;

  my $msg = MIME::Lite->new(
    From    => 'from@myhost.com',
    To      => 'to@yourhost.com',
    Cc      => 'others@other.com, other2@more.com',
    Subject => 'Log file attached...',
    Type    => 'multipart/mixed',
  );

  $msg->attach(
      Type     => 'TEXT',
      Data     => "Here's the Log file",
  );

  $msg->attach(
    Type     => 'image/gif',
    Path     => 'aaa000123.gif',
    Filename => 'logo.gif',
  );

  $msg->send;
}

open (TEXTFILE, '/usr/qa/qaout.log');
$_ = join('',<TEXTFILE>);
close (TEXTFILE)

while (<>) {
  send_me_email if /Error|Permission Denied|Could not send/;
}

Open in new window

0
 
Bryan ButlerCommented:
 $msg->attach(
    Type     => 'image/gif',
    Path     => 'aaa000123.gif',
    Filename => 'logo.gif',
  );

 should be:

$msg->attach(
    Type     => 'text',
    Path     => '/usr/qa',
    Filename => 'qaout.log',
);

0
 
TintinCommented:
Assuming you have mutt (there are plenty of alternatives)

#!/bin/bash
LOG=/usr/qa/qaout.log
egrep -qi "error|permission denied|could not send|no such file or directory" $LOG && mutt -a $LOG -s "Subject line" add@example.com
~

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.