濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 利用PHP如何統(tǒng)計(jì)Nginx日志的User Agent數(shù)據(jù)

利用PHP如何統(tǒng)計(jì)Nginx日志的User Agent數(shù)據(jù)

熱門標(biāo)簽:銀川ai電話機(jī)器人 上海公司外呼系統(tǒng)線路 電梯外呼線路板維修視頻 臨沂智能電銷機(jī)器人軟件 地圖標(biāo)注風(fēng)向標(biāo) 安陽(yáng)自動(dòng)外呼系統(tǒng)價(jià)格是多少 芒果電銷機(jī)器人 浙江外呼電話系統(tǒng)軟件 十堰ai電話機(jī)器人效果怎么樣

前言

即將用到爬蟲,于是打算收集一下User Agent(UA)數(shù)據(jù)。接著馬上想到自己網(wǎng)站的訪問日志不就是現(xiàn)成的優(yōu)質(zhì)數(shù)據(jù)源嗎?于是愉快的決定寫個(gè)腳本統(tǒng)計(jì)一下Nginx訪問日志中的UA信息。

這類簡(jiǎn)單操作,用腳本語(yǔ)言就足夠,毫無疑問肯定要用最熟悉的PHP。打開vim就開擼,十幾分鐘下來,功能簡(jiǎn)單的統(tǒng)計(jì)腳本就搞定了。

腳本目前有三個(gè)功能:

1. 找出所有的UA信息并排序; 2. 統(tǒng)計(jì)操作系統(tǒng)數(shù)據(jù); 3. 統(tǒng)計(jì)瀏覽器數(shù)據(jù)。

程序運(yùn)行截圖如下:

1、UA信息

2、操作系統(tǒng)信息

3、瀏覽器

用腳本統(tǒng)計(jì)最近一個(gè)月的訪問日志,得到以下結(jié)果:

  • 搜索引擎爬蟲比較頻繁,每天有好幾千次數(shù)據(jù)訪問;
  • Windows仍是份額最大的操作系統(tǒng),Linux桌面依然份額很??;
  • Chrome目前是瀏覽器領(lǐng)域的霸主,其次是Firefox,Opera已經(jīng)很小眾了。

最后附上PHP腳本的代碼,也可以從本人的Github里找到:https://github.com/tlanyan/Scripts/blob/master/statUA.php

#!/usr/bin/php
?php
/**
 * @brief stat UA in access log
 *
 * @author tlanyantlanyan@hotmail.com>
 * @link http://tlanyan.me
 */
