-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogservatory.py
More file actions
336 lines (298 loc) · 13 KB
/
logservatory.py
File metadata and controls
336 lines (298 loc) · 13 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
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
import os, re, sys, getopt, sqlite3, math, csv
from datetime import datetime
from dateutil.parser import parse
#from csv import writer
# default args / global variables:
index = '' # path to log index file (for mode=logs)
start = '' # start date or timestamp to process (for mode=logs)
end = '' # end date or timestamp to process (for mode=logs)
format = 'aws-elb-classic' # or 'aws-elb-application', 'ncsa-common', 'ncsa-combined', 'elf'
queries_file = '' # path to file with queries to run
output = '' # path to directory for query output (queryN.csv)
sample = 1.0 # what fraction of logs to sample and query over
buffer_size = 100 # how large to let the log buffer get before ingesting it into SQLite
memory = 100000000 # maximum amount of memory (in bytes) the system can use
period = 60 # how many seconds to wait between successive query runs
encoding = 'utf-8'
queries = []
buffer = []
fields = []
connection = ''
def parse_args(mode='live'):
global index, start, end, format, queries_file, output, sample, buffer_size, memory, period, queries, encoding
if mode=='static':
opts_array = ["index=", "start=", "end=", "format=", "queries=", "output=", "buffer=", "memory=", "period=", "sample=", "encoding="]
else:
opts_array = ["format=", "queries=", "output=", "buffer=", "memory=", "period=", "sample=", "encoding="]
# parse cli args:
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, "", opts_array)
except:
print("Error")
exit()
for opt, arg in opts:
if opt in ['--index']:
index = arg
elif opt in ['--start']:
start = arg
elif opt in ['--end']:
end = arg
elif opt in ['--format']:
format = arg
elif opt in ['--queries']:
queries_file = arg
elif opt in ['--output']:
output = arg
elif opt in ['--buffer']:
buffer_size = arg
elif opt in ['--memory']:
memory = arg
elif opt in ['--period']:
period = arg
elif opt in ['--sample']:
sample = arg
elif opt in ['--encoding']:
encoding = arg
def validate_args(mode='live'):
global index, start, end, format, queries_file, output, sample, buffer_size, memory, period, queries
# validate args:
if format!='aws-elb-classic' and format!='aws-elb-application' and format!='ncsa-common' and format!='ncsa-combined':
print('Argument "format" (the log format) must be one of "aws-elb-classic", "aws-elb-application", "ncsa-common", "ncsa-combined", or "elf". See the documentation for details.')
exit()
#if mode=='logs' and index file doesn't exist:
# print('Argument "index" must be a valid log index file. You can create one with build-log-index.py.')
# exit()
try:
f = open(queries_file, "r")
fl = f.readlines()
f.close()
q = ''
for x in fl:
if x.strip(" \n\t\r")=='##########':
queries.append(q.strip(" \n\t\r"))
q = ''
elif x[:1]=='#':
continue
else:
q = q + ' ' + x
if q!='':
queries.append(q.strip(" \n\t\r"))
except Exception as e:
print(e)
print('Argument "queries" must be a valid query file. See the documentation for format details.')
exit()
if not os.path.isdir(output):
print('Argument "output" must be a directory. Query results will output here, one CSV file per query.')
exit()
try:
buffer_size = int(buffer_size)
except:
print('Argument "buffer" (the buffer size) must be an integer.')
exit()
if buffer_size<=0:
print('Argument "buffer" (the buffer size) must be a positive integer.')
exit()
try:
memory = int(memory)
except:
print('Argument "memory" (the memory limit) must be an integer.')
exit()
if memory<=0:
print('Argument "memory" (the memory limit) must be a positive integer.')
exit()
try:
period = int(period)
except:
print('Argument "period" (the processing period) must be an integer.')
exit()
if period<=0:
print('Argument "period" (the processing period) must be a positive integer.')
exit()
if start!='':
try:
start = datetime.timestamp(parse(start))
except:
print('Argument "start" (processing start date) could not be parsed.')
exit()
if end!='':
try:
end = datetime.timestamp(parse(end))
except:
print('Argument "end" (processing end date) could not be parsed.')
exit()
try:
sample = float(sample)
except:
print('Argument "sample" (the sample rate) must be a floating point number. To query over all logs (and not sample), leave out this Argument.')
exit()
if sample<=0 or sample>1:
print('Argument "sample" (the sample rate) must be a float between 0 and 1. To query over all logs (and not sample), leave out this Argument.')
exit()
def start_database():
global connection, format, fields
connection = sqlite3.connect(':memory:')
c = connection.cursor()
# 1. create logs table based on log format
if format=='aws-elb-classic':
fields = ["timestamp", "elb", "client_ip", "client_port", "backend_ip", "backend_port",
"request_processing_time", "backend_processing_time", "response_processing_time",
"request_status_code", "backend_status_code", "received_bytes", "sent_bytes",
"request_verb", "request_url", "request_protocol", "user_agent",
"ssl_cipher", "ssl_protocol",
]
c.execute("""CREATE TABLE IF NOT EXISTS logs (
timestamp int, elb_name string, request_ip string, request_port int, backend_ip string, backend_port int,
request_processing_time double, backend_processing_time double, client_response_time double,
request_status_code string, backend_status_code string, received_bytes bigint, sent_bytes bigint,
request_verb string, request_url string, request_protocol string, user_agent string, ssl_cipher string,
ssl_protocol string) """)
if format=='aws-elb-application':
fields = ["type", "timestamp", "elb", "client_ip", "client_port", "backend_ip", "backend_port",
"request_processing_time", "backend_processing_time", "response_processing_time",
"request_status_code", "backend_status_code", "received_bytes", "sent_bytes",
"request_verb", "request_url", "request_protocol", "user_agent",
"ssl_cipher", "ssl_protocol",
]
c.execute("""CREATE TABLE IF NOT EXISTS logs (
timestamp int, elb_name string, request_ip string, request_port int, backend_ip string, backend_port int,
request_processing_time double, backend_processing_time double, client_response_time double,
request_status_code string, backend_status_code string, received_bytes bigint, sent_bytes bigint,
request_verb string, request_url string, request_protocol string, user_agent string, ssl_cipher string,
ssl_protocol string) """)
if format=='ncsa-common':
fields = ["request_ip", "auth_user", "timestamp", "request_verb", "request_url", "request_protocol",
"request_status_code", "sent_bytes",
]
c.execute("""CREATE TABLE IF NOT EXISTS logs (
request_ip string, auth_user string, timestamp int, request_verb string, request_url string,
request_protocol string, request_status_code string, sent_bytes bigint) """)
if format=='ncsa-combined':
fields = ["request_ip", "auth_user", "timestamp", "request_verb", "request_url", "request_protocol",
"request_status_code", "sent_bytes", "referrer", "user_agent"
]
c.execute("""CREATE TABLE IF NOT EXISTS logs (
request_ip string, auth_user string, timestamp int, request_verb string, request_url string,
request_protocol string, request_status_code string, sent_bytes bigint, referrer string,
user_agent string) """)
c.execute("""CREATE INDEX timestamp_idx ON logs (timestamp)""")
connection.commit()
def load_index():
global connection, index
cur = connection.cursor()
cur.execute("""CREATE TABLE logs_idx (file string, size_bytes bigint,
n_lines bigint, min_ts bigint, max_ts bigint)""")
with open(index,'r') as fin:
dr = csv.DictReader(fin)
to_db = [(i['file'], i['size_bytes'], i['n_lines'], i['min_ts'], i['max_ts']) for i in dr]
cur.executemany("INSERT INTO logs_idx (file, size_bytes, n_lines, min_ts, max_ts) VALUES (?, ?, ?, ?, ?);", to_db)
connection.commit()
def fetch_log_files():
global connection, start, end, sample
cur = connection.cursor()
count_query = "SELECT COUNT(*) FROM logs_idx"
if start!='' and end!='':
count_query += """ WHERE
( min_ts>=""" + str(int(start)) + """ AND max_ts<=""" + str(int(end)) + """ )
OR ( min_ts<=""" + str(int(start)) + """ AND max_ts>=""" + str(int(start)) + """ )
OR ( min_ts<=""" + str(int(end)) + """ AND max_ts>=""" + str(int(end)) + """ ) """
elif start!='':
count_query += " WHERE max_ts>="+str(int(start))
elif end!='':
count_query += " WHERE min_ts<="+str(int(end))
cur.execute(count_query)
rows = cur.fetchall()
num_rows = rows[0][0]
cur = connection.cursor()
if sample<1.0:
query = """SELECT * FROM ( """ + count_query.replace("COUNT(*)","*") + """
ORDER BY RANDOM() LIMIT """ + str(math.ceil(sample*num_rows)) + """
) ORDER BY min_ts ASC, max_ts ASC, file ASC"""
else:
query = count_query.replace("COUNT(*)","*") + " ORDER BY min_ts ASC, max_ts ASC, file ASC"
cur.execute(query)
rows = cur.fetchall()
return rows
def ingest_logs():
global connection, buffer, format, fields, buffer_size
#print('Ingesting batch of '+str(buffer_size)+' lines...')
log_values = []
for log in buffer:
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
if format=='aws-elb-classic':
# REFERENCE: https://docs.aws.amazon.com/athena/latest/ug/application-load-balancer-logs.html#create-alb-table
regex = r'([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) "([^ ]*) ([^ ]*) (- |[^ ]*)" ("[^\"]*") ([A-Z0-9-]+) ([A-Za-z0-9.-]*)$'
elif format=='aws-elb-application':
regex = r'([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) ([^ ]*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\s]+?)\" \"([^\s]+)\" \"([^ ]*)\" \"([^ ]*)\"'
#([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) "([^ ]*) ([^ ]*) (- |[^ ]*)" ("[^\"]*") ([A-Z0-9-]+) ([A-Za-z0-9.-]*)$'
elif format=='ncsa-common':
regex = '([(\d\.)]+) - (.*?) \[(.*?)\] "(.*?) (.*?) (.*?)" (\d+) (\d+)'
elif format=='ncsa-combined':
regex = '([(\d\.)]+) - (.*?) \[(.*?)\] "(.*?) (.*?) (.*?)" (\d+) (\d+) "(.*?)" "(.*?)"'
matches = re.search(regex, log.strip())
if matches:
values = []
for i, field in enumerate(fields):
if field=='timestamp' and (format=='ncsa-common' or format=='ncsa-combined'):
values.append(datetime.timestamp(parse(matches.group(i+1).replace(':',' ',1))))
elif format=='aws-elb-application' and field=='type':
continue
else:
values.append(matches.group(i+1))
log_values.append(values)
cur = connection.cursor()
if format=='aws-elb-classic' or format=='aws-elb-application':
cur.executemany("""INSERT INTO logs (
timestamp, elb_name, request_ip, request_port, backend_ip, backend_port,
request_processing_time, backend_processing_time, client_response_time,
request_status_code, backend_status_code, received_bytes, sent_bytes,
request_verb, request_url, request_protocol, user_agent, ssl_cipher, ssl_protocol
) VALUES ( strftime('%s', ?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
""", log_values)
elif format=='ncsa-common':
cur.executemany("""INSERT INTO logs (
request_ip, auth_user, timestamp, request_verb, request_url, request_protocol, request_status_code,
sent_bytes) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )
""", log_values)
elif format=='ncsa-combined':
cur.executemany("""INSERT INTO logs (
request_ip, auth_user, timestamp, request_verb, request_url, request_protocol, request_status_code,
sent_bytes, referrer, user_agent) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
""", log_values)
connection.commit()
def run_queries(mode='live', params=()):
global connection, queries, output
#print('Running queries...')
for i, q in enumerate(queries):
cur = connection.cursor()
# prevent accidentally dumping tons of data to console!
if len(params)>0:
cur.execute(q, params)
else:
cur.execute(q)
rows = cur.fetchall()
#print(rows[0:3])
if mode=='live': flag = 'w'
else: flag = 'a'
with open(output + 'query' + str(i) + '.csv', flag) as write_obj:
csv_writer = csv.writer(write_obj)
for row in rows:
csv_writer.writerow(row)
def print_db_stats():
min_ts = get_db_stat('SELECT MIN(timestamp) FROM logs')
if min_ts: min_time = datetime.utcfromtimestamp(min_ts).strftime('%Y-%m-%d %H:%M:%S')
else: min_time = ''
max_ts = get_db_stat('SELECT MAX(timestamp) FROM logs')
if max_ts: max_time = datetime.utcfromtimestamp(max_ts).strftime('%Y-%m-%d %H:%M:%S')
else: max_time = ''
n_rows = get_db_stat('SELECT COUNT(*) FROM logs')
page_size = get_db_stat('PRAGMA page_size')
page_count = get_db_stat('PRAGMA page_count')
print("Database is now "+min_time+" to "+max_time+" ("+str(n_rows)+" rows). Memory used ~= "+str(page_size*page_count)+" bytes.")
def get_db_stat(q):
global connection
cur = connection.cursor()
cur.execute(q)
rows = cur.fetchall()
if rows and rows[0]: return rows[0][0]
else: return None