| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # Copyright (C) 2008 Eric L. Wilhelm | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | cron.loghack - fetch, import, archive | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 Setup | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | cd /disks/logs/ | ||||||||||||||||||||
| 15 | mkdir mysite.{repo,archive,incoming} | ||||||||||||||||||||
| 16 | mkdir mysite.{repo,archive,incoming}/{server1,server2} | ||||||||||||||||||||
| 17 | mkdir mysite.repo/.config | ||||||||||||||||||||
| 18 | vim /disks/logs/mysite/.config/report.conf | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Then your crontab entry is: | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | cron.loghack --site mysite --home /disks/logs | ||||||||||||||||||||
| 23 | --path /var/log/apache2/foo.access.log.N.gz \ | ||||||||||||||||||||
| 24 | --number 2 \ | ||||||||||||||||||||
| 25 | --quiet | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | Requires rsync. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | package bin::cron_loghack; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | ▶ | use Getopt::Helpful; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 1 | ▶ | use IPC::Run; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1 | ▶ | use File::Fu; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1 | ▶ | use Class::Accessor::Classy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | ro qw(site path number quiet); | ||||||||||||||||||||
| 40 | 1 | ▶ | no Class::Accessor::Classy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | # directory names: | ||||||||||||||||||||
| 42 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=20.3
| |||||||||||||||||||||
| 43 | 1 | foreach my $m (qw(repo archive incoming)) { | |||||||||||||||||||
| 44 | 0 | ▶ | eval("sub $m { | ||||||||||||||||||
|
Subroutine
archive: not called CC=2 SCAR=16.7
Subroutine
incoming: not called CC=2 SCAR=16.7
Subroutine
repo: not called CC=2 SCAR=16.7
Branch: 3 branches
| |||||||||||||||||||||
| 45 | my \$self = shift; | ||||||||||||||||||||
| 46 | \$self->{$m} and return(\$self->{$m}); | ||||||||||||||||||||
| 47 | \$self->{$m} = File::Fu->dir(\$self->{site} . '.$m'); | ||||||||||||||||||||
| 48 | };"); | ||||||||||||||||||||
| 49 | 3 | ▶ | $@ and die "I botched it $@"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | sub main { | ||||||||||||||||||
|
Subroutine
main: not called CC=9 SCAR=45.0
| |||||||||||||||||||||
| 55 | 0 | my (@args) = @_; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | my %o = ( | |||||||||||||||||||
| 58 | site => undef, | ||||||||||||||||||||
| 59 | path => '/var/log/apache2/access.log.N.gz', | ||||||||||||||||||||
| 60 | number => 2, | ||||||||||||||||||||
| 61 | quiet => 0, | ||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||
| 63 | 0 | my $home; | |||||||||||||||||||
| 64 | my $only; | ||||||||||||||||||||
| 65 | my $hopt = Getopt::Helpful->new( | ||||||||||||||||||||
| 66 | usage => 'CALLER [options]', | ||||||||||||||||||||
| 67 | ['s|site=s', \$o{site}, '<sitename>', 'for sitename.repo'], | ||||||||||||||||||||
| 68 | ['home=s', \$home, '<homedir>', 'level above repositories'], | ||||||||||||||||||||
| 69 | ['p|path=s', \$o{path}, '<filepath>', 'logfilename template'], | ||||||||||||||||||||
| 70 | ['n|number=i', \$o{number}, '2', 'logrotate file number'], | ||||||||||||||||||||
| 71 | 0 | ['q|quiet', \$o{quiet}, '', 'suppress output unless errors'], | |||||||||||||||||||
| 72 | ['o|only=s', \$only, '<a,b>', 'list of servers'], | ||||||||||||||||||||
| 73 | '+help', | ||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||
| 75 | 0 | $hopt->Get_from(\@args); | |||||||||||||||||||
| 76 | 0 | ▶ | my @only = $only ? split(/,/, $only) : (); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | $o{site} ||= shift(@args); | ||||||||||||||||||
|
Condition0%0/3
$o{"site"} ||= shift @args
MC/DC0%0/2
$o{"site"} ||= shift @args
$o{"site"}
shift @args
Truth table0%0/3
A ||= B
A= $o{"site"}B= shift @args
| |||||||||||||||||||||
| 79 | 0 | ▶ | $o{site} or die "must have a 'site' argument\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | ▶ | $home ||= shift(@args); | ||||||||||||||||||
|
Condition0%0/3
$home ||= shift @args
MC/DC0%0/2
$home ||= shift @args
$home
shift @args
Truth table0%0/3
A ||= B
A= $homeB= shift @args
| |||||||||||||||||||||
| 82 | 0 | ▶ | defined($home) or die "must have a 'home' argument!\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 0 | ▶ | chdir($home) or die "cannot chdir('$home') $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | $o{path} = File::Fu->file($o{path}); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | my $self = bless(\%o, __PACKAGE__); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | local $SIG{__DIE__} = $self->quiet ? sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 90 | 0 | ▶ | die @_ if $^S; # get out if we're in an eval | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 0 | $self->stderr(@_); | |||||||||||||||||||
| 92 | 0 | $self->death(@_); | |||||||||||||||||||
| 93 | 0 | ▶ | } : $SIG{__DIE__}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | my @got = $self->do_fetch(@only ? @only : $self->servers); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | my @links = $self->do_links(@got); | |||||||||||||||||||
| 97 | 0 | $self->do_import(@links); | |||||||||||||||||||
| 98 | 0 | $self->do_archive(@links); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | sub servers { | ||||||||||||||||||
|
Subroutine
servers: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | ▶ | opendir(my $dh, $self->repo) or die "cannot read repository $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | return(grep({$_ !~ m/^\./} readdir($dh))); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | sub do_fetch { | ||||||||||||||||||
|
Subroutine
do_fetch: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||
| 110 | 0 | my (@servers) = @_; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | my $num = $self->number; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | my $target = $self->path & | ||||||||||||||||||||
| 115 | 0 | ▶ | sub { s/\.N\./.$num./ or die $self->path . " has no .N."}; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | my @fetched; | |||||||||||||||||||
| 118 | 0 | foreach my $server (@servers) { | |||||||||||||||||||
| 119 | 0 | my $local = $self->incoming / $server + $target->basename; | |||||||||||||||||||
| 120 | 0 | $self->remote_copy("$server:$target", "$local"); | |||||||||||||||||||
| 121 | 0 | push(@fetched, $local); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | 0 | return(@fetched); | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | 0 | ▶ | sub do_links { | ||||||||||||||||||
|
Subroutine
do_links: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||
| 127 | 0 | my @files = @_; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | my $num = $self->number; | |||||||||||||||||||
| 130 | 0 | foreach my $file (@files) { | |||||||||||||||||||
| 131 | 0 | $file = File::Fu->file($file); | |||||||||||||||||||
| 132 | 0 | my $date = $self->capture('loghack', 'date', $file); | |||||||||||||||||||
| 133 | 0 | ▶ | my $dest = $file & sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 134 | 0 | ▶ | s/\.$num\./.$date./ or die "$file is not the right number"}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 0 | $file = $file->link($dest); | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | 0 | return(@files); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 0 | ▶ | sub do_import { | ||||||||||||||||||
|
Subroutine
do_import: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||
| 141 | 0 | my (@files) = @_; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 0 | ▶ | chdir($self->repo) or die "cannot chdir $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | 0 | $self->run('loghack', 'import', map({"../$_"} @files)); | |||||||||||||||||||
| 145 | 0 | ▶ | chdir('..') or die "cannot chdir back $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | 0 | ▶ | sub do_archive { | ||||||||||||||||||
|
Subroutine
do_archive: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 148 | 0 | my $self = shift; | |||||||||||||||||||
| 149 | 0 | my (@files) = @_; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | foreach my $file (@files) { | |||||||||||||||||||
| 152 | 0 | $file = File::Fu->file($file); | |||||||||||||||||||
| 153 | 0 | my $server = $file->dirname->part(-1); | |||||||||||||||||||
| 154 | 0 | ▶ | (my $base = $file->file) =~ s/.*\.(\d{4}-\d{2}-\d{2})\./$server.$1./ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 155 | or die "$file is a strange filename\n"; | ||||||||||||||||||||
| 156 | 0 | $base =~ s/\.gz$/\.bz2/; | |||||||||||||||||||
| 157 | 0 | my $dest = $self->archive / $server + $base; | |||||||||||||||||||
| 158 | 0 | $self->stdout("archive $file to $dest"); | |||||||||||||||||||
| 159 | 0 | ▶ | $dest->e and die "$dest already exists"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | 0 | ▶ | if($file =~ m/\.bz2$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 0 | $file->rename($dest); | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | else { | ||||||||||||||||||||
| 164 | 0 | ▶ | IPC::Run::run( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | [qw(gunzip -c)], '<', "$file", '|', | ||||||||||||||||||||
| 166 | [qw(bzip2 -c)], '>', "$dest" | ||||||||||||||||||||
| 167 | ) or die "archiving $dest failed"; | ||||||||||||||||||||
| 168 | 0 | $file->unlink; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | chomp(my $rsync = `which rsync`); | ||||||||||||||||||||
| 173 | $rsync or die "you need rsync"; | ||||||||||||||||||||
| 174 | 0 | ▶ | sub remote_copy { | ||||||||||||||||||
|
Subroutine
remote_copy: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 175 | 0 | my $self = shift; | |||||||||||||||||||
| 176 | 0 | my ($from, $to) = @_; | |||||||||||||||||||
| 177 | 0 | $self->run($rsync, '-p', $from, $to); | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | ▶ | sub _run { | ||||||||||||||||||
|
Subroutine
_run: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 181 | 0 | my $self = shift; | |||||||||||||||||||
| 182 | 0 | my (@command) = @_; | |||||||||||||||||||
| 183 | 0 | my ($in, $out, $err); | |||||||||||||||||||
| 184 | 0 | my $ret = IPC::Run::run([@command], \$in, \$out, \$err); | |||||||||||||||||||
| 185 | 0 | return($ret, $out, $err); | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | ▶ | sub run { | ||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 189 | 0 | my $self = shift; | |||||||||||||||||||
| 190 | 0 | my (@command) = @_; | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 0 | $self->stdout('running', " @command"); | |||||||||||||||||||
| 193 | 0 | my ($ret, $out, $err) = $self->_run(@command); | |||||||||||||||||||
| 194 | 0 | $self->stdout(split(/\n/, $out)); | |||||||||||||||||||
| 195 | 0 | $self->stderr(split(/\n/, $err)); | |||||||||||||||||||
| 196 | 0 | ▶ | $ret or die "@command failed:\n$err"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | 0 | return($out); | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 0 | ▶ | sub capture { | ||||||||||||||||||
|
Subroutine
capture: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 201 | 0 | my $self = shift; | |||||||||||||||||||
| 202 | 0 | my ($ret, $out, $err) = $self->_run(@_); | |||||||||||||||||||
| 203 | 0 | ▶ | $ret or die "@_ failed $err"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | 0 | my @out = split(/\n/, $out); | |||||||||||||||||||
| 205 | 0 | ▶ | (@out == 1) and return($out[0]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | 0 | return(@out); | |||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | 0 | ▶ | sub death { | ||||||||||||||||||
|
Subroutine
death: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 210 | 0 | my $self = shift; | |||||||||||||||||||
| 211 | 0 | my (@last) = @_; | |||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 0 | my %sym = (stderr => 'E ', stdout => '# '); | |||||||||||||||||||
| 214 | 0 | print "DEATH\n @last\n"; | |||||||||||||||||||
| 215 | 0 | ▶ | my $out = $self->{outputs} ||= []; | ||||||||||||||||||
|
Condition0%0/2
$self->{"outputs"} ||= []
MC/DC0%0/1
$self->{"outputs"} ||= []
$self->{"outputs"}
Truth table0%0/2
A
A= $self->{"outputs"}
| |||||||||||||||||||||
| 216 | 0 | foreach my $line (@$out) { | |||||||||||||||||||
| 217 | 0 | my $type = shift(@$line); | |||||||||||||||||||
| 218 | 0 | ▶ | my $c = $sym{$type} || $type; # hmm | ||||||||||||||||||
|
Condition0%0/3
$sym{$type} || $type
MC/DC0%0/2
$sym{$type} || $type
$sym{$type}
$type
Truth table0%0/3
A || B
A= $sym{$type}B= $type
| |||||||||||||||||||||
| 219 | 0 | print join(' ', $c, @$line), "\n"; | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | my %fh = ( | ||||||||||||||||||||
| 223 | stderr => \*STDERR, | ||||||||||||||||||||
| 224 | stdout => \*STDOUT, | ||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||
| 226 | 0 | ▶ | sub stderr { | ||||||||||||||||||
|
Subroutine
stderr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 227 | 0 | my $self = shift; | |||||||||||||||||||
| 228 | 0 | $self->_store_io(stderr => @_); | |||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | 0 | ▶ | sub stdout { | ||||||||||||||||||
|
Subroutine
stdout: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 231 | 0 | my $self = shift; | |||||||||||||||||||
| 232 | 0 | $self->_store_io(stdout => @_); | |||||||||||||||||||
| 233 | } | ||||||||||||||||||||
| 234 | 0 | ▶ | sub _store_io { | ||||||||||||||||||
|
Subroutine
_store_io: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 235 | 0 | my $self = shift; | |||||||||||||||||||
| 236 | 0 | my ($type, @lines) = @_; | |||||||||||||||||||
| 237 | 0 | ▶ | unless($self->quiet) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 238 | 0 | my $fh = $fh{$type}; | |||||||||||||||||||
| 239 | 0 | print $fh join("\n", @lines, ''); | |||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | 0 | ▶ | my $out = $self->{outputs} ||= []; | ||||||||||||||||||
|
Condition0%0/2
$self->{"outputs"} ||= []
MC/DC0%0/1
$self->{"outputs"} ||= []
$self->{"outputs"}
Truth table0%0/2
A
A= $self->{"outputs"}
| |||||||||||||||||||||
| 242 | 0 | push(@$out, map({[$type, $_]} @lines)); | |||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | package main; | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | if($0 eq __FILE__) { | ||||||||||||||||||||
| 248 | bin::cron_loghack::main(@ARGV); | ||||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | # vi:ts=2:sw=2:et:sta | ||||||||||||||||||||
| 252 | my $package = 'bin::cron_loghack'; | ||||||||||||||||||||