解析日志问题

WBOY
Release: 2016-06-23 13:11:35
Original
854 people have browsed it

127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/login?aid=6&clientver=20160107&gid=F35DA66BB5EFB4F2FA5A8DF9A0DA2E8E&time=1459135036&value=ABCDEGSHIOEHIE1534876457EWQERQ42&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.017"
127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/pat?rid=51457573427 HTTP/1.1" 200 1068 "-" "-" - "0.017"
127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.021"
127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.018"
127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 174 "-" "-" - "0.043"
127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 66 "-" "-" - "0.035"
127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/img?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&sid=59&rid=41897423418&imgname=101_20160104_105657_733%20-%20%E5%89%AF%E6%9C%AC.jpg HTTP/1.1" 200 67 "-" "-" - "0.115"
127.0.0.1 - - [28/Mar/2016:11:23:15 +0800] "GET /abc/a1/end?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135378&value=9C59B861AE0AB2C6BB9DEC8819AE1015&sid=59&rid=41897423418 HTTP/1.1" 200 38 "-" "-" - "0.136"

如何解析类似以上日志文件分别获得四个接口(login,post,img,end )每分钟访问次数 ,每分钟成功数 每分钟失败数  以及每分钟平均的响应时间
日志格式最后为 响应时间如:0.136。


回复讨论(解决方案)

$log =<<< TXT127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/login?aid=6&clientver=20160107&gid=F35DA66BB5EFB4F2FA5A8DF9A0DA2E8E&time=1459135036&value=ABCDEGSHIOEHIE1534876457EWQERQ42&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/pat?rid=51457573427 HTTP/1.1" 200 1068 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.021"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.018"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 174 "-" "-" - "0.043"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 66 "-" "-" - "0.035"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/img?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&sid=59&rid=41897423418&imgname=101_20160104_105657_733%20-%20%E5%89%AF%E6%9C%AC.jpg HTTP/1.1" 200 67 "-" "-" - "0.115"127.0.0.1 - - [28/Mar/2016:11:23:15 +0800] "GET /abc/a1/end?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135378&value=9C59B861AE0AB2C6BB9DEC8819AE1015&sid=59&rid=41897423418 HTTP/1.1" 200 38 "-" "-" - "0.136"TXT;foreach(preg_split("/[\r\n]+/", $log) as $row) {  $t = explode(" ", $row);  @$res[date('Y-m-d H:i', strtotime(substr($t[3].' '.$t[4], 1, -1)))][basename(parse_url($t[6])['path'])]++;}print_r($res);
Copy after login
Copy after login
Array(    [2016-03-28 11:17] => Array        (            [login] => 1            [pat] => 1        )    [2016-03-28 11:23] => Array        (            [login] => 2            [post] => 2            [img] => 1            [end] => 1        ))
Copy after login
Copy after login
其他细节请自行补上,总是要自己动动脑筋的

使用explode分割成数组,然后分析统计即可。

$log =<<< TXT127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/login?aid=6&clientver=20160107&gid=F35DA66BB5EFB4F2FA5A8DF9A0DA2E8E&time=1459135036&value=ABCDEGSHIOEHIE1534876457EWQERQ42&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/pat?rid=51457573427 HTTP/1.1" 200 1068 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.021"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.018"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 174 "-" "-" - "0.043"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 66 "-" "-" - "0.035"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/img?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&sid=59&rid=41897423418&imgname=101_20160104_105657_733%20-%20%E5%89%AF%E6%9C%AC.jpg HTTP/1.1" 200 67 "-" "-" - "0.115"127.0.0.1 - - [28/Mar/2016:11:23:15 +0800] "GET /abc/a1/end?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135378&value=9C59B861AE0AB2C6BB9DEC8819AE1015&sid=59&rid=41897423418 HTTP/1.1" 200 38 "-" "-" - "0.136"TXT;foreach(preg_split("/[\r\n]+/", $log) as $row) {  $t = explode(" ", $row);  @$res[date('Y-m-d H:i', strtotime(substr($t[3].' '.$t[4], 1, -1)))][basename(parse_url($t[6])['path'])]++;}print_r($res);
Copy after login
Copy after login
Array(    [2016-03-28 11:17] => Array        (            [login] => 1            [pat] => 1        )    [2016-03-28 11:23] => Array        (            [login] => 2            [post] => 2            [img] => 1            [end] => 1        ))
Copy after login
Copy after login
其他细节请自行补上,总是要自己动动脑筋的



