I need a perl script to read the following message from log file

WARNING: Child is hung for request

Perl script should have the following conditions

It should look log file located in specific location (example /mylog) and read "WARNING: Child is hung for request" (Without quotes) message continuously in 50 lines if this exceeds more than 50 lines send an email to example@example.com and write to a file (name error.log) in specific location (/mylogs/error.log)

This script should run in linux

One more important thing is logfile is very large so from this link http://oreilly.com/catalog/cookbook/chapter/ch08.html

We have to use this in our script

This is a beautiful example of a solution that may not be obvious. We read every line in the file but don't have to store them all in memory. This is great for large files. Each line has a 1 in N (where N is the number of lines read so far) chance of being selected.

Here's a replacement for fortune using this algorithm:

$/ = "%%\n";
$data = '/usr/share/games/fortunes';
srand;
rand($.) < 1 && ($adage = $_) while <>;
print $adage;

I tried the following script but it is not working as i expected

#!/usr/bin/perl

use warnings;
use strict;
use Net::SMTP;

## Declare variables ##

my $logFile="/xxxx/x.log";
my $outputFile="/xxxx/output.log";
my $Subject="xxxx Error";

## Open input and output files ##

open(LOG,"$logFile") or die "Unable to open logfile:$!\n";
open(OUT,">$outputFile") or die "Unable to open output file:$!\n";

## Call functions ##
&logFilter();
#&sendMail();

## Function to send out mail ##
sub sendMail
{

$msg = new MIME::Lite;

$msg->build(

      Type =>'text',
      Path =>"cat $outputFile |",
      ReadNow => 1

);

      $msg->add(From =>"<email>");
      $msg->add(To =>"$ARGV[0];");
      $msg->add(Subject => "$Subject");
      $msg->send('smtp','localhost',Debig=>1);

}

## Function to filter logs ##
sub logFilter

   {

while(<LOG>)

   {

             if(($_  =~ /WARNING: Child is hung for request/) )

    {

             next;

     }
    print OUT;

}
close(LOG);
close(OUT);
}

Thanks for your help

Edited 6 Years Ago by Nick Evan: Snipped personal info

It should look log file located in specific location (example /mylog) and read "WARNING: Child is hung for request" (Without quotes) message continuously in 50 lines if this exceeds more than 50 lines send an email

while(<LOG>)
	{
	# increase the count when the string found otherwise reset the count value
	($_  =~ /WARNING: Child is hung for request/) ?	($count++ ): ($count =0);

	#Call the function when count greater than 50
	&sendMail if ($count > 50);
	}
This article has been dead for over six months. Start a new discussion instead.