-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfilter-log.pl
executable file
·72 lines (60 loc) · 1.79 KB
/
filter-log.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl
#
# Filter log entries between <fromdate> and <todate>
#
# USAGE: filter-log.pl <fromdate> <todate>
#
# The script reads from stdin and writes to stdout. The dates must
# have the format: Sat Feb 14 16:55:28 2015.
use strict;
use Time::Piece;
#use Data::Dumper qw(Dumper);
sub usage
{
print "USAGE: filter-log.pl <fromdate> <todate>\n";
exit;
}
# Get the complete argument string.
my $arguments = '';
foreach (@ARGV) {
$arguments .= /\s/ ? " \'" . $_ . "\'" : " " . $_;
}
# Read the two dates specified on the command line.
$arguments =~ /([a-zA-Z]{3} [a-zA-Z]{3} {1,2}[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}) ([a-zA-Z]{3} [a-zA-Z]{3} {1,2}[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}).*/;
my( $fromdate, $todate ) = ( $1, $2 );
if( $fromdate eq "" )
{
usage;
}
# Convert timestamps to UNIX time.
my $time_piece = Time::Piece->strptime( $fromdate, "%c" );
my $from_timestamp = $time_piece->epoch;
$time_piece = Time::Piece->strptime( $todate, "%c" );
my $to_timestamp = $time_piece->epoch;
foreach my $log_entry( <STDIN> )
{
chomp $log_entry;
my $log_entry_begin = substr $log_entry, 0, 2;
if( $log_entry_begin ne "E:" && $log_entry_begin ne "OK" )
{
my $log_timestamp;
my $measurements;
my @test = $log_entry =~ /([a-zA-Z]{3} [a-zA-Z]{3} {1,2}[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}) (.*)/;
if( $#test == 1 )
{
$log_timestamp = $test[ 0 ];
$measurements = $test[ 1 ];
if( $log_timestamp ne "" )
{
# Convert the text timestamp to UNIX time.
# $log_timestamp = str2time( $log_timestamp );
$time_piece = Time::Piece->strptime( $log_timestamp, "%c" );
$log_timestamp = $time_piece->epoch;
if( ( $from_timestamp <= $log_timestamp ) && ( $log_timestamp <= $to_timestamp ) )
{
print "$log_entry\n";
}
}
}
}
}