📄 tomcat_trend.pl
字号:
last if( $logtime >= $curdate ); if( $line =~ /\d\)\]: / ) { # Handle a mod_jk error # print "mod_jk error! " . scalar(localtime($logtime)) . " $line\n"; if( $line =~ /(jk_tcp_socket_recvfull failed|ERROR: Receiving from tomcat failed)/ ) { $Global{tomcat_full}++; $Interval{tomcat_full}++; } elsif( $line =~ /(ajp_process_callback - write failed|ERROR sending data to client. Connection aborted or network problems)/ ) { $Global{client_gone}++; $Interval{client_gone}++; } next; } else { # Handle a mod_jk request log entry # print "$line\n"; $line =~ s/^\[.*\] //; # print "$line\n"; $line =~ s/\"(GET|POST|OPTIONS|HEAD)[^\"]*\" //; # print "$line\n"; $line =~ s/[\?\;].*\"//; # print "$line\n"; $line =~ s/\"//g; # print "$line\n"; ($work, $host, $page, $status, $latency) = split /\s+/,$line; $page =~ s/\/\//\//g; $page =~ s/\.\//\//g; # print scalar(localtime($logtime)) . " $work $host $page $status $latency\n"; if( length($work) <= 0 || length($host) <= 0 || length($page) <= 0 || $status !~ /^\d+$/ || $latency !~ /^\d+\.\d+$/ ) { print "Unknown log entry: $origline\n" unless $origline =~ /\.c /; next; } # Throw out abnormally long requests and log them as an error if( $latency >= 1800 ) { $Global{errors} .= "Error: $page has an HTTP status of $status and an "; $Global{errors} .= "abnormally long request latency of $latency seconds\n"; next; } # Save the data by day for Global, Worker, and Host push @{$Global{latency}},$latency; $workers = $Global{workers}; if( !defined $$workers{$work} ) { undef @{"$work"}; undef %{"$work"}; undef %{"$work-hosts"}; ${"$work"}{latency} = \@{"$work"}; ${"$work"}{hosts} = \%{"$work-hosts"}; ${"$work"}{interval} = ""; $$workers{$work} = \%{"$work"}; } $worker = $$workers{$work}; push @{$$worker{latency}},$latency; if( !defined $$worker{hosts}{$host} ) { undef @{"$work-$host"}; undef %{"$work-$host"}; undef %{"$work-$host-pages"}; ${"$work-$host"}{latency} = \@{"$work-$host"}; ${"$work-$host"}{pages} = \%{"$work-$host-pages"}; ${"$work-$host"}{interval} = ""; $$worker{hosts}{$host} = \%{"$work-$host"}; } $hoster = $$worker{hosts}{$host}; push @{$$hoster{latency}},$latency; if( !defined $$hoster{pages}{$page} ) { undef @{"$work-$host-$page"}; $$hoster{pages}{$page} = \@{"$work-$host-$page"}; } push @{$$hoster{pages}{$page}},$latency; # Save the data by 5 minute interval for Global, Worker, and Host push @{$Interval{latency}},$latency; $workers = $Interval{workers}; if( !defined $$workers{"$work"} ) { undef @{"int-$work"}; undef %{"int-$work"}; undef %{"int-$work-hosts"}; ${"int-$work"}{latency} = \@{"int-$work"}; ${"int-$work"}{hosts} = \%{"int-$work-hosts"}; $$workers{$work} = \%{"int-$work"}; } $worker = $$workers{$work}; push @{$$worker{latency}},$latency; if( !defined $$worker{hosts}{$host} ) { undef @{"int-$work-$host"}; undef %{"int-$work-$host"}; ${"int-$work-$host"}{latency} = \@{"int-$work-$host"}; $$worker{hosts}{$host} = \%{"int-$work-$host"}; } $hoster = $$worker{hosts}{$host}; push @{$$hoster{latency}},$latency; } } } undef $fh;}# If the last log file ends before switch to the current day,# output the last days dataif( $logtime < $curdate ) { &IntervalStats(\%Global,\%Interval,$previnterval*300); &DailyStats($startdate,\%Global);}exit;sub IntervalStats($$$) { my $global = $_[0]; my $data = $_[1]; my $interval = $_[2]; ($count,$median,$mean,$stddev,$min,$max) = &CalcStats($$data{latency}); $$global{interval} .= "$interval $count $median $mean $stddev $min $max $$data{client_gone} $$data{tomcat_full}\n"; foreach $work ( keys %{$$data{workers}} ) { $worker = $$data{workers}{$work}; $gworker = $$global{workers}{$work}; ($count,$median,$mean,$stddev,$min,$max) = &CalcStats($$worker{latency}); $$gworker{interval} .= "$interval $count $median $mean $stddev $min $max\n"; foreach $host ( keys %{$$worker{hosts}} ) { $hoster = $$worker{hosts}{$host}; $ghoster = $$gworker{hosts}{$host}; ($count,$median,$mean,$stddev,$min,$max) = &CalcStats($$hoster{latency}); $$ghoster{interval} .= "$interval $count $median $mean $stddev $min $max\n"; } }}sub DailyStats($$) { my $date = $_[0]; my $data = $_[1]; &SaveStats($data,$date,"","global"); &SaveFile($$data{interval},$date,"","daily"); foreach $work ( keys %{$$data{workers}} ) { $worker = $$data{workers}{$work}; &SaveStats($worker,$date,$work,"global"); &SaveFile($$worker{interval},$date,$work,"daily"); foreach $host ( keys %{$$worker{hosts}} ) { $hoster = $$worker{hosts}{$host}; &SaveStats($hoster,$date,"$work/$host","global"); &SaveFile($$hoster{interval},$date,"$work/$host","daily"); $pagedata = ""; foreach $page ( sort keys %{$$hoster{pages}} ) { $pager = $$hoster{pages}{$page}; ($count,$median,$mean,$stddev,$min,$max) = &CalcStats($pager); $pagedata .= "$page $count $median $mean $stddev $min $max\n"; } $pagedata .= $$data{errors}; &SaveFile($pagedata,$date,"$work/$host","request"); } }}sub CalcStats($) { my $data = $_[0]; $stats = Statistics::Descriptive::Full->new(); $stats->add_data(@{$data}); $median = $stats->median(); $mean = $stats->mean(); $stddev = $stats->standard_deviation(); $max = $stats->max(); $min = $stats->min(); $count = $stats->count(); return ($count,$median,$mean,$stddev,$min,$max);}sub SaveStats($$$$) { my $data = $_[0]; my $date = $_[1]; my $dir = $_[2]; my $file = $_[3]; if( length($dir) > 0 ) { $dir = "$archivedir/$dir"; } else { $dir = $archivedir; } mkdir "$dir",0755; $outfile = "$dir/${file}.data"; ($count,$median,$mean,$stddev,$min,$max) = &CalcStats($$data{latency}); open DATA, ">>$outfile" or die $!; print DATA "$date $count $median $mean $stddev $min $max"; print DATA " $$data{client_gone} $$data{tomcat_full}" if defined $$data{tomcat_full}; print DATA "\n"; close DATA;}sub SaveFile($$$$) { my $data = $_[0]; my $date = $_[1]; my $dir = $_[2]; my $file = $_[3]; my ($day, $mon, $year); ($day, $mon, $year) = (localtime($date))[3..5]; $year += 1900; $mon++; $mon = "0$mon" if $mon < 10; $day = "0$day" if $day < 10; $file = "$year-$mon-$day-$file"; if( length($dir) > 0 ) { $dir = "$archivedir/$dir"; } else { $dir = $archivedir; } mkdir "$dir",0755; $outfile = "$dir/${file}.data"; open DATA, ">>$outfile" or die $!; print DATA $data; close DATA;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -