-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatUA.php
More file actions
237 lines (211 loc) · 6.19 KB
/
Copy pathstatUA.php
File metadata and controls
237 lines (211 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/php
<?php
/**
* @brief stat UA in access log
*
* @author tlanyan<tlanyan@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);