-
Notifications
You must be signed in to change notification settings - Fork 0
/
pylog.py
64 lines (46 loc) · 1.44 KB
/
pylog.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
# Apache Spark Log Processor
from pyspark import SparkContext, SparkConf
import sys
import math
def writeToFile(fname, string):
f = open(fname,'a')
f.write(string)
f.close()
def Main():
sc = SparkContext(appName='pylog')
if len(sys.argv) == 3:
log = sc.textFile(sys.argv[1])
log_ne = log.filter(lambda x: len(x) > 0 )
line = log_ne.map(lambda x: x.split())
ip = line.map(lambda x: (x[0],1))\
.reduceByKey(lambda x,y: x+y)\
.sortByKey(True)
req_10 = line.map(lambda x: (x[3].replace('[',''),1))\
.map(lambda x: (x[0][0:-4]+'0:00',1))\
.reduceByKey(lambda x,y: x+y)\
.sortByKey(True)
tp99 = line.map(lambda x: (int(x[8]),1))\
.sortByKey(lambda x: x)
index = int(math.ceil(tp99.count()*.99))
tp = tp99.take(index)
outlog = 'IP : Count\n'
# requests from an IP
for (i, icount) in ip.collect():
#print(i + ' => ' + str(icount))
outlog += i + ' ' + str(icount) + '\n'
writeToFile(sys.argv[2]+'/ip_addr.log',outlog)
outlog = 'Request interval: Count\n'
# requests every 10 minute
for (ts, tscount) in req_10.collect():
#print(ts + ' => ' + str(tscount))
outlog += ts + ' ' + str(tscount) + '\n'
writeToFile(sys.argv[2]+'/req_int.log',outlog)
# TP99
#print ('TP99 = ' + str(tp[-1][0]))
outlog = 'TP99 ' + str(tp[-1][0]) + '\n'
writeToFile(sys.argv[2]+'/tp_99.log',outlog)
else:
print 'Usage: pylog <input file> <output path>'
sc.stop()
if __name__ == '__main__':
Main()