Repository navigation
Expand file tree
/
Copy pathsend_to_dB.py
More file actions
85 lines (67 loc) · 2.48 KB
/
Copy pathsend_to_dB.py
File metadata and controls
85 lines (67 loc) · 2.48 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
import sys
import apachelog
import pymongo
from datetime import datetime
# Mongo DB config
MONGO_HOST='localhost'
MONGO_PORT=27017
MONGO_DB_NAME='logging_db'
MONGO_COLLECTION_NAME='access_log_collection'
MONGO_COLLECTION_SIZE=1*1024*1024 # 1MB
# Access Log format. Combined.
ACCESS_LOG_FORMAT = r'%h %l %l %u %t \"%r\" %>s %b'
# Aliases for apache log parser
ACCESS_ALIAS = {
'%>s': 'status',
'%b': 'size',
'%h': 'client_ip',
'%r': 'request',
'%t': 'time',
'%{Referer}i': 'referer',
'%{User-Agent}i': 'user_agent' }
# Configure alias
def apachelog_parser_alias(self, name):
if name in ACCESS_ALIAS: return ACCESS_ALIAS[name]
else: return name
apachelog.parser.alias = apachelog_parser_alias
# Initialize mongo
def do_mongo_init():
# Connect allowing slave
connection = pymongo.MongoClient("localhost", 27017)
db = connection[MONGO_DB_NAME]
# Mongo will create the collection automacly, BUT we want to set
# capped collection to enable "log rotation".
# If you try to create a collection that already exists, it thows
# pymongo.CollectionInvalid
try:
collection = \
db.create_collection(MONGO_COLLECTION_NAME,
capped = True,
size = MONGO_COLLECTION_SIZE)
print( "New collection created")
except pymongo.errors.CollectionInvalid:
# If already exists, get it.
collection = db[MONGO_DB_NAME]
return (connection, db, collection)
def main():
(mongo_connection, mongo_db, mongo_collection) = do_mongo_init()
parser = apachelog.parser(ACCESS_LOG_FORMAT)
file = open('log.txt')
while True:
logline = file.readline()
if logline == '': break
try:
data = parser.parse(logline)
# save as datetime to allow date queries
(timestamp_str, timezone_str) = data['time'][1:-1].split(' ')
data['timestamp'] = datetime.strptime(timestamp_str, "%d/%b/%Y:%H:%M:%S")
data['timezone'] = int(timezone_str)
(data['method'], data['url'], data['proto']) = data['request'].split(' ')
# store the data in the Mongo DB
mongo_collection.insert_one(data)
except apachelog.ApacheLogParserError:
# ignore invalid lines
print('Invalid log format: "' + logline + '"')
mongo_connection.close()
if __name__ == '__main__':
main()