/
lwes-perl-journal-listener
executable file
·166 lines (124 loc) · 4.05 KB
/
lwes-perl-journal-listener
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use LWES::EventParser;
use LWES::Listener;
use POSIX qw(strftime);
my $help_opt = 0;
my $man_opt = 0;
my $verbose_opt = 0;
my $stats_opt = 0;
Getopt::Long::Configure ("pass_through");
GetOptions
(
'help' => \$help_opt,
'man' => \$man_opt,
'stats' => \$stats_opt,
'verbose' => \$verbose_opt,
) or pod2usage (2);
pod2usage (-exitval => -1, -verbose => 0) if $help_opt;
pod2usage (-exitval => -2, -verbose => 2) if $man_opt;
my $listener_or_code;
# if the first argument is a file, we'll assume it s a journal file, and
# use the PrintingListener
if (-f $ARGV[0])
{
$listener_or_code = "LWES::Listeners::EventPrintingListener";
}
else
{
$listener_or_code = shift @ARGV;
}
# determine the callback which will process each event
my $processEventFunc = getProcessEventFunc ($listener_or_code, \@ARGV);
foreach my $journal (@ARGV)
{
my $start = time();
my $num_events = 0;
my $num_success = 0;
my $num_error = 0;
if (-f $journal)
{
open FH, "zcat $journal |";
# read and parse header
my $header_bytes;
while ( read (FH, $header_bytes, 22) == 22)
{
my $header = bytesToHeader ($header_bytes);
# read and parse bytes
my $event_bytes;
my $n = read (FH, $event_bytes, $header->{'PayloadLength'});
if (defined ($n) && $n == $header->{'PayloadLength'})
{
my $event = bytesToEvent ($event_bytes);
# merge header into event
foreach my $h (keys %{$header})
{
unless (exists ($event->{$h}))
{
$event->{$h} = $header->{$h};
}
}
# call handler
my ($success, $errors) = $processEventFunc->($event);
$num_events++;
if (defined ($success))
{
$num_success += $success;
}
if (defined ($errors))
{
$num_error += $errors;
}
}
else
{
die "malformed or truncated journal";
}
}
close FH;
}
if ($stats_opt)
{
my $log_date = strftime ("%a %b %e %H:%M:%S %Y", localtime (time()));
my $end = time()-$start;
print "$log_date : $journal had $num_events events processed with $num_success successful actions and $num_error errors in $end seconds\n";
}
}
0;
__END__
=head1 NAME
lwes-perl-journal-listener - listens for events from a journal
=head1 SYNOPSIS
lwes-perl-journal-listener [options] [<listener> [<args>] | <code>] <journal(s)>
Options:
--stats Print information about each file processed
-help Brief help message
-man Full Documentation
<listener> is the name of a perl module which extends the base
listener LWES::Listener and provides an initialize and processEvent
method. The <args> are passed directly to the listener constructor.
code is perl code which is embedded in a function which takes one
argument, a reference to a perl hash which contains the contents
of the event called $event
=head1 OPTIONS
=over 8
=item B<--stats>
Print information about how many events were processed and how long it took
to process.
=item B<-help>
Print help information
=item B<-man>
Print more verbose help information
=back
=head1 DESCRIPTION
The lwes-perl-journal-listener is a tool for inspecting LWES events that
are in journal files created by the lwes-journaller.
There are several ways to use the tool. To just print out events as they
are seen it can be invoked as
% lwes-perl-journal-listener <journal>
Alternatively you can provide a LWES::Listener module as outlined in
the lwes-perl-event-listener man page.
=cut