ApacheLog-Parser

 view release on metacpan or  search on metacpan

bin/cron.loghack  view on Meta::CPAN

sub servers {
  my $self = shift;

  opendir(my $dh, $self->repo) or die "cannot read repository $!";
  return(grep({$_ !~ m/^\./} readdir($dh)));
}

sub do_fetch {
  my $self = shift;
  my (@servers) = @_;

  my $num = $self->number;

  my $target = $self->path &
    sub { s/\.N\./.$num./ or die $self->path . " has no .N."};

  my @fetched;
  foreach my $server (@servers) {
    my $local = $self->incoming / $server + $target->basename;
    $self->remote_copy("$server:$target", "$local");
    push(@fetched, $local);
  }
  return(@fetched);
}
sub do_links {
  my $self = shift;
  my @files = @_;

  my $num = $self->number;
  foreach my $file (@files) {
    $file = File::Fu->file($file);
    my $date = $self->capture('loghack', 'date', $file);
    my $dest = $file & sub {
      s/\.$num\./.$date./ or die "$file is not the right number"};
    $file = $file->link($dest);
  }
  return(@files);
}
sub do_import {
  my $self = shift;
  my (@files) = @_;

  chdir($self->repo) or die "cannot chdir $!";
  $self->run('loghack', 'import', map({"../$_"} @files));
  chdir('..') or die "cannot chdir back $!";
}
sub do_archive {
  my $self = shift;
  my (@files) = @_;

  foreach my $file (@files) {
    $file = File::Fu->file($file);
    my $server = $file->dirname->part(-1);
    (my $base = $file->file) =~ s/.*\.(\d{4}-\d{2}-\d{2})\./$server.$1./
      or die "$file is a strange filename\n";
    $base =~ s/\.gz$/\.bz2/;
    my $dest = $self->archive / $server + $base;
    $self->stdout("archive $file to $dest");
    $dest->e and die "$dest already exists";
    if($file =~ m/\.bz2$/) {
      $file->rename($dest);
    }
    else {
      IPC::Run::run(
        [qw(gunzip -c)], '<', "$file", '|',
        [qw(bzip2 -c)], '>', "$dest"
      ) or die "archiving $dest failed";
      $file->unlink;
    }
  }
}
chomp(my $rsync = `which rsync`);
$rsync or die "you need rsync";
sub remote_copy {
  my $self = shift;
  my ($from, $to) = @_;
  $self->run($rsync, '-p', $from, $to);
}

sub _run {
  my $self = shift;
  my (@command) = @_;
  my ($in, $out, $err);
  my $ret = IPC::Run::run([@command], \$in, \$out, \$err);
  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";
  }
}



( run in 1.370 second using v1.01-cache-2.11-cpan-39bf76dae61 )