计算Apache最后10秒的响应时间

计算Apache最后10秒的响应时间

我有下一个 apache 的日志格式:

LogFormat "%h %l %u %t \"%r\" %>s %b %D \"%{Referer}i\" \"%{User-Agent}i\""

where's %D- 服务请求所花费的时间(以微秒为单位)。

我想知道如何获得最后 10 秒的平均响应时间。脚本或其他东西应该以某种方式计算过去 10 秒的请求数并总结响应时间。结果应该是:number_of_requests_for_last_10_Seconds / sum_of_request_time_for_last_10_Seconds。

这可能与 awk 或其他东西有关吗?谢谢

更新

日志样本:

93.182.72.47 - - [19/Aug/2014:02:24:19 -0700] "GET /test/085 HTTP/1.1" 200 1006 445 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"
119.232.112.148 - - [19/Aug/2014:02:24:19 -0700] "GET /test/003 HTTP/1.1" 200 3 84234 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"
85.244.38.232 - - [19/Aug/2014:02:24:19 -0700] "GET /test/332 HTTP/1.1" 200 3 75760 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
236.131.91.87 - - [19/Aug/2014:02:24:16 -0700] "GET /test/006 HTTP/1.1" 200 32 3640965 "-" "python-requests/2.2.1 CPython/2.7.3 Linux/2.6.32-431.17.1.el6.x86_64"
112.241.72.130 - - [19/Aug/2014:02:24:19 -0700] "GET /test/042 HTTP/1.1" 200 50 1148668 "-" "python-requests/2.2.1 CPython/2.6.6 Linux/2.6.32-431.20.3.el6.x86_64"

更新2

tailf /var/log/httpd/access_log | perl -MTime::Piece -lne '
   BEGIN{$threshold = (localtime) - 10}
   if (/\[(.*?)\] ".*?" \d+ \d+ (\d+)/) {
     $d = Time::Piece->strptime($1, "%d/%b/%Y:%H:%M:%S %z");print $d->datetime." ".$threshold->datetime;
     if ($d >= $threshold) {$t += $2; $n++}
   }
   END{print $t/$n if $n}' /var/log/httpd/access_log

...
2014-08-19T03:54:42 2014-08-19T03:54:27
2014-08-19T03:54:43 2014-08-19T03:54:27
2014-08-19T03:54:43 2014-08-19T03:54:27
2014-08-19T03:54:42 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:43 2014-08-19T03:54:27
2014-08-19T03:54:43 2014-08-19T03:54:27
2014-08-19T03:54:43 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:43 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:45 2014-08-19T03:54:27
2014-08-19T03:54:45 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:45 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:44 2014-08-19T03:54:27
2014-08-19T03:54:46 2014-08-19T03:54:27

答案1

这是一个典型的工作perl

对于 Time::Piece 1.17 及以上版本(perl5.12 及以上版本):

perl -MTime::Piece -lne '
   BEGIN{$threshold = (localtime) - 10}
   if (/\[(.*?)\] ".*?" \d+ \d+ (\d+)/) {
     $d = Time::Piece->strptime($1, "%d/%b/%Y:%H:%M:%S %z");
     if ($d >= $threshold) {$t += $2; $n++}
   }
   END{print $t/$n if $n}' your-file.log

Time::Piece 1.15 及以下版本不支持%z,因此,假设调用者的时区与日志文件的时区匹配,您可以这样做:

perl -MTime::Piece -lne '
   BEGIN{$threshold = (localtime) - 10}
   if (/\[([^]]*:\d+).*?".*?" \d+ \d+ (\d+)/) {
     $d = Time::Piece->strptime($1, "%d/%b/%Y:%H:%M:%S");
     if ($d->datetime ge $threshold->datetime) {$t += $2; $n++}
   }
   END{print $t/$n if $n}' your-file.log

相关内容