Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

print lines withing specific date range from mailoog file

by theravadamonk (Scribe)
on Mar 17, 2016 at 09:13 UTC ( [id://1158061]=perlquestion: print w/replies, xml ) Need Help??

theravadamonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a postfix logfile /var/log/maillog. I want to send a blocked mail digest to users on daily basis. ( Let's say @ 10.30 am everyday). I want to print all lines from 10.30 a.m from yesterday to 10.30 am today from this /var/log/maillog to anoter file.

Then I will grep the BLOCKED mails from this file and send a Blocked mail Digest report to users.

How can I achieve this?

I found similar urls.

Extracting Log File data for a given date range
http://stackoverflow.com/questions/21995141/perl-script-to-extract-data-from-log-file-between-two-date-ranges-not-necessary

mail maillog looks like this

Feb 1 03:10:39 mailgw postfix/smtpd[31168]: disconnect from host.doma +in.net[8.3.14.x] Feb 1 03:10:41 mailgw postfix/smtpd[31175]: connect from localhost.lo +caldomain[127.0.0.1] Feb 1 03:10:41 mailgw postfix/smtpd[31175]: 42B1A1620467: client=loca +lhost.localdomain[127.0.0.1]
and so on I am still trying to write it. Can you monks help me on this.

Replies are listed 'Best First'.
Re: print lines withing specific date range from maillog file
by Corion (Patriarch) on Mar 17, 2016 at 09:17 UTC

    Personally, I would use a premade solution like dategrep. But if you want to write this yourself, perlre is a good starting point. Show us the code you've already written and where it fails to do what you want. We will help you with that part.

    Also, please update your node to clearly separate data from prose. Use <code>...</code> tags around your code and your data. Thanks!

      Hi, This seems to work. Here's my date_range.ok.pl file.
      #!/usr/bin/perl use strict; use warnings; use Time::Piece; my $format = '%b %e %T'; my $start = Time::Piece->strptime('Feb 1 10:30:00', $format); my $end = Time::Piece->strptime('Feb 2 10:30:00', $format); open(FILE, "/var/log/maillog") or die "Couldn't open maillog: $!; abor +ting"; while (<FILE>) { my ($timestamp) = /(^\w+\s+\d+\s+\d\d:\d\d:\d\d)/; my $t = Time::Piece->strptime($timestamp, $format); print if $t >= $start && $t <= $end; } close(FILE);
      I hardcoded below 2 things.
      my $start = Time::Piece->strptime('Feb 1 10:30:00', $format); my $end = Time::Piece->strptime('Feb 2 10:30:00', $format);

      Now I want system to fill today's date and yesterday's date? instead of Feb 16 and Feb 17

      I want system to fill Mar 16 and Mar 17

      Time range (10.30 to 10.30) is OK. I can hardcode it.

      Any comment?

        In general, "yesterday" is just 24 hours before today, except for days where the daylight savings time changes. Time::Piece has documented methods for date/time arithmetic, and it even includes an example to add one day. But I'm quite certain that the example can be modified to subtract one day as well.

        What problems did you encounter when using the documented arithmetic methods with your objects?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1158061]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found