Skip to content

Instantly share code, notes, and snippets.

@richardjharris
Created April 1, 2013 22:07
Show Gist options
  • Save richardjharris/5288171 to your computer and use it in GitHub Desktop.
Save richardjharris/5288171 to your computer and use it in GitHub Desktop.
Identify gaps in logs (DateTime, broken version)
#!/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