/* vim: set ts=4; set sw=4; set ss=4; set expandtab; */
function getFileList(string $path) : array {
 return glob(rtrim($path, "/") . "/*access.log*");
}
function statFiles(array $files) : array {
 $stat = [];
 echo PHP_EOL, "start to read files...", PHP_EOL;
 foreach ($files as $file) {
  echo "read file: $file ...", PHP_EOL;
  $contents = getFileContent($file);
  foreach ($contents as $line) {
   $ua = getUA($line);
   if (isset($stat[$ua])) {
    $stat[$ua] += 1;
   } else {
    $stat[$ua] = 1;
   }
  }
 }
 echo "stat all files done!", PHP_EOL, PHP_EOL;
 return $stat;
}
function getFileContent(string $file) : array {
 if (substr($file, -3, 3) === ".gz") {
  return gzfile($file);
 }
 return file($file);
}
function getUA(string $line) : ?string {
 // important! Nginx log format determins the UA location in the line!
 // You may have to refactor following codes to get the right result
 // UA starts from fifth double quote 
 $count = 0; $offset = 0;
 while ($count  5) {
  $pos = strpos($line, '"', $offset);
  if ($pos === false) {
   echo "Error! Unknown line: $line", PHP_EOL;
   return null;
  }
  $count ++;
  $offset = $pos + 1;
 }
 $end = strpos($line, '"', $offset);
 return substr($line, $offset, $end - $offset);
}
function usage() {
 echo "Usage: php statUA.php [option] [dir]", PHP_EOL;
 echo " options:", PHP_EOL;
 echo " -h: show this help", PHP_EOL;
 echo " -v: verbose mode", PHP_EOL;
 echo "-n NUM: UA list number", PHP_EOL;
 echo " dir: directory to the log files", PHP_EOL;
 echo PHP_EOL;
}
function filterUA(array $stat, array $UAFilters) {
 $filterCount = 0;
 foreach ($UAFilters as $filter) {
  foreach ($stat as $ua => $count) {
   if (stripos($ua, $filter) !== false) {
    $filterCount += $count;
    unset($stat[$ua]);
   }
  }
 }
 echo "filter $filterCount records!", PHP_EOL;
}
function printCount(array $stat) {
 $sum = array_sum($stat);
 foreach ($stat as $key => $count) {
  echo $key, " : ", $count, ", percent: ", sprintf("%.2f", 100*$count/$sum), PHP_EOL;
 }
}
function statOS(array $UAs) : array {
 global $debug;
 echo PHP_EOL, "stat OS...", PHP_EOL;
 $os = ["Windows", "MacOS", "Linux", "Android", "iOS", "other"];
 $stat = array_fill_keys($os, 0);
 foreach ($UAs as $key => $count) {
  if (strpos($key, "Windows") !== false) {
   $stat["Windows"] += $count;
  } else if (strpos($key, "Macintosh") !== false) {
   $stat["MacOS"] += $count;
  // must deal Android first, then Linux
  } else if (strpos($key, "Android") !== false) {
   $stat["Android"] += $count;
  } else if (strpos($key, "Linux") !== false) {
   $stat["Linux"] += $count;
  } else if (strpos($key, "iPhone") !== false || strpos($key, "iOS") !== false || strpos($key, "like Mac OS") !== false || strpos($key, "Darwin") !== false) {
   $stat["iOS"] += $count;
  } else {
   if ($debug) {
    echo "other: $key, count: $count", PHP_EOL;
   }
   $stat["other"] += $count;
  }
 }
 return $stat;
}
function statBrowser(array $UAs) : array {
 global $debug;
 echo PHP_EOL, "stat brwoser...", PHP_EOL;
 $browsers = ["Chrome", "Firefox", "IE", "Safari", "Edge", "Opera", "other"];
 $stat = array_fill_keys($browsers, 0);
 foreach ($UAs as $key => $count) {
  if (strpos($key, "MSIE") !== false) {
   $stat["IE"] += $count;
  } else if (strpos($key, "Edge") !== false) {
   $stat["Edge"] += $count;
  } else if (strpos($key, "Firefox") !== false) {
   $stat["Firefox"] += $count;
  } else if (strpos($key, "OPR") !== false) {
   $stat["Opera"] += $count;
  // first Chrome, then Safari
  } else if (strpos($key, "Chrome") !== false) {
   $stat["Chrome"] += $count;
  } else if (strpos($key, "Safari") !== false) {
   $stat["Safari"] += $count;
  } else {
   if ($debug) {
    echo "other: $key, count: $count", PHP_EOL;
   }
   $stat["other"] += $count;
  }
 }
 return $stat;
}
function parseCmd() {
 global $debug, $num, $path, $argc, $argv;
 $optind = null;
 $options = getopt("hvn:", [], $optind);
 if ($argc > 2  empty($options)) {
  usage();
  exit(1);
 }
 if (isset($options['h'])) {
  usage();
  exit(0);
 }
 if (isset($options['v'])) {
  $debug = true;
 }
 if (isset($options['n'])) {
  $num = intval($options['n']);
  if ($num = 0) {
   $num = 10;
  }
 }
 if ($argc === 2  empty($options)) {
  $path = $argv[1];
 }
 if ($argc > $optind) {
  $path = $argv[$optind];
 }
 if (!is_dir($path)) {
  echo "invalid directory: $path", PHP_EOL;
  exit(1);
 }
 if ($debug) {
  echo "num: $num", PHP_EOL;
  echo "verbose: ", var_export($debug, true), PHP_EOL;
  echo "path: $path", PHP_EOL;
 }
}
if (version_compare(PHP_VERSION, "7.1")  0) {
 exit("scripts require PHP >=7.1");
}
$path = ".";
$debug = false;
$num = 10;
$UAFilters = [
 "spider",
 "bot",
 "wget",
 "curl",
];
parseCmd();
$files = getFileList($path);
if (empty($files)) {
 echo '"' . realpath($path) . '" does not contain access log files.', PHP_EOL;
 exit(0);
}
$allUA = statFiles($files);
if (empty($allUA)) {
 echo "no data", PHP_EOL;
 exit(0);
}
filterUA($allUA, $UAFilters);
// sort array with count
uasort($allUA, function ($a, $b) {
 return $b - $a;
});
if ($debug) {
 print_r($allUA);
}
echo PHP_EOL, "---- top $num UA ----", PHP_EOL;
printCount(array_slice($allUA, 0, $num));
echo "-------------------", PHP_EOL;
$os = statOS($allUA);
echo PHP_EOL, "os count:", PHP_EOL;
printCount($os);
$browser = statBrowser($allUA);
echo PHP_EOL, "browser count:", PHP_EOL;
printCount($browser);

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

您可能感興趣的文章:
  • 詳解php+nginx 服務(wù)發(fā)生500 502錯(cuò)誤排查思路
  • 深入分析nginx+php-fpm服務(wù)HTTP狀態(tài)碼502
  • 詳解nginx+php執(zhí)行請(qǐng)求的工作原理
  • php和nginx交互實(shí)例講解

標(biāo)簽:荊門 遂寧 常州 徐州 吐魯番 武威 遵義 寧夏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用PHP如何統(tǒng)計(jì)Nginx日志的User Agent數(shù)據(jù)》,本文關(guān)鍵詞  利用,PHP,如何,統(tǒng)計(jì),Nginx,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用PHP如何統(tǒng)計(jì)Nginx日志的User Agent數(shù)據(jù)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于利用PHP如何統(tǒng)計(jì)Nginx日志的User Agent數(shù)據(jù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    潮州市| 阿拉善右旗| 铁岭市| 平武县| 达拉特旗| 巨野县| 阳谷县| 杭州市| 上饶县| 襄樊市| 长治县| 班戈县| 赤壁市| 长丰县| 保定市| 长白| 兖州市| 溧水县| 福州市| 霸州市| 德阳市| 弥渡县| 和平区| 通化县| 乾安县| 江西省| 河源市| 若尔盖县| 徐水县| 叶城县| 呼伦贝尔市| 定远县| 裕民县| 揭阳市| 承德县| 南丹县| 阳曲县| 泉州市| 滦平县| 株洲市| 泽库县|