繁体   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