forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx.logs.py
More file actions
executable file
·131 lines (104 loc) · 3.97 KB
/
Copy pathnginx.logs.py
File metadata and controls
executable file
·131 lines (104 loc) · 3.97 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
#!/usr/bin/env python
"""
Reads through an nginx access log file and extracts a count of http status codes.
To enable calculation of response times you need to enable more logging in Nginx. Add this to nginx.conf to the http block:
log_format timed_combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$request_time"';
Then in your log directives use time_combined. For example:
access_log /var/log/nginx/yourdomain.com.access.log timed_combined;
"""
import re
import os
import sys
TMPDIR = '/opt/dataloop/tmp'
## Change these two variables if you are duplicating this script. Otherwise you will overwrite the filepointer held in the tmpfile.
TMPFILE = 'dl-nginx-access'
LOGFILE = '/var/log/nginx/access.log'
combined = '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'
timed_combined = '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$request_time"'
status_codes={'2xx': 0,
'3xx': 0,
'4xx': 0,
'5xx': 0}
times = {}
times['count'] = 0
times['total'] = 0
times['max'] = 0
offset = 0
def tmp_file():
# Ensure the dataloop tmp dir is available
if not os.path.isdir(TMPDIR):
os.makedirs(TMPDIR)
if not os.path.isfile(TMPDIR + '/' + TMPFILE):
os.mknod(TMPDIR + '/' + TMPFILE)
def get_offset():
with open(TMPDIR + '/' + TMPFILE, 'r') as off:
try:
offset = int(off.read())
except:
# unable to read offset file
offset = 0
return offset
def write_offset(offset):
with open(TMPDIR + '/' + TMPFILE, 'w') as off:
off.write("%s" % offset)
# Flow
tmp_file()
# Get the last know log file read position
# to mitigate logrotation, if the file is smaller than offset, read the whole
# log file again
position = get_offset()
file_size = os.stat(LOGFILE).st_size
if file_size < position:
position = 0
with open(LOGFILE) as fp:
if position > 0:
fp.seek(position)
for line in fp.xreadlines():
regex = ''.join('(?P<' + g + '>.*?)' if g else re.escape(c) for g, c in re.findall(r'\$(\w+)|(.)', timed_combined))
m = re.match(regex, line)
if not m:
regex = ''.join('(?P<' + g + '>.*?)' if g else re.escape(c) for g, c in re.findall(r'\$(\w+)|(.)', combined))
m = re.match(regex, line)
try:
data = m.groupdict()
except:
print "Plugin Failed!"
sys.exit(2)
code = data['status']
if code not in status_codes.keys():
status_codes[code] = 1
else:
status_codes[code] += 1
if code.startswith('2'):
status_codes['2xx'] += 1
if code.startswith('3'):
status_codes['3xx'] += 1
if code.startswith('4'):
status_codes['4xx'] += 1
if code.startswith('5'):
status_codes['5xx'] += 1
if 'request_time' in data.iterkeys():
time_taken = data['request_time']
if 'min' not in times.iterkeys():
times['min'] = time_taken
times['count'] += 1
times['total'] += float(time_taken)
if time_taken > times['max']:
times['max'] = time_taken
if time_taken < times['min']:
times['min'] = time_taken
write_offset(fp.tell())
fp.close()
# emit lots of lovely metrics:
message = "OK | "
for k,v in status_codes.iteritems():
message += "%s=%s;;;; " % (k,v)
if times['count'] > 0:
message += "avg_time=%0.2fs;;;; " % (float(times['total'])/float(times['count']))
if 'request_time' in data.iterkeys():
message += "max_time=%0.2fs;;;; min_time=%0.2fs;;;;" % (float(times['max']), float(times['min']))
print message
sys.exit(0)