/[Frey]/trunk/lib/Frey/Shell/Log.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Shell/Log.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 936 - (show annotations)
Tue Jan 6 11:56:28 2009 UTC (15 years, 3 months ago) by dpavlin
File size: 1646 byte(s)
sponge improvements
1 package Frey::Shell::Log;
2 use Moose;
3
4 extends 'Frey';
5 with 'Frey::Web';
6
7 use DateTime::Locale;
8
9 has log_command => (
10 is => 'rw',
11 isa => 'Str',
12 # required => 1,
13 default => 'tail /var/log/daemon.log',
14 );
15
16 has from_datetime => (
17 is => 'rw',
18 isa => 'Str',
19 );
20
21 has to_datetime => (
22 is => 'rw',
23 isa => 'Str',
24 );
25
26 # we don't use DateTime because it's huge overhead for every log line
27 my $month2nr;
28 {
29 my @months = @{ DateTime::Locale->load('en_US')->month_abbreviations };
30 warn "# months ", join(',',@months);
31
32 foreach my $i ( 0 .. $#months ) {
33 $month2nr->{ lc($months[$i]) } = $i + 1;
34 }
35 }
36
37 sub as_sponge {
38 my ($self) = @_;
39
40 my $cmd = $self->log_command;
41 warn "# cmd: $cmd";
42
43 my @rows;
44
45 my $from = $self->from_datetime;
46 my $to = $self->to_datetime;
47
48 my $stats = {
49 from => $from,
50 to => $to,
51 };
52
53 warn "# stats ",$self->dump( $stats );
54
55 my $yyyy = (localtime(time))[5] + 1900;
56
57 open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!";
58 while(<$fh>) {
59 chomp;
60
61 if ( m{^(\w\w\w)\s+(\d+)\s+(\d\d:\d\d:\d\d)\s+(.+)} ) {
62 my $message = $4;
63 my $mm = $month2nr->{lc($1)} || die "month $1 unknown";
64 my $dt = sprintf("%04d-%02d-%02d %s", $yyyy, $mm, $2, $3, $4);
65 if ( $from && $dt lt $from ) {
66 $stats->{before_from}++;
67 next;
68 }
69 if ( $to && $dt gt $to ) {
70 $stats->{after_to}++;
71 next;
72 }
73 push @rows, [ $dt, $message ];
74 $stats->{rows}++;
75 } else {
76 warn "# skip $_\n";
77 $stats->{skipped}++;
78 }
79 }
80
81 warn "# stats ",$self->dump( $stats );
82
83 return {
84 NAME => [ 'date', 'message' ],
85 rows => [ sort { $a->[0] cmp $b->[0] } @rows ],
86 stats => $stats,
87 log_command => $cmd,
88 }
89 }
90
91 1;

  ViewVC Help
Powered by ViewVC 1.1.26