-
Notifications
You must be signed in to change notification settings - Fork 10
/
uwsgi-log
executable file
·357 lines (303 loc) · 11.3 KB
/
uwsgi-log
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
#!/usr/bin/env python3
# uwsgi-log (part of ossobv/vcutil) // wdoekes/2019 // Public Domain
#
# Quick and dirty uWSGI webserver log parser and analyzer. Supports a
# tail function to dump periodic stats into a file to be read by e.g.
# Zabbix monitoring.
#
# Example usage:
#
# # uwsgi-log /var/log/uwsgi/acme-web.log
# (prints out various timing totals)
#
# Example tail setup:
#
# # cat >/etc/systemd/system/[email protected] <<EOF
# [Unit]
# Description=Write uWSGI log stats every minute
#
# [Service]
# ExecStart=ExecStart=/usr/bin/uwsgi-log --tail \
# /var/log/uwsgi/%i.log /tmp/httpreq-%i.stat
# Restart=on-failure
#
# [Install]
# WantedBy=multi-user.target
# EOF
#
# # systemctl start [email protected]
#
# Example zabbix setup:
#
# # Example call httpreq[acme-web,max]
# # Available keys: count, min, max, avg, med
# UserParameter=httpreq[*], awk '/^$2:/{print $$2}' /tmp/httpreq-$1.stat
#
# TODO:
#
# - Should we unlink the stats file when stopping? May be better for
# monitoring. (No data being more accurate than stale data.)
# - Refactor write_summary_every_x, avoiding duplicate code (and break
# it up into smaller functions).
# - Use argparse for the argument parsing.
#
from collections import defaultdict
from stat import S_ISREG
from time import sleep, time
import os
import re
import sys
# log-format = [pid: %(pid)|app: -|req: -/-] %(var.HTTP_X_REAL_IP) (%(user))
# {%(vars) vars in %(pktsize) bytes} [%(ctime)] %(method) %(uri) => generated
# %(rsize) bytes in %(msecs) msecs (%(proto) %(status)) %(headers) headers in
# %(hsize) bytes (%(switches) switches on core %(core)) (proxy=%(addr))
EXAMPLE_LINES = ('''\
[pid: 1312|app: 0|req: 108304/1211400] 10.70.22.29 () {48 vars in 726 bytes} \
[Wed Sep 25 06:25:18 2019] \
GET /api/v1/general/learnmore \
=> generated 0 bytes in 121 msecs \
(HTTP/1.0 401) 6 headers in 314 bytes (2 switches on core 0)''',)
EXAMPLE_MATCHES = (
{'ip': '10.70.22.29', 'method': 'GET',
'request': '/api/v1/general/learnmore',
'bytes': '0', 'msecs': '121', 'httpstatus': 'HTTP/1.0 401'},
)
LINE_MATCHER = re.compile('''\
^\\[[^]]*\\] (?P<ip>[0-9.]+) \\(-?\\) \\{[^}]*\\} \
\\[[^]]*\\] \
(?P<method>\\S+) (?P<request>\\S+) \
=> generated (?P<bytes>\\d+) bytes in (?P<msecs>\\d+) msecs \
\\((?P<httpstatus>HTTP[^ ]+ \\d+)\\)''')
for line, match in zip(EXAMPLE_LINES, EXAMPLE_MATCHES):
found_match = LINE_MATCHER.match(line)
assert found_match, line
dict_match = found_match.groupdict()
assert dict_match == match, (line, dict_match)
class Record:
def __init__(self, d):
self.ip = d['ip']
self.method = d['method']
self.request = d['request']
self.query_string = ''
if '?' in self.request:
self.request, self.query_string = self.request.split('?', 1)
self.bytes = int(d['bytes'])
self.msecs = int(d['msecs'])
self.http_ver, self.http_code = d['httpstatus'].split(' ')
self.http_code = int(self.http_code)
def __repr__(self):
return (
'<Record({o.method} {o.request} '
'status={o.http_code} time={o.msecs})>'
.format(o=self))
class Timing:
def __init__(self, points):
if not points:
self.min = self.max = self.median = self.total = self.average = 0
return
points = list(sorted(points))
self.min = points[0]
self.max = points[-1]
if len(points) % 2 == 0:
self.median = (sum(
points[(len(points) // 2):(len(points) // 2 + 2)]) + 1) // 2
else:
self.median = points[(len(points) // 2)]
self.total = sum(points)
self.average = (self.total + 1) // len(points)
class Records:
def __init__(self):
self._list = list()
@property
def msec(self):
if not hasattr(self, '_msec'):
self._msec = Timing([i.msecs for i in self._list])
return self._msec
def add(self, record):
self._list.append(record)
def __eq__(self, other):
return id(self) == id(other)
def __lt__(self, other):
if self == other:
return False
return len(self) < len(other)
def __len__(self):
return len(self._list)
def __str__(self):
return (
'{len}x {o.msec.median}med {o.msec.average}avg '
'{o.msec.min}min {o.msec.max}max'
.format(len=len(self), o=self))
class BasicSummary:
def __init__(self):
self.all = Records()
def add(self, record):
self.all.add(record)
class AdvancedSummary(BasicSummary):
def __init__(self):
super().__init__()
self.by_method = defaultdict(Records)
self.by_request = defaultdict(Records)
def add(self, record):
super().add(record)
self.by_method['{} {}'.format(record.method, record.http_code)].add(
record)
self.by_request['{} {} {}'.format(
record.method, record.http_code, record.request)].add(
record)
def display_all(self):
self.display_method_and_status()
self.display_common_requests()
self.display_median_times()
self.display_total_times()
self.display_slow_requests()
def display_method_and_status(self):
print(self.format_summary('methods', self.by_method))
def display_common_requests(self):
print(self.format_summary(
'by_request, by count (top 12)',
self.by_request, top=12,
sort_key=(lambda kr: (-len(kr[1]), kr[0]))))
def display_median_times(self):
print(self.format_summary(
'by_request, by median (>1000 hits, >100 median)',
self.by_request,
sort_key=(lambda kr: (-kr[1].msec.median, kr[0])),
filter_key=(
lambda kr: len(kr[1]) > 1000 and kr[1].msec.median > 100)))
def display_total_times(self):
print(self.format_summary(
'by_request, by total time (top 20)',
self.by_request, top=20,
sort_key=(lambda kr: (-kr[1].msec.total, kr[0]))))
def display_slow_requests(self):
print(self.format_summary(
'by_request, very slow',
self.by_request,
sort_key=(lambda kr: (-kr[1].msec.max, kr[0])),
filter_key=(lambda kr: kr[1].msec.max > 10000)))
@staticmethod
def format_summary(
title, source=None, top=None, min_results=1,
sort_key=(lambda kr: kr[0]), filter_key=(lambda kr: True)):
filtered = list(filter(filter_key, source.items()))
filtered.sort(key=sort_key)
ret = ['{}:'.format(title)]
idx = 0
for key, records in filtered:
if len(records) >= min_results:
ret.append(' {key}: {records}'.format(
key=key, records=records))
idx += 1
if top and idx == top:
break
ret.append('')
return '\n'.join(ret)
def show_single_summary(filename):
summ = AdvancedSummary()
with open(sys.argv[1]) as fp:
for idx, line in enumerate(fp):
# The log includes more than just the log lines, it may also have
# backtraces.
if not line.startswith('[pid: '):
continue
# Extract record and add to aggregator.
try:
r = Record(LINE_MATCHER.match(line).groupdict())
except Exception:
print('error on {idx}: {line}'.format(
idx=(idx + 1), line=line), file=sys.stderr)
raise
summ.add(r)
summ.display_all()
def dont_rape_non_regular_file(filename):
"""
Quick function to prevent someone passing /dev/stdout as argument.
That would make us destroy it, which is not a good idea.
"""
try:
st = os.lstat(filename)
except FileNotFoundError:
pass
else:
if not S_ISREG(st.st_mode):
raise ValueError('unexpected non-regular file {}'.format(filename))
def write_summary_every_x(log_filename, summary_filename, time_seconds):
timeslice = time_seconds
minute = time() // timeslice
fp = open(log_filename)
statname = summary_filename
statname_tmp = statname + '.new'
dont_rape_non_regular_file(statname)
dont_rape_non_regular_file(statname_tmp)
summ = BasicSummary()
try:
fp.seek(0, 2) # seek_end
# Loop every timeslice.
while True:
# Loop the entire timeslice.
while True:
if minute != (time() // timeslice):
break
for idx, line in enumerate(fp):
# The log includes more than just the log lines, it
# may also have backtraces.
if not line.startswith('[pid: '):
continue
# Extract record and add to aggregator.
try:
r = Record(LINE_MATCHER.match(line).groupdict())
except Exception:
print('error on {idx}: {line}'.format(
idx=(idx + 1), line=line), file=sys.stderr)
raise
summ.add(r)
sleep(1)
with open(statname_tmp, 'w') as out:
print(
'time:\t{time}\nslice:\t{timeslice}\ncount:\t{count}\n'
'min:\t{o.min}\nmax:\t{o.max}\n'
'avg:\t{o.average}\nmed:\t{o.median}\n'.format(
time=int(minute * timeslice), timeslice=timeslice,
count=len(summ.all), o=summ.all.msec), file=out)
dont_rape_non_regular_file(statname)
os.rename(statname_tmp, statname)
# Get fresh records and check if file needs to be reopened.
summ = BasicSummary()
minute = time() // timeslice
try:
open_stat = os.fstat(fp.fileno())
disk_stat = os.stat(fp.name)
if open_stat.st_ino != disk_stat.st_ino:
raise OSError('renamed?')
if fp.tell() > open_stat.st_size:
raise OSError('truncated?')
except OSError:
name = fp.name
fp.close()
while True:
try:
fp = open(name)
except OSError:
sleep(1)
else:
fp.seek(0, 2) # seek_end
break
finally:
fp.close()
if sys.argv[1:2] == ['--tail']:
# Will write a summary of the request times found in the LOG_SOURCE
# to a stats file, named SUMMARY_DEST. For example:
# uwsgi-log --tail /var/log/uwsgi/app/acme-web.log /tmp/acme-web.stat &
# tail -F /tmp/acme-web.stat
assert len(sys.argv) == 4, (
'Usage: {} --tail LOG_SOURCE SUMMARY_DEST'.format(sys.argv[0]))
write_summary_every_x(sys.argv[2], sys.argv[3], 60)
elif len(sys.argv) == 2 and not sys.argv[1].startswith('-'):
show_single_summary(sys.argv[1])
else:
print(
'Usage: {argv0} LOG_SOURCE\n'
'Usage: {argv0} --tail LOG_SOURCE SUMMARY_DEST'.format(
argv0=sys.argv[0]), file=sys.stderr)
sys.exit(1)