-1

我通过命令行传递输入文件位置和输出文件位置,我收到这些错误:

值“C:\Users\Mruppe2\workspace\Perl”对选项输入无效(预期数字)
值“C:\temp\output.csv”对选项输出无效(预期扩展数字)

5MinInterval2.pl 第 154 行没有这样的文件或目录。

命令行输入如下所示:

perl 5MinInterval2.pl -i C:\Users\Mruppe2\workspace\Perl 5 Essential Training\MPMStats Project\error_log -o C:\temp\output.csv

这是我的代码:

# This program parses a error_log for necessary information and outputs a CSV file with the highest busy value for each 5 minute interval.  

use strict;
use warnings;
use Getopt::Long;  

#Define argument types
my $input = '';
my $output = '';
GetOptions('input=i' => \$input,'output=o' => \$output); 

# Ignore theses values
my %ignorables = map { $_ => 1 } qw([notice mpmstats: rdy bsy rd wr ka log dns cls bsy: in);  

# Subroutine to pull the line containing AP22,SM22, and Apache stats
sub findLines {
    my($item,@result)=("");

    # Iterates over the lines in the file, putting each into $_ 
    while ($input) {      

        # Select only those fields that have the word 'notice'
        if (/\[notice/) {          

            # Place those lines with the word 'rdy' on the next line
            if (/\brdy\b/){
                push @result,"$item\n";
                $item="";

            }
            else {
                $item.=",";
            }

            # Split the line into fields, separated by spaces, skip the %ignorables         
            my @line = grep { not defined $ignorables{$_} } split /\s+/;    

            # More cleanup         
            s/|^\[|notice|[]]//g for @line; # remove unnecessary elements from the array

            # Output the line.  
            @line = join(",", @line);          
            s/,,/,/g for @line;
            map $item.=$_, @line;
            }
        } 
        @result
    }  

# Place the subroutine contents into an array
my @array = &findLines;
my $line;

# Create an subroutine to place the contents of AP22, SM22, and Apache in the correct order
sub Program{
    my @return = ();
    chomp @array;

    my ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$dow2,$mon2,$day2,$time2,$year2,$val1,$mod1,$val2,$mod2,$val3,$mod3,$ap22,$sm22,$apache);
    foreach $line (@array){
    ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$dow2,$mon2,$day2,$time2,$year2,$val1,$mod1,$val2,$mod2,$val3,$mod3) = ((split /[,]/, $line),("")x24); 
    $line = "$dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls";

    # For lines with no variables
    if ($mod1 eq ""){
        $line = $line.","."0".","."0".","."0";
    }

    # For lines with only SM22
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq ""){
        $line = $line.",".$val1.","."0".","."0";
    }

    # For lines with only AP22
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "" && $mod3 eq ""){
        $line = $line.",".$val1.","."0".","."0";
    }

    # For lines with AP22-SM22-Apache
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "mod_sm22.cpp" && $mod3 eq "ApacheModule.cpp"){
        $line = $line.",".$val1.",".$val2.",".$val3;
    }

    # For lines with SM22-AP22-Apache
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "mod_was_ap22_http.c" && $mod3 eq "ApacheModule.cpp"){
        $line = $line.",".$val2.",".$val1.",".$val3;
    }

    # For lines with AP22-SM22
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "mod_sm22.cpp" && $mod3 eq ""){
        $line = $line.",".$val1.",".$val2.","."0";
    }

    # For lines with SM22-AP22
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "mod_was_ap22_http.c" && $mod3 eq ""){
        $line = $line.",".$val2.",".$val1.","."0";
    }

    # For lines with SM22-Apache
    if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "ApacheModule.cpp" && $mod3 eq ""){
        $line = $line.","."0".",".$val2.",".$val2;
    }

    # For lines with AP22-Apache
    if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "ApacheModule.cpp" && $mod3 eq ""){
        $line = $line.",".$val1.","."0".",".$val2;
    }

    # Push the array out of the subroutine
    ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$ap22,$sm22,$apache) = ((split/[,]/, $line),("")x16);
    push @return, ("$line\n");
    }
    return @return;
}

# Initialize the hashes
my %interval; 
my %month; @month{qw/ jan feb mar apr may jun jul aug sep oct nov dec /} = '01' .. '12';  

# Put the contents of the subroutine into an array
my @finalArray = &Program;

# Delete the first line of the array
$finalArray[0] = "";

# Create a new array without the first line
my @lastArray = @finalArray;


# Select only those lines with the highest busy count in each 5 minute interval
my @maxima;  
for my $record (@lastArray) {    
    my @fields = $record =~ /([^,\s]+)/g;   
    next unless @fields;    
    my @range = @fields[1..4];
    $range[2] =~ s|(\d+):\d\d$|5*int($1/5)|e;  
    my $range = join ' ', @range;   
    my $value = $fields[5];  
    my ($dow, $mon, $day, $time, $year, $rdy, $by, $rd, $wr, $ka, $log, $dns, $cls, $ap22, $sm22, $apache) = split /[,]/, $record;
    my $record2 = "$range,$rdy,$by,$rd,$wr,$ka,$log,$dns,$cls,$ap22,$sm22,$apache";
    if (@maxima == 0 or $range ne $maxima[-1][0]) {     
        push @maxima, [$range, $value, $record2];   
    }   
    else {     
        @{$maxima[-1]}[1,2] = ($value, $record2) if $maxima[-1][1] > $value;   
    } 
} 

# Print the contents to the CSV file
open my $fh, ">", $output or die $!;
print $fh "Time Interval,rdy,bsy,rd,wr,ka,log,dns,cls,ap22,sm22,apache\n";
print $fh $_->[2] for @maxima;

我究竟做错了什么?

4

1 回答 1

2

好吧,从外观上看,您确实将“输入”声明为整数:

... 'input=i' ...

也许您正在寻找字符串:

... 'input=s' ....
于 2012-07-10T19:03:53.083 回答