Thread gnu-grep braucht ewig unter Perl (system)
(7 answers)
Opened by equinox at 2010-11-01 12:37
Das hier
Code (perl): (dl
)
(defined $args->{method}) && ($#{$args->{method}} + 1 == 1) Ich glaube du kommst mit dem Quoten durch einander. So sollte es funktionieren: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 sub run { my ($self) = shift; my $args = $self->{args}; my $post_method = 'POST'; my $get_method = 'GET .*=(htt|ft|ph)p(:|%3[aA])(/|%2[fF])'; my $method_string = qq!"($post_method |$get_method)!; if ( defined($args->{method}) && @{$args->{method}}==1 ) { if (lc($args->{method}->[0]) eq 'post') { $method_string = qq!"$post_method!; } elsif (lc($args->{method}->[0]) eq 'get') { $method_string = qq!"($get_method)!; } } my $command = qq!find $args->{docroot}/logs/ -maxdepth 1 -type f -name access.log\* | xargs -r -n1 -P10 /bin/zgrep -E '$method_string.* HTTP/[0-9]\.[0-9]" [23][0-9]{2}'!; open FH, "-|", $command; local $/; my $output=<FH>; close FH; return $output; } Dein Code kann durchaus schneller sein, da in deiner Pipeline geforkt wird. Das macht aber nur auf einem Mehrprozessorsystem wirklich Sinn. Das hab ich mal nachgebildet: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 #!/usr/bin/perl use strict; use warnings; use IO::Uncompress::Gunzip; use File::Type; my $ft = File::Type->new(); my @lines_found; my @files=glob('/kunden/homepages/20/d264326328/htdocs/logs/access.log*'); my %pipes; while(@files || keys(%pipes)) { if(keys(%pipes) >= 10 || @files==0) { for my $child (keys(%pipes)) { my $fh=$pipes{$child}; if(eof($fh)) { delete($pipes{$child}); next; } eval{ local $SIG{ALRM}=sub{die()}; alarm(1); while(<$fh>) { push(@lines_found,$_); } alarm(0); }; } next; } next unless(@files); my $file=shift(@files); next unless(-f $file); my $pid=open(my $child, '-|'); die("Fork failed!") unless(defined($pid)); if($pid) { $pipes{$pid}=$child; next; } child($file); } print $_ for(@lines_found); sub child { my $file=shift; my $fh=undef; my $ftype=$ft->mime_type($file); if($ftype=~/gzip/) { unless($fh=IO::Uncompress::Gunzip->new($file)) { warn "ERROR open Copressed $file ($IO::Uncompress::Gunzip::GunzipError)"; } } elsif($ftype=~/octet-stream|text/) { unless(open($fh, '<', $file)) { warn "ERROR open $file ($!)\n"; } } else { warn("ERROR open $file Filetype unknown ($ftype)\n"); } if($fh) { while(my $line=<$fh>) { print $line if($line=~m!"(?:POST |GET .*=(?:htt|ft|ph)p(?::|%3a)(?:/|%2f)).* HTTP/[0-9]\.[0-9]" [23][0-9]{2}!i) } close($fh); } exit; } Last edited: 2010-11-02 23:36:25 +0100 (CET) |