ApacheLog-Parser

 view release on metacpan or  search on metacpan

bin/cron.loghack  view on Meta::CPAN

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  $home ||= shift(@args);
  defined($home) or die "must have a 'home' argument!\n";
  chdir($home) or die "cannot chdir('$home') $!";
 
  $o{path} = File::Fu->file($o{path});
 
  my $self = bless(\%o, __PACKAGE__);
 
  local $SIG{__DIE__} = $self->quiet ? sub {
    die @_ if $^S; # get out if we're in an eval
    $self->stderr(@_);
    $self->death(@_);
  } : $SIG{__DIE__};
 
  my @got = $self->do_fetch(@only ? @only : $self->servers);
  my @links = $self->do_links(@got);
  $self->do_import(@links);
  $self->do_archive(@links);
}
 
sub servers {

bin/cron.loghack  view on Meta::CPAN

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  return($ret, $out, $err);
}
 
sub run {
  my $self = shift;
  my (@command) = @_;
 
  $self->stdout('running', "  @command");
  my ($ret, $out, $err) = $self->_run(@command);
  $self->stdout(split(/\n/, $out));
  $self->stderr(split(/\n/, $err));
  $ret or die "@command failed:\n$err";
  return($out);
}
 
sub capture {
  my $self = shift;
  my ($ret, $out, $err) = $self->_run(@_);
  $ret or die "@_ failed $err";
  my @out = split(/\n/, $out);
  (@out == 1) and return($out[0]);
  return(@out);
}
 
sub death {
  my $self = shift;
  my (@last) = @_;
 
  my %sym = (stderr => 'E ', stdout => '# ');
  print "DEATH\n  @last\n";
  my $out = $self->{outputs} ||= [];
  foreach my $line (@$out) {
    my $type = shift(@$line);
    my $c = $sym{$type} || $type; # hmm
    print join(' ', $c, @$line), "\n";
  }
}
my %fh = (
  stderr => \*STDERR,
  stdout => \*STDOUT,
);
sub stderr {
  my $self = shift;
  $self->_store_io(stderr => @_);
}
sub stdout {
  my $self = shift;
  $self->_store_io(stdout => @_);
}
sub _store_io {
  my $self = shift;
  my ($type, @lines) = @_;
  unless($self->quiet) {
    my $fh = $fh{$type};

bin/loghack  view on Meta::CPAN

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  my ($dir, @hosts) = @_;
  require IPC::Open3;
  require IO::Select;
  require IO::Handle;
  my $sel = IO::Select->new();
  my %track;
  my $prog = basename($0);
  foreach my $host (@hosts) {
    (my $realhost = $host) =~ s/#\d+$//;
    my $stdin;
    my ($stdout, $stderr) = map({IO::Handle->new} 1..3);
    my $pid = IPC::Open3::open3(
      $stdin, $stdout, $stderr,
      ($realhost eq 'localhost' ? () : ('ssh', $realhost)),
      $prog, '-d', $dir
    );
    #warn "started $pid to $host";
    $stdout->autoflush;
    $stderr->autoflush;
    $pid or die "gah no pid\n";
    #warn "$stdin, $stdout, $stderr";
    $track{$pid} = my $obj = {
      stdin  => $stdin,
      stdout => $stdout,
      stderr => $stderr,
      host   => $host,
    };
    $sel->add($obj->{sel_o} = [$stdout, $pid, 'stdout']);
    $sel->add($obj->{sel_e} = [$stderr, $pid, 'stderr']);
  }
  return($sel, %track);
}
my $lglob = sub {
  my ($opt, @spec) = @_;
  local $opt->{lazy_glob} = 1;
  return(repo_files($opt, @spec));
};
my $datethru = sub {shift(@_); _date_dwim(@_) };
my %cluster_fspec = (

bin/loghack  view on Meta::CPAN

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  $hlen = $l if($l > $hlen);
}
 
#die map({"$_ => " . join(", ", %{$track{$_}})} keys(%track));
my %hmap = map({$track{$_}{host} => $_} keys(%track));
my %sels = map({$track{$_->[1]}{host} => $_} $sel->handles);
 
my %blacklist;
my $output = sub {
  my ($host, $which, @lines) = @_;
  my $pref = ($which eq 'stderr' ? '!' : '#');
  printf("%-${hlen}s %s %s", $host, $pref, $_) for(@lines);
};
my $end_host = sub {
  my ($host) = @_;
 
  my $pid = delete($hmap{$host}) or die "no pid at $host";
  my $obj = delete($track{$pid});
 
  warn ' 'x($hlen+1), "closing $host\n";
  close($obj->{stdin});
  my $errh = $obj->{stdout};
  local $SIG{ALRM} = sub { warn "no stderr on $host\n"};
  alarm(2);
  $output->($host, 'stderr', <$errh>);
  alarm(0);
  $sel->remove(delete($obj->{sel_o})) or die;
  #$errh->blocking(0);
  $sel->remove(delete($obj->{sel_e})) or die;
};
my $fill_host = sub {
  my ($host) = @_;
 
  if($blacklist{$host}) {
    warn "$host is blacklisted\n";

bin/loghack  view on Meta::CPAN

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
      $blacklist{$host} = 1;
      $end_host->($host);
    }
  }
};
 
 
# go!
$fill_host->($_) for(@hosts);
 
my %f = (stderr => 0, stdout => 1);
while($sel->count) {
  READ: while(my @ready = $sel->can_read) {
    @ready = sort({$f{$a->[2]} <=> $f{$b->[2]}} @ready);
 
    foreach my $bit (@ready) {
      my ($fh, $pid, $which) = @$bit;
      my $obj = $track{$pid};
      my $host = $obj->{host};
      $fh->blocking(0);
      until(eof($fh)) {



( run in 0.338 second using v1.01-cache-2.11-cpan-d6f9594c0a5 )