Created
April 1, 2013 22:07
-
-
Save richardjharris/5288171 to your computer and use it in GitHub Desktop.
Identify gaps in logs (DateTime, broken version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use autodie ':io'; | |
use DateTime::Format::Human::Duration; | |
use DateTime::Format::Strptime; | |
my $min_gap = shift || 3600; | |
my $span = DateTime::Format::Human::Duration->new; | |
my $dtparse = DateTime::Format::Strptime->new( | |
pattern => '%F %T %Z', | |
locale => 'en_GB', | |
time_zone => 'Europe/London', | |
); | |
my $prev; | |
while (my $line = <>) { | |
my $ts = parse_date($line); | |
if ($ts) { | |
if ($prev) { | |
my $delta = $ts - $prev; | |
if ($delta->in_units('seconds') >= $min_gap) { | |
print "--- GAP: " . $span->format_duration_between($prev, $ts) . " ---\n"; | |
} | |
} | |
$prev = $ts; | |
} | |
print $line; | |
} | |
sub parse_date { | |
my $line = shift; | |
$line =~ /^\[ (.*?) \]/x or return; | |
my $ts = $dtparse->parse_datetime($1); | |
return $ts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment