Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: grabbing time from a syslog logfile

by socketdave (Curate)
on Sep 12, 2005 at 20:18 UTC ( #491361=note: print w/ replies, xml ) Need Help??


in reply to grabbing time from a syslog logfile

I would craft a regex that properly gathers the entire date from the beginning of your log entry, then use something like str2time from Date::Parse to get a unix timestamp. They're much easier to do math on, and this can take care of all sorts of gotchas for you, perhaps something similar to this, depending on your log format:

#!/usr/bin/perl use warnings; use strict; use Date::Parse; my $line = 'Sep 12 15:07:48 2005 -0500 hostname rest of log entry goes + here'; ( my $date ) = ( $line =~ /^(\w\w\w \d\d \d\d:\d\d:\d\d \d\d\d\d [-|\+ +]\d\d\d\d)/ ); print str2time( $date )."\n";


As for grabbing the last x number of lines from a file, you could open a pipe to a tail:

open (PIPE, "/path/to/tail -n 10 /path/to/logfile |");
Update: Better yet, try File::Tail.


Comment on Re: grabbing time from a syslog logfile
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://491361]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-12-27 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls