Created
October 7, 2015 12:57
-
-
Save huanle0610/c844708a92f1a4150c20 to your computer and use it in GitHub Desktop.
parse nginx log to get primary script unknow requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = '/var/log/nginx/YOURDOMAIN.error.log'; | |
$fd = fopen ($file, "r"); | |
while (!feof ($fd)) | |
{ | |
$buffer = fgets($fd, 4096); | |
$lines[] = $buffer; | |
} | |
fclose ($fd); | |
var_dump(count($lines)); | |
$urls = array('items' => array()); | |
$list_all = array(); | |
foreach($lines as $one) | |
{ | |
if(preg_match('/"Primary script unknown"(?:.*?)request: "(.*?) (.*?) (.*?)"/', $one, $match)) | |
{ | |
$list_all[] = array( | |
'method' => $match[1], | |
'url' => $match[2], | |
'protocol' => $match[3], | |
); | |
} | |
} | |
//$list = array_unique($list_all); | |
$list = $list_all; | |
$urls['totalCount'] = count($list); | |
$urls['items'] = $list; | |
//var_dump($urls); | |
file_put_contents('hk.json', json_encode($urls)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment