-i input filename (instead of having it hard-coded) -s output .stat file (to a certain location) -o output .csv file (to a certain location) #### # This program parses a error_log for necessary information and outputs a CSV file with the highest busy value for each 5 minute interval. # The program also outputs a STAT file with the last 5 minute interval of the CSV. use strict; use warnings; # Ignore theses values my %ignorables = map { $_ => 1 } qw([notice mpmstats: rdy bsy rd wr ka log dns cls bsy: in); # Open the error log - Change the name of the file as needed. open my $error_fh, '<', 'iset_error_log'; # 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 (<$error_fh>) { # 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; # Initialize CSV file and STAT file and print the header for the CSV file open my $fh, ">", 'log_5min.csv' or die $!; print $fh "Time Interval,rdy,bsy,rd,wr,ka,log,dns,cls,ap22,sm22,apache\n"; open my $FILE, ">", 'last_5min.stat' or die $!; # 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 print $fh $_->[2] for @maxima; # Print the last line to the STAT file my $string = (); $string = $_->[2] for $maxima[-1]; my ($timeStamp, $rdy, $by, $rd, $wr, $ka, $log, $dns, $cls, $ap22, $sm22, $apache) = split /[,]/, $string; print $FILE "$by\nMax Busy\n";