-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSosTraffic.py
115 lines (96 loc) · 4.01 KB
/
SosTraffic.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
import os, re, json
from path import Path
class Stats():
ops = 0
elapsed = 0
sysErr = 0
usrErr = 0
success = 0
size = 0
def addStat(self, jsonLog):
bytesReceived = jsonLog["bytesReceived"]
httpCode = jsonLog["httpCode"]
latency = jsonLog["elapsed_ms"]
if("contentLength" in jsonLog):
objSize = jsonLog["contentLength"]
self.size += objSize
elif("bodyLength" in jsonLog):
objSize = jsonLog["bodyLength"]
self.size += objSize
self.ops += 1
self.elapsed += latency
if(httpCode < 300):
self.success += 1
elif(httpCode < 500):
self.usrErr += 1
elif(httpCode >= 500):
self.sysErr += 1
def addUnitStat(self, stat):
self.ops += stat.ops
self.elapsed += stat.elapsed
self.sysErr += stat.sysErr
self.usrErr += stat.usrErr
self.success += stat.success
self.size += stat.size
def getLatency(self):
return self.elapsed/self.ops
def getResult(self):
return ("ops: "+str(self.ops)+", success: " + '{:.1f}%'.format(self.success/self.ops*100)
+ ", usrErr: "+ '{:.1f}%'.format(self.usrErr/self.ops*100)
+ ", sysErr: "+ '{:.3f}%'.format(self.sysErr/self.ops*100)
+ ", avgObjSize: "+ '{:.1f}'.format(self.size/self.ops) + " bytes"
+ ", avgLatency: "+ '{:.1f}'.format(self.elapsed/self.ops) + " ms")
class SosTraffic:
cslogs = []
pattern = re.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]+[+-]{1}[0-9]{2}:[0-9]{2} (.*)")
stats = dict()
def __init__(self, sosarchive):
csLogsdir = sosarchive +'/sos_commands/metalk8s/by-resources/pod/zenko/'
if Path(csLogsdir).is_dir():
files = os.listdir(csLogsdir)
for file in files:
if (re.search("artesca-data-connector-cloudserver-(.*)-(.*)_logs.txt", file)):
self.cslogs.append(csLogsdir + str(file))
else:
print("No cs log files found")
self.parseLogs()
def parseLogs(self):
for logFile in self.cslogs:
with open(logFile, 'r') as file:
lines = file.readlines()
for line in lines:
if (re.search(",\"message\":\"responded(.*)", line)):
jsonStr = self.extractJsonContent(line)
jsonLog = json.loads(jsonStr)
self.addStat(jsonLog)
def extractJsonContent(self, line):
result = self.pattern.search(line)
jsonStr = result.group(1)
return jsonStr
def addStat(self, jsonLog):
if("bucketName" in jsonLog and "accountName" in jsonLog):
accountName = jsonLog["accountName"]
bucketName = jsonLog["bucketName"]
action = jsonLog["action"]
key = accountName+"-"+bucketName
#print(accountName+"-"+bucketName+"-"+action+"-"+str(httpCode)+"-"+str(bytesReceived))
if(self.stats.get(key) is None):
self.stats[key] = dict()
if(self.stats[key].get(action) is None):
self.stats[key][action] = Stats()
self.stats[key][action].addStat(jsonLog)
def printResult(self):
print("============================== TRAFFIC ===============================")
print("### Display stats per bucket when latency > 100ms or when sysErr > 0 ###")
total = dict()
for key in self.stats:
print(key)
for action in self.stats.get(key):
if( self.stats[key][action].getLatency() > 100 or self.stats[key][action].sysErr > 0):
print(" "+ action + ": " + self.stats[key][action].getResult())
if(total.get(action) is None):
total[action] = Stats()
total[action].addUnitStat(self.stats[key][action])
print("Total:")
for action in total:
print(" "+ action + ": " + total[action].getResult())