簡體   English   中英

計算失敗請求的百分比以及IP地址

[英]calculate the percentage of failed requests along with ip address

嗨,我正在嘗試計算失敗請求的百分比。 假設只有200和404錯誤代碼,

到目前為止,我正計划提取IP地址和錯誤代碼,並將它們放入數組中。 有沒有比我采用的方法更好的方法了

 Sample access log content:
 <pre>
 192.168.2.20 - - [28/Jul/2006:10:27:10 -0300] "GET /try/ HTTP/1.0" 200 3395
 127.0.0.1 - - [28/Jul/2006:10:22:04 -0300] "GET / HTTP/1.0" 200 2216
 127.0.0.1 - - [28/Jul/2006:10:27:32 -0300] "GET /hidden/ HTTP/1.0" 404 7218
 </pre>

and trying to output array as below

例如:array(“ 127.0.0.1” => 0.5,“ 192.168.2.20” => 0,)

       function analyzeAccessLog($fileName)
        {
            //$fh = fopen($fileName,'r') 
            $people = file_get_contents($fileName);
            if (preg_match('/\200|404?\w/',$people,$matches)) {
            {
                  $int1=$matches[0];
                  print "$int1 \n";
              }
            }
            }
           analyzeAccessLog('log.txt');

這是我想出的。 它使您可以指定所需百分比的狀態碼。 您的問題是百分比,但您還提到了存儲IP地址,因此此函數將IP及其狀態代碼存儲在數組中(與索引號匹配),因此您可以對存儲在其中的數據進行所需的操作(確定每個IP等狀態的百分比)。

function analyzeAccessLog($fileName,$status = '404'){
    $lines = file($fileName);
    $i = 0;
    $s = 0;
    foreach ($lines as $line) {

        //Get the IP
        $ip = array();
        $ip = explode('-',$line);
        $data['ip'][$i] = trim($ip[0]);

        //Get the status code.
        $code = array();
        $code = array_map('strrev', explode(' ', strrev($line)));
        $data['code'][$i] = trim($code[1]);
        if($code[1] == $status){
            $s++;
        }
        $i++;
    }
    $percentage = round((($s/$i)*100),2).'% status code '.$status;
    return $percentage;
}

$data = analyzeAccessLog('file.txt');

echo($data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM