By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,373 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,373 IT Pros & Developers. It's quick & easy.

Trouble with Perl script

P: 37
I am trying to modify a script that's supposed to extract data from a log file.

The script is as under ..
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. ####################################################################
  3. # genreport
  4. ####################################################################
  5. sub genreport {
  6.    my ($logfile, $reportfile) = @_;
  7.    my ($line, @message, @summary, $count, $match, $n);
  8.  
  9.    open(LOGFILE, "<$logfile")
  10.       or die "Can't open $logfile: $!";
  11.  
  12.    open (REPORTFILE, ">$reportfile")
  13.       or die "Can't open $reportfile: $!";
  14.  
  15.    $count = $match = 0;
  16.    while ($line = <LOGFILE>) {
  17.       chomp $line;
  18.       if (length $line) {
  19.          push @message, $line;
  20.       } else {
  21.          if ($message[0] =~ /^\[ERROR](.*)\administrator.$/) {
  22.             #foreach $n (0..$#message) {
  23.         #  if ($message[$n] =~ /^infrastructure:ID_UNHANDLED:.*/) {
  24.              $match = 1;
  25.         #   }
  26.            if (($message[$n] =~ /^nested exception is:.*/) && ($match == 1)) {
  27.            push @summary, $message[$n + 1];
  28.            }           
  29.        # }
  30.         if ($match == 1) {
  31.               foreach $line (@message) {
  32.                   printf REPORTFILE "%s\n", $line;
  33.            }
  34.                printf REPORTFILE "\n";
  35.                $count++;
  36.            $match = 0;
  37.             }
  38.          } 
  39.     else {
  40.             die "parse error: missing date";
  41.          }
  42.          @message = ();
  43.       }
  44.    }
  45.  
  46.    if ($count > 0) {
  47.       printf "There were %s un-handled server exceptions:\n\n", $count;
  48.       foreach $line (@summary) {
  49.          printf "   ***> %s\n", $line
  50.       }
  51.       printf "\n";
  52.    } else {
  53.       printf "There were no un-handled server exceptions for this reporting period.\n\n";
  54.    }
  55. }
  56.  
  57. ####################################################################
  58. # main
  59. ####################################################################
  60.  
  61. if (@ARGV != 2) {
  62.    die "usage: UAESummary.pl <logfile> <reportfile>";
  63. }
  64.  
  65. my ($logfile, $reportfile) = @ARGV;
  66.  
  67. genreport($logfile, $reportfile);
  68.  
  69. exit 0;
  70.  
  71. ############
  72.  
The log file is as under ...

[ERROR] [09 Sep 2008 13:58:59] [Trace] [WebContainer : 88] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
nested exception is:
infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
at curam.util.transaction.ResourcesJTA.00000(Resourc esJTA.java(Compiled Code))
at curam.util.transaction.TransactionInfo.commit(Tran sactionInfo.java(Compiled Code))
at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Compiled Code))
at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Inlined Compiled Code))
at curam.util.invoke.EJSRemoteStatelessEJBMethod_51fe 6ae7.invoke(EJSRemoteStatelessEJBMethod_51fe6ae7.j ava(Compiled Code))
at sun.reflect.GeneratedMethodAccessor120.invoke(Unkn own Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
at java.security.AccessController.doPrivileged1(Nativ e Method)
at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))


#####

When I try running it though, it returns ...

C:\PerlScripts>UAESummary.bat CuramApp.log Report.log
There were no un-handled server exceptions for this reporting period.

Even though there is clearly a string that matches the regular expression in the script, unless I am missing something.

Please help.
Sep 10 '08 #1
Share this Question
Share on Google+
6 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Well, if I am reading this code right, then your code is doing exactly what you have told it to do. On line 16 you do an if test on "length $line". I assume this is returning true and going into that part of the if. The regex's and the $count variable are all inside of the "else" part of the if statement. The if test would have to fail for your code to work.

