Skip to content

Instantly share code, notes, and snippets.

@mvidner
Created September 11, 2014 11:26
Show Gist options
  • Select an option

  • Save mvidner/2ca75b5119c6a78f2e7d to your computer and use it in GitHub Desktop.

Select an option

Save mvidner/2ca75b5119c6a78f2e7d to your computer and use it in GitHub Desktop.
#! /usr/bin/perl
# http://en.opensuse.org/User:Mvidner
=head1 NAME
y2logpids - grep a YaST log file for process id changes and measure the time differences
=head1 SYNOPSIS
y2logpids -h|--help
y2logpids [-m] [-d [<seconds>]] [log-files]
=head1 OPTIONS AND ARGUMENTS
=over
=item B<-m>, B<--maximum>
Show the maximum time difference between log lines, within a single
PID.
=item B<-d> [I<seconds>], B<--difference>[=I<seconds>]
Show where the time difference is larger than I<seconds> (60 if
unspecified), within a single PID
=back
=cut
use warnings;
use strict;
use Date::Parse;
use Getopt::Long;
use Pod::Usage;
my $help = 0;
my $maximum = 0;
my $difference = 0;
Getopt::Long::Configure ("bundling");
GetOptions ("maximum|m" => \$maximum,
"difference|d:60" => \$difference,
"help|h" => \$help,
) or pod2usage(2);
pod2usage(1) if $help;
my $last = "";
my $lpid = "";
my $pid = "";
my $last_time = undef;
my $time_diff_max = -1;
my $time_diff_max_at = "";
while (<>) {
if (m/^(....-..-.. ..:..:..) <.> [^(]*\((\d+)\)/) {
$pid = $2;
if ($pid ne $lpid) {
print $last, $_;
$last_time = undef;
}
elsif ($maximum || $difference) # find the time difference
{
my $text_time = $1;
my $unix_time = str2time ($text_time);
if (! defined $last_time) {
$last_time = $unix_time;
}
my $time_diff = $unix_time - $last_time;
if ($time_diff_max < $time_diff) {
$time_diff_max = $time_diff;
$time_diff_max_at = $_;
}
if ($difference && $time_diff >= $difference) {
print $last;
print "... $time_diff s ...\n";
print $_;
}
$last_time = $unix_time;
}
$last = $_;
$lpid = $pid;
}
}
print $last;
if ($maximum) {
print "Maximum time difference $time_diff_max seconds at\n";
print $time_diff_max_at;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment