1753318 Members
6261 Online
108792 Solutions
New Discussion юеВ

Perl equivalent needed

 
SOLVED
Go to solution
Allanm
Super Advisor

Perl equivalent needed

For the following command I need a perl equivalent with a couple of more things -

cat /tmp/mail |grep Appname > /tmp/mail1;cat /tmp/mail >> /tmp/mail1; mail -s "mail subject here" allan@mail.com < /tmp/mail1; >/tmp/mail ; >/tmp/mail1

==================
cat /tmp/mail

***** Alert *****

Notification Type: PROBLEM

Appname: flle system
Host: localhost
Address: 127.0.0.1
State: CRITICAL

Date/Time: Tue Apr 19 13:01:03 PDT 2011

Additional Info:

(Return code of 127 is out of bounds - plugin may be missing)

Console : http://URL
***** Alert *****

Notification Type: PROBLEM

Appname: Appname1
Host: remotehost.domain.com
Address: x.x.x.x
State: CRITICAL

Date/Time: Tue Apr 19 13:27:13 PDT 2011

Additional Info:

Connection refused

Console http://URL


===========

Want to create a Perl script which does a cat on /tmp/mail and egrep for "Appname|Date" and gives an output like this:

Tue Apr 19 13:01:03 PDT 2011 : flle system
Tue Apr 19 13:27:13 PDT 2011 : Appname1

and then send this output to my email address and finally empty out the file(s).
11 REPLIES 11
Allanm
Super Advisor

Re: Perl equivalent needed

A script rather than a one liner please.

Thanks,
Allan.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl equivalent needed

Hi Allanm:

This will get the fields you want. You can redirect the output to a file or you can store it in a variable. While we could write the whole set of code in Perl, I'll leave that as an exercise for you :-)

# perl -nle 'chomp;$name=$1 if m{^Appname:\s*(.+)};print "$1 $name" if m{^Date/Time:\s*(.+)}' file
Tue Apr 19 13:01:03 PDT 2011 flle system
Tue Apr 19 13:27:13 PDT 2011 Appname1

Regards!

...JRF...
Allanm
Super Advisor

Re: Perl equivalent needed

Thanks JRF, forgot to mention , In addition, I need to append the original alerts to the bottom of the date:appname text -

Tue Apr 19 13:01:03 PDT 2011 : flle system
Tue Apr 19 13:27:13 PDT 2011 : Appname1

***** Alert *****

Notification Type: PROBLEM

Appname: flle system
Host: localhost
Address: 127.0.0.1
State: CRITICAL

Date/Time: Tue Apr 19 13:01:03 PDT 2011

Additional Info:

(Return code of 127 is out of bounds - plugin may be missing)

Console : http://URL

***** Alert *****

Notification Type: PROBLEM

Appname: Appname1
Host: remotehost.domain.com
Address: x.x.x.x
State: CRITICAL

Date/Time: Tue Apr 19 13:27:13 PDT 2011

Additional Info:

Connection refused

Console http://URL

It will take me time to come up with a script but I am working.

Regards,
Allan.
Hakki Aydin Ucar
Honored Contributor

Re: Perl equivalent needed

simplified version as alternative calling from a shell ;

#!/usr/bin/ksh
/path-to-perl/perl -ne 'print if /Appname/|/Date/' /tmp/mail >
/tmp/mail_problem
Allanm
Super Advisor

Re: Perl equivalent needed

Hi JRF,Others!

I have made the following script -

#!/usr/bin/perl

use strict;

my ($app,$cmd);
open(FH,"<","/tmp/mail") or die "Fail- $!\n";
open(FW,">","/tmp/mail_test") or die "Fail- $!\n";
while() {
chomp;
if(/Appname:(.*)/) { $app=$1; }
if (/Date\/Time:(.*)/) { printf FW "%s:%s\n",$1,$app; }
}

$cmd='mail -s "mail subject here" allan@mail.com < /tmp/mail_test 2>/tmp/error_mail';
system($cmd);
if ( $? ne 0 ) { die "Failure - while sending email. Please check file /tmp/error_mail \n"; }
close(FW);
close(FH);

Please review and let me know of any pitfall(any stale filhandle).

In addition to Date/Appname, I am struggling a bit with printing rest of the alert text after printing Date/Appname.. Can you help?

Thanks,
Allan.
Allanm
Super Advisor

Re: Perl equivalent needed

I have also added delete(un-link) to the bottom of script to clean up the file(s).

close(FW);
close(FH);

unlink "/tmp/mail" or warn "Could not delete email: $!";; ;
Allanm
Super Advisor

Re: Perl equivalent needed

Additionally can we also get host as well, so the alert should look like -

======
Appname1:remotehost.domain.com:Tue Apr 19 13:27:13 PDT 2011
...

***** Alert *****

Notification Type: PROBLEM

Appname: flle system
Host: localhost
Address: 127.0.0.1
State: CRITICAL

Date/Time: Tue Apr 19 13:01:03 PDT 2011

Additional Info:

(Return code of 127 is out of bounds - plugin may be missing)

Console : http://URL
***** Alert *****

Notification Type: PROBLEM

Appname: Appname1
Host: remotehost.domain.com
Address: x.x.x.x
State: CRITICAL

Date/Time: Tue Apr 19 13:27:13 PDT 2011

Additional Info:

Connection refused

Console http://URL

Thanks,
Allan.

Hein van den Heuvel
Honored Contributor

Re: Perl equivalent needed



>> Additionally can we also get host as well, so the alert should look like -

======
Appname1:remotehost.domain.com:Tue Apr 19 13:27:13 PDT 2011

This is where YOU try to really understand how the appname came to be, and replicate for the host.

(Untested!) solution below:

hth,
Hein

Instead of

> if(/Appname:(.*)/) { $app=$1; }
> if (/Date\/Time:(.*)/) { printf FW "%s:%s\n",$1,$app; }
}

try (saving 4 bytes on first line :^) :

$app = $1 if /Appname:(.*)/;
$host = $1 if /^Host:(.*)/;
if (/Date\/Time:(.*)/) { printf FW "%s:%s:%s\n",$app,$host,$1; }
}



Allanm
Super Advisor

Re: Perl equivalent needed

That worked! Thanks,

Last thing I need is to append all alerts to the bottom of this list.So something like this -

Tue Apr 19 13:01:03 PDT 2011 : flle system:Host
Tue Apr 19 13:27:13 PDT 2011 : Appname1:Host

***** Alert *****

Notification Type: PROBLEM

Appname: flle system
Host: localhost
Address: 127.0.0.1
State: CRITICAL

Date/Time: Tue Apr 19 13:01:03 PDT 2011

Additional Info:

(Return code of 127 is out of bounds - plugin may be missing)

Console : http://URL
***** Alert *****

Notification Type: PROBLEM

Appname: Appname1
Host: remotehost.domain.com
Address: x.x.x.x
State: CRITICAL

Date/Time: Tue Apr 19 13:27:13 PDT 2011

Additional Info:

Connection refused

Console http://URL

Regards,
Allan.