Perl script needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script needed
# 1  
Old 04-20-2011
Perl script needed

For the following command I need a perl script equivalent with a couple of more things -
Code:
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:
Code:
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).

A need a Perl script rather than a one liner please.

Thanks,
Jack.

Last edited by Yogesh Sawant; 04-20-2011 at 03:26 AM.. Reason: added code tags
# 2  
Old 04-20-2011
Could this help you ?

Code:
#!/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(<FH>) {
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);

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 04-20-2011
Thanks Praveen, 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

Regards,
Jack.
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
# 4  
Old 04-20-2011
I also a beginner in perl. Hope this helps. The same could've been done easily in shell.
In the below code, I am still using the system call to send mail. There are perl modules for the mailing.

Code:
#!/bin/perl

my @appname,@date;
my $i=0, $j=0;
my $temp;
open FPR, file or die 'Error : Could not open the file';

while(<FPR>){
  chomp;
  if(/Appname/){
    ($temp, @appname[$i++])=split(/:/);
    @appname[$i-1] =~ s/^ //;
  }
  if(/Date/){
    ($temp, @date[$j++])=split(/Time/);
    @date[$j-1] =~ s/^://;
    @date[$j-1] =~ s/^ //;
  }
}
close(FPR);

open FPW, ">/tmp/mail" or die 'Error : Could not open the file';
for($j=0;$j<$i;$j++){
print FPW "@date[$j] : @appname[$j]\n";
}
close(FPW);

system("cat /tmp/mail | mail -s \"Subject Goes here\" ahamed\@ahamed.com");

unlink "/tmp/mail";

regards,
Ahamed

---------- Post updated at 11:45 PM ---------- Previous update was at 10:56 PM ----------

This will append the alert body also after the TIME:APPNAME.
Tweak it as per your needs


Code:
#!/bin/perl

my @appname,@date,@alerbody,@datalen;
my $i=0, $j=0, $k=0, $l=0, $m=0;
my $temp;
open FPR, file or die 'Error : Could not open the file';
while(<FPR>){
  if(/Alert/){
    $l=$l+1;
  }
  @alertbody[$k++]=$_;
  @datalen[$l]=$k;
  chomp;
  if(/Appname/){
    ($temp, @appname[$l])=split(/:/);
    @appname[$l] =~ s/^ //;
  }
  if(/Date/){
    ($temp, @date[$l])=split(/Time/);
    @date[$l] =~ s/^://;
    @date[$l] =~ s/^ //;
  }
}
close(FPR);
open FPW, ">/tmp/mail" or die 'Error : Could not open the file';
for($j=1;$j<=$l;$j++){
  print FPW "=======================START===============================\n";
  print FPW "@date[$j] : @appname[$j]\n";
  print FPW "===========================================================\n";
  for($i=$m;$i<@datalen[$j];$i++){
    print FPW @alertbody[$i];
  }
  $m=@datalen[$j]+1;
  print FPW "=======================END=================================\n\n";
}
close(FPW);
system("mail -s \"The Earth\" ahamed\@ahamed.com < /tmp/mail");
unlink "/tmp/mail";

regards,
Ahamed
# 5  
Old 04-20-2011
hi Praveen,Ahmed -

I am using Praveen's script -

Additionally I have added unlink to clean the files in the end.

One more thing I want to add the hostname to appname & date in the line and append the all the alerts to the mail in the end after all appname/date/hostname (as descrived earlier).

Code:
#!/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(<FH>) {
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);

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 04-20-2011 at 06:40 PM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed on Perl Script to Handle Log files that are rotated using logrotate

Hello all, I am working on a perl script which will read weblogic logfile and send the error messages to Zenoss Monitoring tool. At present the script works and it can able to send the error messages to Zenoss. The problem comes when the logrotate has been applied to the weblogic log file. At... (3 Replies)
Discussion started by: kar_333
3 Replies

2. Shell Programming and Scripting

Help needed in calling path file from Perl script.

Hi All, I need help in accessing a path file (path.xyz_pqr) not having a shell shebang and exporting paths in the following syntax, export DB_SCRIPT_PATH="/abc/aash/scripts/db_scripts/xyz_pqr" export SRC_FILES_DIR="/bcd/fdw/incoming/xyz_pqr" I need to use the path contained in the... (8 Replies)
Discussion started by: xtatic
8 Replies

3. Shell Programming and Scripting

Help needed with Perl Script

i have the following script: #!/usr/local/bin/perl use STUN::Client; use Data::Dumper; use strict; my $stun_client = STUN::Client->new; $stun_client->stun_server('10.59.29.14'); my $r = $stun_client->get; my $ip = $r->{ma_address}; print "IP: $ip\n\nResult (hash):... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

5. Shell Programming and Scripting

Shell or perl script needed

Hi! I have a curl call which gets me the following output: curl http://hostname1.qa.com:8888/getlist OUTPUT - { "token" = "201119041112"; "apps" = { "PaymentApp" = "http://hostname1.qa.com:2333/cgi-bin/Jboss/PaymentApp-EMEA.woa/paymentInfo"; "OrderConsumer" =... (4 Replies)
Discussion started by: jacki
4 Replies

6. Shell Programming and Scripting

Perl script help needed..

Hi, I am new to perl scripting. Can anybody help me in solving the below problem. I have file, which is called map_file. According to map_file's last column data, i need a output file, which has repeats as like the map_file's last column name. Thank you in advance Vasanth (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

7. Shell Programming and Scripting

Perl Script Help Needed

I need this script to be able to check both IPs that are given to it and exit with an OK... if one of those expected IPs is returned. The script is run like this: /bin/dns_checker.pl -s 69.34.55.66 -q htt.jababa.com -e 69.44.56.33,45.47.43.55 Right now, the script is failing, but when I... (4 Replies)
Discussion started by: SkySmart
4 Replies

8. Shell Programming and Scripting

Simple Script needed for Processing CSV file perl

I am new to perl, and need a script to pull a CSV file, strip out 2 leading columns, and 2 ending columns, and resave the file in a new location. The file is basic and has less than 10,000 records. Secondly, can I schedule perl scripts to run daily? Can someone provide the basic script to... (1 Reply)
Discussion started by: cobbjob
1 Replies

9. Shell Programming and Scripting

Assistance needed with perl script

Ok, theres a log file containing the below. Lets call the logfile log_fantastic: 2009/03/16 21:42:45 USER: tonnabo - MAC: 0014BF2D385A - STATUS_ID: 30 - STATE: ERROR 2009/03/16 21:42:45 USER: tonnabo - MAC: 001310AC120D - STATUS_ID: 15 - STATE: OK 2009/03/16 21:42:45 USER: tonnabo - MAC:... (5 Replies)
Discussion started by: SkySmart
5 Replies

10. Shell Programming and Scripting

Script Needed Urgent in shell or PERL!!!!!!!!!!!!!!!!!!!!

My I/p FIle #%npr # { { collectionBeginTime_Date 19Jul2007 granularityPeriod 900 NODEID BSC19 RLC_Raw_Blocksize_CS2 2048 START_DATE 19Jul07 measTimeStamp_Date 20Jul2007 COLLECTION_INTERVAL_MINS 15 RLC_User_Blocksize_CS2 1024 ... (1 Reply)
Discussion started by: aajan
1 Replies
Login or Register to Ask a Question