switching licenses
[lwes-perl.git] / lwes-perl-journal-listener
blob3bba38a6efe56df3fb48c8b57c77764152f40367
1 #!/usr/bin/env perl
3 use strict;
4 use warnings;
6 use Getopt::Long;
7 use Pod::Usage;
8 use LWES::EventParser;
9 use LWES::Listener;
10 use POSIX qw(strftime);
12 my $help_opt = 0;
13 my $man_opt = 0;
14 my $verbose_opt = 0;
15 my $stats_opt = 0;
17 Getopt::Long::Configure ("pass_through");
19 GetOptions
21 'help' => \$help_opt,
22 'man' => \$man_opt,
23 'stats' => \$stats_opt,
24 'verbose' => \$verbose_opt,
25 ) or pod2usage (2);
27 pod2usage (-exitval => -1, -verbose => 0) if $help_opt;
28 pod2usage (-exitval => -2, -verbose => 2) if $man_opt;
30 my $listener_or_code;
32 # if the first argument is a file, we'll assume it s a journal file, and
33 # use the PrintingListener
34 if (-f $ARGV[0])
36 $listener_or_code = "LWES::Listeners::EventPrintingListener";
38 else
40 $listener_or_code = shift @ARGV;
43 # determine the callback which will process each event
44 my $processEventFunc = getProcessEventFunc ($listener_or_code, \@ARGV);
46 foreach my $journal (@ARGV)
48 my $start = time();
49 my $num_events = 0;
50 my $num_success = 0;
51 my $num_error = 0;
54 if (-f $journal)
56 open FH, "zcat $journal |";
58 # read and parse header
59 my $header_bytes;
60 while ( read (FH, $header_bytes, 22) == 22)
62 my $header = bytesToHeader ($header_bytes);
64 # read and parse bytes
65 my $event_bytes;
66 my $n = read (FH, $event_bytes, $header->{'PayloadLength'});
67 if (defined ($n) && $n == $header->{'PayloadLength'})
69 my $event = bytesToEvent ($event_bytes);
71 # merge header into event
72 foreach my $h (keys %{$header})
74 unless (exists ($event->{$h}))
76 $event->{$h} = $header->{$h};
80 # call handler
81 my ($success, $errors) = $processEventFunc->($event);
82 $num_events++;
83 if (defined ($success))
85 $num_success += $success;
87 if (defined ($errors))
89 $num_error += $errors;
92 else
94 die "malformed or truncated journal";
98 close FH;
101 if ($stats_opt)
103 my $log_date = strftime ("%a %b %e %H:%M:%S %Y", localtime (time()));
104 my $end = time()-$start;
105 print "$log_date : $journal had $num_events events processed with $num_success successful actions and $num_error errors in $end seconds\n";
111 __END__
113 =head1 NAME
115 lwes-perl-journal-listener - listens for events from a journal
117 =head1 SYNOPSIS
119 lwes-perl-journal-listener [options] [<listener> [<args>] | <code>] <journal(s)>
121 Options:
122 --stats Print information about each file processed
123 -help Brief help message
124 -man Full Documentation
126 <listener> is the name of a perl module which extends the base
127 listener LWES::Listener and provides an initialize and processEvent
128 method. The <args> are passed directly to the listener constructor.
130 code is perl code which is embedded in a function which takes one
131 argument, a reference to a perl hash which contains the contents
132 of the event called $event
134 =head1 OPTIONS
136 =over 8
138 =item B<--stats>
140 Print information about how many events were processed and how long it took
141 to process.
143 =item B<-help>
145 Print help information
147 =item B<-man>
149 Print more verbose help information
151 =back
153 =head1 DESCRIPTION
155 The lwes-perl-journal-listener is a tool for inspecting LWES events that
156 are in journal files created by the lwes-journaller.
158 There are several ways to use the tool. To just print out events as they
159 are seen it can be invoked as
161 % lwes-perl-journal-listener <journal>
163 Alternatively you can provide a LWES::Listener module as outlined in
164 the lwes-perl-event-listener man page.
166 =cut