forked from aws-samples/service-screener-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleCount.py
144 lines (115 loc) · 3.95 KB
/
RuleCount.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
import os
import sys
import json
try:
from prettytable import PrettyTable
except ModuleNotFoundError as err:
sys.exit('PrettyTable module not found, run \"pip install prettytable\"')
SERVICE_FOLDER_PATH = os.getcwd() + '/services'
def getReporterPaths(serviceFolderPath):
reporterPath = {}
with os.scandir(serviceFolderPath) as dir:
directories = list(dir)
directories.sort(key=lambda x: x.name)
for item in directories :
if item.is_dir():
servicePath = serviceFolderPath + '/' + item.name
serviceItems = os.scandir(servicePath)
for service in serviceItems:
if service.is_file() and service.name.endswith('reporter.json'):
reporterPath[item.name] = servicePath + '/' + service.name
serviceItems.close()
return reporterPath
def getRulesFromJSON(reporterPath):
reporterFile = open(reporterPath, 'r')
ruleJSON = reporterFile.read()
reporterFile.close()
rules = json.loads(ruleJSON)
return rules
def getTableFormat(cntType):
if cntType == 'PILLAR':
tableFormat = {
'C': 0,
'P': 0,
'S': 0,
'R': 0,
'O': 0,
'T': 0
}
else:
tableFormat = {
'I': 0,
'L': 0,
'M': 0,
'H': 0
}
return tableFormat
def getCntSummary(reporterPath, cntType):
cntTable = getTableFormat(cntType)
rules = getRulesFromJSON(reporterPath)
for ruleName in rules:
if cntType == 'PILLAR':
allCategory = rules[ruleName]['category']
category = allCategory[0]
else:
category = rules[ruleName]['criticality']
cntTable[category] += 1
return cntTable
def formSummaryPrettyTable(tableType):
totalCategoryTable = getTableFormat(tableType)
reporterPaths = getReporterPaths(SERVICE_FOLDER_PATH)
totalService = len(reporterPaths)
totalRules = 0
table = []
## Form per service row and calculate total number of rules
info = {}
for service in reporterPaths:
path = reporterPaths[service]
cntResult = getCntSummary(path, tableType)
serviceRow = [service]
totalPerService = 0
for category in cntResult:
serviceRow.append(cntResult[category])
totalPerService = totalPerService + cntResult[category]
totalCategoryTable[category] = totalCategoryTable[category] + cntResult[category]
serviceRow.append(totalPerService)
table.append(serviceRow)
totalRules = totalRules + totalPerService
## Does not matter, just need it to run once
sname = service
if(tableType == 'PILLAR'):
if sname == 'lambda_':
sname = 'lambda'
info[sname] = totalPerService
if len(info) > 0:
f = open("info.json", "w+")
f.write(json.dumps(info))
f.close()
## Form splitter row
splitRow = ['-------']
for i in range (len(totalCategoryTable) + 1):
splitRow.append('')
table.append(splitRow)
## Form total Row
totalRow = ['Total']
totalRow = totalRow + list(totalCategoryTable.values())
totalRow.append(totalRules)
table.append(totalRow)
## Form mean and header row
meanRow = ['Mean']
titleRow = ['Services']
for category in totalCategoryTable:
meanRow.append(round(totalCategoryTable[category]/totalService, 2))
titleRow.append(category.center(7,'_'))
meanRow.append(round(totalRules/totalService, 2))
titleRow.append('_TOTAL_')
table.append(meanRow)
prettyTable = PrettyTable()
prettyTable.field_names = titleRow
prettyTable.add_rows(table)
return prettyTable
if __name__ == "__main__":
print('Service Rules Pillar Count:')
print(formSummaryPrettyTable('PILLAR'))
print('Service Rules Criticality Count:')
print(formSummaryPrettyTable('SEVERITY'))