過去10秒間にApacheの応答時間を計算します。

過去10秒間にApacheの応答時間を計算します。

次の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

おすすめ記事