-
Notifications
You must be signed in to change notification settings - Fork 4
/
msys2-logstats
executable file
·461 lines (391 loc) · 15.5 KB
/
msys2-logstats
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/env python3
# Expects traefik json access logs either by passing a log file as the first argument, or via stdin
import json
import re
import sys
import argparse
from datetime import datetime
from collections import Counter
from typing import List, Tuple, Optional
from dataclasses import dataclass
from tabulate import tabulate
import requests_cache
from netaddr import IPSet, IPAddress
@dataclass
class LogEntry:
ClientHost: str
DownstreamStatus: int
RequestHost: str
RequestMethod: str
RequestPath: str
RequestProtocol: str
UserAgent: str
time: str
client_info: "Optional[ClientInfo]"
@dataclass
class UserAgent:
pacman_version: Tuple[int, int, int]
windows_version: Tuple[int, int]
build_number: int
cygwin_arch: str
host_arch: str
libalpm_version: Tuple[int, int, int]
@dataclass
class ClientInfo:
pacman_version: str
windows_edition: str
user_agent: UserAgent
ci: str
def is_valid_user_agent(ua: str) -> bool:
return ua.startswith("pacman") and "_NT-" in ua
def parse_user_agent(ua: str) -> UserAgent:
assert is_valid_user_agent(ua)
m = re.match(r"pacman/([^\s]+) \(([^-]+)-([^-]+)-?([0-9]+|)-?(.+|) ([^)]+)\) libalpm/(.+)", ua)
assert m is not None
pacman_version, _, windows_version, build_number, extra, arch, alpm_version = m.groups()
pacman_version = tuple(map(int, pacman_version.split("-")[0].split(".")))
windows_version = tuple(map(int, windows_version.split(".")))
build_number = int(build_number or "-1")
cygwin_arch = arch
if extra in ("WOW64", "WOW") and cygwin_arch == "i686":
host_arch = "x86_64"
elif extra == "ARM64":
host_arch = "aarch64"
elif extra:
host_arch = "???"
else:
host_arch = cygwin_arch
alpm_version = tuple(map(int, alpm_version.split(".")))
return UserAgent(
pacman_version,
windows_version,
build_number,
cygwin_arch,
host_arch,
alpm_version
)
def test_parse_user_agent():
ua = "pacman/6.0.1 (MSYS_NT-10.0-19042 x86_64) libalpm/13.0.1"
assert parse_user_agent(ua).pacman_version == (6, 0, 1)
ua = "pacman/6.0.1 (MSYS_NT-10.0-19042-WOW64 i686) libalpm/13.0.1"
assert parse_user_agent(ua).cygwin_arch == "i686"
assert parse_user_agent(ua).host_arch == "x86_64"
assert parse_user_agent(ua).build_number == 19042
ua = "pacman/5.2.1 (MSYS_NT-6.1-7601-WOW64 i686) libalpm/12.0.1"
assert parse_user_agent(ua).cygwin_arch == "i686"
assert parse_user_agent(ua).host_arch == "x86_64"
assert parse_user_agent(ua).build_number == 7601
ua = "pacman/5.1.0 (MINGW64_NT-10.0 x86_64) libalpm/11.0.0"
assert parse_user_agent(ua).build_number == -1
assert parse_user_agent(ua).cygwin_arch == "x86_64"
assert parse_user_agent(ua).host_arch == "x86_64"
ua = "pacman/5.0.1 (UCRT64_NT-10.0-WOW i686) libalpm/10.0.1"
assert parse_user_agent(ua).build_number == -1
assert parse_user_agent(ua).host_arch == "x86_64"
assert parse_user_agent(ua).cygwin_arch == "i686"
ua = "pacman/4.2.1-313-g5535-dirty (MINGW64_NT-6.1 x86_64) libalpm/9.0.1"
assert parse_user_agent(ua).pacman_version == (4, 2, 1)
ua = "pacman/6.0.1 (MSYS_NT-6.0-6002 x86_64) libalpm/13.0.1"
assert parse_user_agent(ua).windows_version == (6, 0)
ua = "pacman/6.1.0 (MSYS_NT-10.0-22631-ARM64 x86_64) libalpm/14.0.0"
assert parse_user_agent(ua).host_arch == "aarch64"
assert parse_user_agent(ua).cygwin_arch == "x86_64"
ua = "pacman/6.1.0 (MSYS_NT-10.0-22631-ARM64 i686) libalpm/14.0.0"
assert parse_user_agent(ua).host_arch == "aarch64"
assert parse_user_agent(ua).cygwin_arch == "i686"
def test_get_windows_edition():
ua = "pacman/6.0.1 (MSYS_NT-6.0-6002 x86_64) libalpm/13.0.1"
assert get_windows_edition(parse_user_agent(ua)) == "Vista"
ua = "pacman/4.2.1-463-gbb493-dirty (MINGW32_NT-5.0 i686) libalpm/10.0.0"
assert get_windows_edition(parse_user_agent(ua)) == "2000"
def get_windows_edition(ua: UserAgent):
if ua.windows_version >= (10, 0):
if ua.build_number >= 22000:
return "11"
else:
return "10"
elif ua.windows_version == (5, 0):
return "2000"
elif ua.windows_version == (5, 1):
return "XP"
elif ua.windows_version == (5, 2):
return "XP"
elif ua.windows_version == (6, 0):
return "Vista"
elif ua.windows_version == (6, 1):
return "7"
elif ua.windows_version == (6, 2):
return "8"
elif ua.windows_version == (6, 3):
return "8.1"
else:
assert 0, ua
def get_ci_networks():
session = requests_cache.CachedSession(
'msys2-logstats-ci', expire_after=360, use_cache_dir=True)
r = session.get("https://api.github.com/meta")
r.raise_for_status()
gha = IPSet(r.json()["actions"])
r = session.get("https://www.appveyor.com/ips.json")
r.raise_for_status()
appveyor = IPSet(r.json())
r = session.get("https://www.gstatic.com/ipranges/cloud.json")
r.raise_for_status()
prefixes = set()
for prefix in r.json()["prefixes"]:
prefixes.add(prefix.get("ipv4Prefix", prefix.get("ipv6Prefix")))
gcp = IPSet(prefixes)
r = session.get("https://ip-ranges.amazonaws.com/ip-ranges.json")
r.raise_for_status()
prefixes = set()
for prefix in r.json()["prefixes"]:
prefixes.add(prefix["ip_prefix"])
for prefix in r.json()["ipv6_prefixes"]:
prefixes.add(prefix["ipv6_prefix"])
aws = IPSet(prefixes)
r = session.get(
"https://www.microsoft.com/en-us/download/details.aspx?id=56519",
headers={
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0'
}
)
r.raise_for_status()
match = re.search("(https://download.microsoft.com/.*?/ServiceTags_Public_.*?\\.json)", r.text)
r = session.get(match.groups(0)[0])
r.raise_for_status()
prefixes = set()
for value in r.json()["values"]:
for nw in value["properties"]["addressPrefixes"]:
prefixes.add(nw)
azure = IPSet(prefixes)
return {"GHA": gha, "APPV": appveyor, "GCP": gcp, "AWS": aws, "AZ": azure}
def get_repo_for_path(path: str) -> str:
repo = path.rsplit("/", 1)[0].lstrip("/")
if repo == "mingw/i686":
repo = "mingw/mingw32"
elif repo == "mingw/x86_64":
repo = "mingw/mingw64"
return repo
def get_type_for_path(path: str) -> str:
if path.endswith(".db") or ".db." in path:
return "db"
elif path.endswith(".files") or ".files." in path:
return "db"
else:
return "pkg"
def print_repos(entries, show_ci):
for request_type in ["pkg", "db"]:
type_requests = [e for e in entries if get_type_for_path(e.RequestPath) == request_type]
table = []
for (repo, type_, ci), count in Counter([
(get_repo_for_path(e.RequestPath), get_type_for_path(e.RequestPath),
e.client_info.ci) for e in type_requests]).most_common():
pcnt = count / len(type_requests) * 100
line = [repo, type_, ci, f"{pcnt:.2f}%", f"{count}"]
if not show_ci:
line.pop(2)
table.append(line)
headers = ["Repo", "Type", "CI", "% Requests", "Requests"]
if not show_ci:
headers.pop(2)
print()
print(tabulate(table, headers, stralign="right", numalign="right"))
def print_windows_major(clients, entries, show_ci):
per_request = {}
for (edition, ci), count in Counter([(e.client_info.windows_edition, e.client_info.ci) for e in entries]).most_common():
per_request[(edition, ci)] = count
table = []
for (edition, ci), count_clients in Counter([(u.windows_edition, u.ci) for u in clients]).most_common():
pcnt_clients = count_clients / len(clients) * 100
count_req = per_request[(edition, ci)]
pcnt_req = count_req / len(entries) * 100
line = [edition, ci, f"{pcnt_clients:.2f}%", f"{count_clients}", f"{pcnt_req:.2f}%", f"{count_req}"]
if not show_ci:
line.pop(1)
table.append(line)
headers = ["Windows", "CI", "% Clients", "Clients", "% Requests", "Requests"]
if not show_ci:
headers.pop(1)
print()
print(tabulate(table, headers, stralign="right", numalign="right"))
def print_ci_systems(clients, entries):
per_request = {}
for ci, count in Counter([e.client_info.ci for e in entries]).most_common():
per_request[ci] = count
table = []
for ci, count_clients in Counter([u.ci for u in clients]).most_common():
pcnt_clients = count_clients / len(clients) * 100
count_req = per_request[ci]
pcnt_req = count_req / len(entries) * 100
line = [ci, f"{pcnt_clients:.2f}%", f"{count_clients}", f"{pcnt_req:.2f}%", f"{count_req}"]
table.append(line)
headers = ["CI", "% Clients", "Clients", "% Requests", "Requests"]
print()
print(tabulate(table, headers, stralign="right", numalign="right"))
def print_windows_version_details(clients, show_ci):
table = []
for (windows_version, build_number, ci), count in Counter(
[(u.user_agent.windows_version, u.user_agent.build_number, u.ci) for u in clients]).most_common():
pcnt = count / len(clients) * 100
line = [".".join(map(str, windows_version)), build_number, ci, f"{pcnt:.2f}%", f"{count}"]
if not show_ci:
line.pop(2)
table.append(line)
headers = ["Win Ver", "Build Number", "CI", "% Clients", "Clients"]
if not show_ci:
headers.pop(2)
print()
print(tabulate(table, headers, stralign="right", numalign="right"))
def print_pacman(clients, show_ci):
table = []
for (version, ci), count in Counter([(u.pacman_version, u.ci) for u in clients]).most_common():
pcnt = count / len(clients) * 100
line = [version, ci, f"{pcnt:.2f}%", f"{count}"]
if not show_ci:
line.pop(1)
table.append(line)
headers = ["Pacman Ver", "CI", "% Clients", "Clients"]
if not show_ci:
headers.pop(1)
print()
print(tabulate(table, headers, stralign="right", numalign="right"))
def print_system_arch(clients, show_ci):
table = []
for (cygwin_arch, host_arch, ci), count in Counter([(u.user_agent.cygwin_arch, u.user_agent.host_arch, u.ci) for u in clients]).most_common():
pcnt = count / len(clients) * 100
line = [cygwin_arch, host_arch, ci, f"{pcnt:.2f}%", f"{count}"]
if not show_ci:
line.pop(2)
table.append(line)
headers = ["Cygwin Arch", "Host Arch", "CI", "% Clients", "Clients"]
if not show_ci:
headers.pop(2)
print()
print(tabulate(table, headers, stralign="right", numalign="right"))
def datetime_fromisoformat(value: str) -> datetime:
# For Python <3.11
if value.endswith("Z"):
value = value[:-1] + "+00:00"
return datetime.fromisoformat(value)
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='?', type=argparse.FileType('r', encoding="utf-8"), default=sys.stdin)
parser.add_argument('--show-ci', action='store_true', help='show CI/cloud providers')
parser.add_argument('--skip-ci', action='store_true', help='skip CI/cloud IP ranges')
parser.add_argument('--only-ci', action='store_true', help='only CI/cloud IP ranges')
parser.add_argument('--show-summary', action='store_true', help='show only a CI/cloud summary')
args = parser.parse_args(argv[1:])
assert not (args.skip_ci and args.only_ci)
detect_ci = False
if args.show_summary:
assert not args.skip_ci
assert not args.only_ci
args.show_ci = True
detect_ci = True
if args.skip_ci or args.only_ci:
detect_ci = True
if detect_ci:
ci_networks = get_ci_networks()
entries: List[LogEntry] = []
decoder = json.JSONDecoder()
with args.infile as h:
for line in h:
if not line.startswith("{"):
continue
decoded = decoder.decode(line)
if "RequestHost" not in decoded or "request_User-Agent" not in decoded:
continue
entry = LogEntry(
decoded["ClientHost"],
decoded["DownstreamStatus"],
decoded["RequestHost"],
decoded["RequestMethod"],
decoded["RequestPath"],
decoded["RequestProtocol"],
decoded["request_User-Agent"],
decoded["time"],
None,
)
if entry.RequestHost not in ["repo.msys2.org", "mirror.msys2.org"]:
continue
if entry.RequestMethod != "GET":
continue
if not is_valid_user_agent(entry.UserAgent):
continue
entries.append(entry)
def user_key(entry):
# only take the system part of the UA, so we don't count
# a client twice if it upgrades during a job for example
system_id = "".join(entry.UserAgent.split()[1:3])
return system_id + entry.ClientHost
first = entries[0].time
last = entries[0].time
grouped = {}
for entry in entries:
if entry.time < first:
first = entry.time
if entry.time > last:
last = entry.time
key = user_key(entry)
grouped.setdefault(key, []).append(entry)
ip_to_ci = {}
if detect_ci:
def get_ip_to_ci(ip_addr: str) -> str:
ip = IPAddress(ip_addr)
for name, ipset in ci_networks.items():
if ip in ipset:
return name
return ""
for group_entries in grouped.values():
ip = group_entries[0].ClientHost
ip_to_ci[ip] = get_ip_to_ci(ip)
clients: List[ClientInfo] = []
for group_entries in grouped.values():
user_agent = parse_user_agent(group_entries[0].UserAgent)
ci = ip_to_ci.get(group_entries[0].ClientHost, "")
client_info = ClientInfo(
".".join(map(str, user_agent.pacman_version)),
get_windows_edition(user_agent),
user_agent,
ci
)
clients.append(client_info)
for e in group_entries:
e.client_info = client_info
if args.skip_ci:
entries = [e for e in entries if not ip_to_ci.get(e.ClientHost, "")]
clients = [c for c in clients if not c.ci]
if args.only_ci:
entries = [e for e in entries if ip_to_ci.get(e.ClientHost, "")]
clients = [c for c in clients if c.ci]
# Log info
diff = datetime_fromisoformat(last) - datetime_fromisoformat(first)
duration = (diff).total_seconds()
requests_per_second = len(entries) / duration
print(tabulate([
["Duration", f"from {first} to {last} ({diff})"],
["Requests", f"{len(entries)} ({requests_per_second:.2f}/s)"],
["Clients", f"{len(clients)} (clients are grouped by IP+WinVer+Arch)"],
["Included", "CI only" if args.only_ci else "non-CI only" if args.skip_ci else "all"],
]))
# Repos
if not args.show_summary:
print_repos(entries, args.show_ci)
# CI Systems
if args.show_ci:
print_ci_systems(clients, entries)
# Windows versions
if not args.show_summary:
print_windows_major(clients, entries, args.show_ci)
# Windows versions detailed
if not args.show_summary:
print_windows_version_details(clients, args.show_ci)
# Pacman
if not args.show_summary:
print_pacman(clients, args.show_ci)
# CPU Arch
if not args.show_summary:
print_system_arch(clients, args.show_ci)
if __name__ == "__main__":
main(sys.argv)