-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
154 lines (134 loc) · 5.05 KB
/
utils.py
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
import re
# Matches only object record page hits
OBJ_VIEW = re.compile(r"""GET /objects/(?P<namespace>ora|uuid|hdl)(\:|\%3A|\%253A)(?P<id>[0-9abcedf\-]+)[/]? """, re.I|re.U)
# Matches any GET on an object resource base URL, inc. record views
OBJ_GETS = re.compile(r"""GET /objects/(?P<namespace>ora|uuid|hdl)(\:|\%3A|\%253A)(?P<id>[0-9abcedf\-]+)[/]?""", re.I|re.U)
# Matches any GET on an object datastream
OBJ_DATASTREAM = re.compile(r"""GET /objects/(?P<namespace>ora|uuid|hdl)(\:|\%3A|\%253A)(?P<id>[0-9abcedf\-]+)/datastreams/(?P<dsid>[0-9A-z\-]+) """, re.I|re.U)
IP_TEST = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
import logging
logger = logging.getLogger("logfile_utils")
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
def parseline(line):
tokens = line.split(" ")
columns = []
escaped = []
for token in tokens:
token = token.strip()
if token:
if escaped:
if token.endswith("\""):
escaped.append(token[:-1])
columns.append(" ".join(escaped))
escaped = []
else:
escaped.append(token)
else:
if token.startswith("\""):
if token.endswith("\""):
columns.append(token[1:-1])
else:
escaped = [token[1:]]
else:
columns.append(token)
return columns
def pageview(parsedline):
if len(parsedline) == 6:
return False
try:
logger.debug("Checking to see if %s is a object/pageview" % parsedline[9])
m = OBJ_VIEW.match(parsedline[9])
except IndexError, e:
logger.debug("Failed to match - Exception thrown")
return False
if m != None:
results = m.groupdict()
if results.get("id") and results.get("namespace"):
logger.debug("It is a view/dl!")
return "%s:%s" % (results['namespace'], results['id'])
return False
def getonobjecturl(parsedline):
if len(parsedline) == 6:
return False
try:
logger.debug("Checking to see if %s is a object-orientated URL" % parsedline[9])
m = OBJ_GETS.match(parsedline[9])
except IndexError, e:
logger.debug("Failed to match - Exception thrown")
return False
if m != None:
results = m.groupdict()
if results.get("id") and results.get("namespace"):
logger.debug("It is an object-oriented URL!")
return "%s:%s" % (results['namespace'], results['id'])
return False
def isbotip(ip, r): # r = redis_client
for botlist in r.smembers('botlist'):
domainip = "1234567890"
try:
domainip = ".".join(ip.split(".")[:3])
except:
logger.debug("Domain IP parse Fail")
pass
if r.sismember(botlist, ip) or r.sismember(botlist, domainip):
return True
return False
def isbot_in_ua(pl):
if len(pl)>13:
ua = pl[13]
for agent in ['msnbot','Slurp','bot','crawler']:
if ua.find(agent)>0:
return True
return False
def characterise_logline(line, r):
pl = parseline(line)
if len(pl)>5:
pid = getonobjecturl(pl)
logger.debug("Testing if %s is a bot" % (pl[4]))
if IP_TEST.match(pl[4]) != None and pl[10] != "404":
if isbot_in_ua(pl):
return "bot"
logger.debug("User agent says it is a bot!")
if isbotip(pl[4], r):
logger.debug("It is a bot!")
return "bot"
elif pid:
return "itemview"
else:
return "other"
else:
return "fourohfour"
else:
return
def characterise_and_requeue_logline(line, r = None, bothits = "bothits", objectviews = "objectviews", other = "other", fourohfour = "fof"):
if r == None:
logger.error("You must pass a redis client to this function for it to work")
character = characterise_logline(line, r)
if character.startswith("b"):
r.incr("u:%s" % bothits)
r.lpush("q:%s" % bothits, line)
elif character.startswith("i"):
r.incr("u:%s" % objectviews)
r.lpush("q:%s" % objectviews, line)
elif character.startswith("o"):
r.incr("u:%s" % other)
r.lpush("q:%s" % other, line)
elif character.startswith("f"):
r.incr("u:%s" % fourohfour)
r.lpush("q:%s" % fourohfour, line)
else:
#ignore the line
pass
#if __name__ == "__main__":
# from redis import Redis
# from os import listdir
# from os.path import isdir, join
# r = Redis()
# for logfile in [x for x in listdir(logpath) if not isdir(join(logpath,x))]:
# logger.debug("Parsing %s" % logfile)
# parselog("%s/%s" % (logpath, logfile), r, 1000)