恩恩,谢谢!大神 我想统计这一分钟的平均响应时间,我该如何实现呢??

其他细节请自行补上,总是要自己动动脑筋的

响应时间 是哪项?
加起来除以条数

响应时间 是哪项?
加起来除以条数


就是最后的那个数字 第一条是0.017

$log =<<< TXT127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/login?aid=6&clientver=20160107&gid=F35DA66BB5EFB4F2FA5A8DF9A0DA2E8E&time=1459135036&value=ABCDEGSHIOEHIE1534876457EWQERQ42&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/pat?rid=51457573427 HTTP/1.1" 200 1068 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.021"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.018"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 174 "-" "-" - "0.043"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 66 "-" "-" - "0.035"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/img?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&sid=59&rid=41897423418&imgname=101_20160104_105657_733%20-%20%E5%89%AF%E6%9C%AC.jpg HTTP/1.1" 200 67 "-" "-" - "0.115"127.0.0.1 - - [28/Mar/2016:11:23:15 +0800] "GET /abc/a1/end?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135378&value=9C59B861AE0AB2C6BB9DEC8819AE1015&sid=59&rid=41897423418 HTTP/1.1" 200 38 "-" "-" - "0.136"TXT;foreach(preg_split("/[\r\n]+/", $log) as $row) {  $t = explode(" ", $row);  $k = date('Y-m-d H:i', strtotime(substr($t[3].' '.$t[4], 1, -1)));  @$res[$k][basename(parse_url($t[6])['path'])]++;  $res[$k]['avg'][] = trim(end($t), '"'); //新增}foreach($res as &$v) $v['avg'] = array_sum($v['avg']) / count($v['avg']); //新增print_r($res);
Copy after login
Copy after login

Array(    [2016-03-28 11:17] => Array        (            [login] => 1            [avg] => 0.017            [pat] => 1        )    [2016-03-28 11:23] => Array        (            [login] => 2            [avg] => 0.061333333333333            [post] => 2            [img] => 1            [end] => 1        ))
Copy after login
位置不对就自己改

$log =<<< TXT127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/login?aid=6&clientver=20160107&gid=F35DA66BB5EFB4F2FA5A8DF9A0DA2E8E&time=1459135036&value=ABCDEGSHIOEHIE1534876457EWQERQ42&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:17:33 +0800] "GET /abc/a1/pat?rid=51457573427 HTTP/1.1" 200 1068 "-" "-" - "0.017"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.021"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "GET /abc/a1/login?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&pwd=4897654897486489WERQER487WEEQWEW HTTP/1.1" 200 38 "-" "-" - "0.018"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 174 "-" "-" - "0.043"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/post?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18 HTTP/1.1" 200 66 "-" "-" - "0.035"127.0.0.1 - - [28/Mar/2016:11:23:14 +0800] "POST /abc/a1/img?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135377&value=5913350E12FE86E07E794E8E532D2D18&sid=59&rid=41897423418&imgname=101_20160104_105657_733%20-%20%E5%89%AF%E6%9C%AC.jpg HTTP/1.1" 200 67 "-" "-" - "0.115"127.0.0.1 - - [28/Mar/2016:11:23:15 +0800] "GET /abc/a1/end?aid=5&clientver=20160107&gid=4897654WQERWQWRRWRERASDW595564WQ&time=1459135378&value=9C59B861AE0AB2C6BB9DEC8819AE1015&sid=59&rid=41897423418 HTTP/1.1" 200 38 "-" "-" - "0.136"TXT;foreach(preg_split("/[\r\n]+/", $log) as $row) {  $t = explode(" ", $row);  $k = date('Y-m-d H:i', strtotime(substr($t[3].' '.$t[4], 1, -1)));  @$res[$k][basename(parse_url($t[6])['path'])]++;  $res[$k]['avg'][] = trim(end($t), '"'); //新增}foreach($res as &$v) $v['avg'] = array_sum($v['avg']) / count($v['avg']); //新增print_r($res);
Copy after login
Copy after login


好的 谢谢!!!谢谢大神!!!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template