-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgalipd.py
More file actions
executable file
·39 lines (28 loc) · 1.25 KB
/
galipd.py
File metadata and controls
executable file
·39 lines (28 loc) · 1.25 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
#!/usr/bin/env python3
"""
the module that is the galipd systemd service
galipd.processAccessLogs(filename) will continuously read lines from filename.
If the line is syntactically valid JSON, and the log line contains a "remote_addr" field,
the value for 'remote_addr' will be used to find the geolocation from which the request originated.
The geolocation data will be stored in a database with some meta data
the data can be accessed later using the galip module also in this package.
"""
import os
import jsonLogging
from readJsonFile import getJsonObjectsFromFileInfinite as logGenerator
jogger = jsonLogging.getJsonLogger("galipd")
def getGeolocation(ip_addr):
jogger.info("looking up location for IP address: %s", ip_addr)
def processAccessLogs(filename):
jogger.info("Processing access logs from {}".format(filename))
logs = logGenerator(filename)
while True:
try:
alo = next(logs) # access log object
getGeolocation(alo['remote_addr'])
except ValueExcept:
jogger.warning("hmmm...something bad happened? Carry on")
if __name__ == '__main__':
logfile = os.getenv('GALIP_ACCESS_LOG_FILE')
if not logfile: logfile = '/var/log/nginx/access.log'
processAccessLogs(logfile)