CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Perl Script : Display logfile entries between 6pm to 6am following day

 



skm27273
New User

Sep 30, 2013, 3:46 AM

Post #1 of 7 (10169 views)
Perl Script : Display logfile entries between 6pm to 6am following day Can't Post

Hi Team

Iam new to Perl scriptig space,Can any one please help me with a perl script for the below situation

we have a logfile with below form of entries

**FAILED PROD** - BUGS - 08/26/13 @11:12 - BUGS~23AUG13~23AUG13~23AUG13~26AUG13~1109

**FAILED PROD** - cockroach - 08/29/13 @11:52 -cockroach~29AUG13~29AUG13~29AUG13~29AUG13~1152

we need to capture only those logfile entries that come between 6pm to 6am following day.

Thankyou

kum


BillKSmith
Veteran

Sep 30, 2013, 4:06 PM

Post #2 of 7 (10159 views)
Re: [skm27273] Perl Script : Display logfile entries between 6pm to 6am following day [In reply to] Can't Post

You have not give us nearly enough requirements. The following code would work if all of the following assumptions are true.

  • You want all nighttime data (not just the previous night) in the log file,
  • Time is logged in standard 24-hour format

  • The hour field is always preceeded by an '@'and followed by a ':'

  • The blank line comes after every entry (including last).

  • The blank lines consist only of a single newline

  • You can use hard-coded filenames

  • You do not try to log another entry while this script is active.



  • Code
    use strict; 
    use warnings;
    my $AT_NIGHT = qr/(?<=)@(18|19|20|21|22|23|00|01|02|03|04|05)(?=:)/xms;
    open my $IN_LOG , '<', 'myapp.log' or die "cannot open myapp.log: $!";
    local $/ = q();
    open my $NIGHT_LOG, '>', 'app_night.log' or die "cannot open app_night.log:\!";
    while (my $entry = <$IN_LOG>) {
    if ($entry =~ $AT_NIGHT) {
    print {$NIGHT_LOG} $entry ;
    }
    }
    close $NIGHT_LOG;
    close $IN_LOG;

    Good Luck,
    Bill


    skm27273
    New User

    Oct 1, 2013, 7:37 AM

    Post #3 of 7 (10151 views)
    Re: [BillKSmith] Perl Script : Display logfile entries between 6pm to 6am following day [In reply to] Can't Post

    Hi Bill



    Thank you for your valueable response, you have covered all the assumptions, However could please incorporate the below two requirements inthe script,

    1. The script should match the word FAILURE that comes between 6pm till 6am following day

    2. The script should look for the latest entries in the file (i.e. It should read the file from where it has left in the past run.)



    Thanks a million fo all your efforts and support.


    skm27273
    New User

    Oct 1, 2013, 7:40 AM

    Post #4 of 7 (10149 views)
    Re: [BillKSmith] Perl Script : Display logfile entries between 6pm to 6am following day [In reply to] Can't Post

    There is no blank lines between each logfile entry


    BillKSmith
    Veteran

    Oct 1, 2013, 11:12 AM

    Post #5 of 7 (10145 views)
    Re: [skm27273] Perl Script : Display logfile entries between 6pm to 6am following day [In reply to] Can't Post

    It is no surprise that most of my assumptions are wrong. I really was only trying to give you some idea of the information that is required. All I have to go on is two examples. Neither of them is from the time period of interest. Now you say that they are both wrong in at least one important detail (no blank line). Is an entry always exactly two lines with the time and date on the first. What do I do if I find an exception?

    You have added a new requirement to start where we left off the last time. There are several ways that may be done, but the choice depends on many things. Do I have access to the previous file? How big is it? Do I have access to a configuration file where I can keep information? What OS are you using? Does it make sense to remember the position in the log file or is the log sometimes cleared? How often does this program usually run. How many new entries does it usually find? How many day entries does it usually skip?

    Clearly we need to know a lot more about your data and about how you will use the program. My list of questions probbly is not complete. It is enough to get started.
    Good Luck,
    Bill


    skm27273
    New User

    Oct 1, 2013, 8:46 PM

    Post #6 of 7 (10140 views)
    Re: [BillKSmith] Perl Script : Display logfile entries between 6pm to 6am following day [In reply to] Can't Post

    Hi Bill



    Below is the details which provides more information

    This logfile contains variety of messages, but we need to capture only the entries that has text "FAILED PROD" that comes between 6PM to 6AM . The script will be scheduled to execute every 5 mins between 6PM to 6AM, therefore we need to have script capture only the latest entries (if anyexists) since last run.If any such entries exists then script should throw the message "Failed PROD ERROR @ Time : "Log file entry detail"" otherwise do nothing.



    Log File details : This log file is on /var/log/myapp/logfile.txt on RHEL5

    This is a single log file which gets appended whenever there is new entry/message.

    Each logfile entry is in one single line and there is no blank lines in between.


    Chris Charley
    User

    Oct 2, 2013, 12:56 PM

    Post #7 of 7 (10120 views)
    Re: [skm27273] Perl Script : Display logfile entries between 6pm to 6am following day [In reply to] Can't Post

    You might want to consider File::Tail.

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives