phpスクリプト解析nginxログ

1464 ワード

nginxログフォーマット
access_logログフォーマット
log_format  main  '$server_name$remote_addr$remote_user[$time_local]"$request"'
                  '$status$body_bytes_sent"$http_referer"'
                  '"$http_user_agent""$http_x_forwarded_for"';

ログパラメータ
server_name 	  	 :          
remote_addr 		 :       ip  
remote_user 	 	 :          
time_local  		 :         
status    		 :        http   
body_bytes_sent 	 :                 
http_referer	         :             
http_user_agent 	 :         
http_x_forwarded_for     :       ip

ログ分割子
特殊な印刷不可文字^A(ctrl+v,ctrl+a)をログ分割子として使用
キーワードに基づいてファイルの内容をフィルタ
需要
httpのリクエストに基づいて「weibo」というキーワードがファイルの内容を抽出しているかどうか
phpコード
    /**
     * Description:              
     *
     * @return array
     */
    function readFileContent ($filename)
    {
        $weibo_content = array();
        $fh = @fopen($filename, 'r');
        
        if ($fh) {
            while (! feof($fh)) {
                $row = fgets($fh, 4096);
                $row_arr = explode("", $row);
                if (isset($row_arr[3]) && preg_match('/weibo/', $row_arr[3])) {
                    $weibo_content[] = $row_arr;
                }
            }
        }
        fclose($fh);
        
        return $weibo_content;
    }