I think you need to re-work this code. (assuming I have read it correctly).

Also, please use code tags around your code. I have added them here for you. Simply edit your original post to see how to use them.

Regards,

Jeff
Sep 10 '08 #2

P: 37
Thanks for the quick response, Jeff.

I removed that part from the code, as can be seen below...


Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3.  
  4. ####################################################################
  5. # genreport
  6. ####################################################################
  7.   sub genreport {
  8.    my ($logfile, $reportfile) = @_;
  9.    my ($line, @message, @summary, $count, $match, $n);
  10.  
  11.    open(LOGFILE, "<$logfile")
  12.       or die "Can't open $logfile: $!";
  13.  
  14.    open (REPORTFILE, ">$reportfile")
  15.       or die "Can't open $reportfile: $!";
  16.  
  17.    $count = $match = 0;
  18.    while ($line = <LOGFILE>) {
  19.       chomp $line;
  20.           if ($message[0] =~ /^\[ERROR](.*)\administrator.$/) {
  21.             #foreach $n (0..$#message) {
  22.         #  if ($message[$n] =~ /^infrastructure:ID_UNHANDLED:.*/) {
  23.              $match = 1;
  24.         #   }
  25.            if (($message[$n] =~ /^nested exception is:.*/) && ($match == 1)) {
  26.           push @summary, $message[$n + 1];
  27.            }           
  28.        # }
  29.         if ($match == 1) {
  30.              foreach $line (@message) {
  31.                   printf REPORTFILE "%s\n", $line;
  32.            }
  33.                printf REPORTFILE "\n";
  34.                $count++;
  35.            $match = 0;
  36.             }
  37.          } 
  38.     #else {
  39.     #        die "parse error: missing date";
  40.     #     }
  41.          @message = ();
  42.  
  43.    }
  44.  
  45.    if ($count > 0) {
  46.       printf "There were %s un-handled server exceptions:\n\n", $count;
  47.       foreach $line (@summary) {
  48.          printf "   ***> %s\n", $line
  49.       }
  50.       printf "\n";
  51.    } else {
  52.       printf "There were no un-handled server exceptions for this reporting period.\n\n";
  53.    }
  54. }
I am still getting the same message though ...

C:\PerlScripts>UAESummary.bat CuramApp.log Report.log
There were no un-handled server exceptions for this reporting period.


I am just wondering if the reg exp is right since the code doesn't seem to be adding to the count and is printing the stmt ..

"There were no un-handled server exceptions for this reporting period.\n\n"
Sep 10 '08 #3

eWish
Expert 100+
P: 971
Is that a copy of that actual data you are using that you listed in the first post?

--Kevin
Sep 10 '08 #4

P: 37
Yes it is. I made it work actually, but now I wanted to extract the part from the stack that says "nested exception" and was having trouble with that.

The code is as below ...

Expand|Select|Wrap|Line Numbers
  1.  while ($line = <LOGFILE>) {
  2.       chomp $line;
  3.   if (length $line) {
  4.          push @message, $line;
  5.           if ($message[0] =~ /^\[ERROR](.*)\infrastructure:ID_UNHANDLED:.*$/) {
  6.             foreach $n (0..$#message) {
  7.         #  if ($message[$n] =~ /^infrastructure:ID_UNHANDLED:.*/) {
  8.              $match = 1;
  9.         #   }
  10.            if (($message[$n] =~ /^nested exception is:(.*)/) && ($match == 1)) {
  11.           push @summary, $message[$n + 1];
  12.            }           
  13.         }
  14.         if ($match == 1) {
  15.              foreach $line (@message) {
  16.                   printf REPORTFILE "%s\n", $line;
  17.            }
  18.                printf REPORTFILE "\n";
  19.                $count++;
  20.            $match = 0;
  21.             }
  22.          } 
The report I get is as below ...

[ERROR] [09 Sep 2008 12:36:56] [Trace] [WebContainer : 78] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.

[ERROR] [09 Sep 2008 12:55:23] [Trace] [WebContainer : 83] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.

[ERROR] [09 Sep 2008 12:55:36] [Trace] [WebContainer : 80] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.

[ERROR] [09 Sep 2008 12:57:54] [Trace] [WebContainer : 77] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.


This is part of the input log(in addition to the 1st line that's captured above, I want the part below the "nested exception is:" string to be displayed too ...

[ERROR] [10 Sep 2008 10:13:17] [Trace] [WebContainer : 144] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
at curam.util.internal.HandleException.getRemoteExcep tion(HandleException.java(Compiled Code))
at curam.util.internal.HandleException.getRemoteExcep tion(HandleException.java(Compiled Code))
at curam.util.internal.CuramSessionBean.o00000(CuramS essionBean.java(Compiled Code))
at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Compiled Code))
at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Inlined Compiled Code))
at curam.util.invoke.EJSRemoteStatelessEJBMethod_51fe 6ae7.invoke(EJSRemoteStatelessEJBMethod_51fe6ae7.j ava(Compiled Code))
at sun.reflect.GeneratedMethodAccessor120.invoke(Unkn own Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
nested exception is:
infrastructure:RUN_ID_RECORD_NOT_FOUND: Record not found.
at curam.util.dataaccess.DatabaseCall.o00000(Database Call.java(Compiled Code))
at curam.util.dataaccess.DatabaseCall.o00000(Database Call.java(Compiled Code))
at curam.util.dataaccess.DatabaseCall.execute(Databas eCall.java(Compiled Code))
at curam.fem.entity.base.EMChecklist.read(EMChecklist .java(Compiled Code))
at curam.fem.facade.impl.EMChecklist.doNothing(EMChec klist.java(Compiled Code))
at sun.reflect.GeneratedMethodAccessor874.invoke(Unkn own Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Compiled Code))
at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Inlined Compiled Code))
at curam.util.invoke.EJSRemoteStatelessEJBMethod_51fe 6ae7.invoke(EJSRemoteStatelessEJBMethod_51fe6ae7.j ava(Compiled Code))
at sun.reflect.GeneratedMethodAccessor120.invoke(Unkn own Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
Sep 11 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
I am really not clear on what you are trying to parse out of the log file, but see if this helps:

Expand|Select|Wrap|Line Numbers
  1. while ($line = <DATA>) {
  2.    if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:.*$/) {
  3.       push @message, $line;
  4.       $match = 1;
  5.       next;
  6.    }
  7.    if ($match && ($line =~ /^nested exception is:/)) {
  8.        $line = <DATA>;
  9.        push @message, $line;
  10.        $match = 0;
  11.        next;
  12.    }
  13. }
  14. for (@message) {
  15.    print;
  16. }
  17. __DATA__
  18. [ERROR] [09 Sep 2008 13:58:59] [Trace] [WebContainer : 88] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  19. nested exception is:
  20. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
  21. at curam.util.transaction.ResourcesJTA.00000(Resourc esJTA.java(Compiled Code))
  22. at curam.util.transaction.TransactionInfo.commit(Tran sactionInfo.java(Compiled Code))
  23. at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Compiled Code))
  24. at curam.util.internal.CuramSessionBean.invoke(CuramS essionBean.java(Inlined Compiled Code))
  25. at curam.util.invoke.EJSRemoteStatelessEJBMethod_51fe 6ae7.invoke(EJSRemoteStatelessEJBMethod_51fe6ae7.j ava(Compiled Code))
  26. at sun.reflect.GeneratedMethodAccessor120.invoke(Unkn own Source)
  27. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  28. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  29. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  30. at java.security.AccessController.doPrivileged1(Nativ e Method)
  31. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  32. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  33. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  34.  
Sep 12 '08 #6

P: 37
Thanks, this helped.
Sep 17 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.