-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer2
executable file
·122 lines (107 loc) · 4.73 KB
/
analyzer2
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
#!/usr/bin/python
""""
Author:
Umang Agrawal ([email protected])
Description:
Generates a SA warning report by analyzing the logs generated by the static_dcos script.
How to use:
Put this script in the directory containg all the logs(created by the saw_report script). Then, run python analyzer.py from that directory.
NOTE : This script runs fine only in python 3.x with pandas library installed.
"""
#Importing Required Packages
import os,shutil,glob,io,time
from collections import defaultdict
import sys
import pandas as pd
def sa_warning_analysis(file):
warning_record = dict()
with open(file) as f:
line = f.readline().strip()
while (True):
if '@@' in line :
file_name = "/".join(line.rsplit("/", 4)[1:])
count = 0
if file_name not in warning_record.keys():
warning_record[file_name] = defaultdict(list)
line = f.readline().strip()
elif '**' in line and count == 0:
while(True):
line = f.readline().strip()
if '**' in line:
count = 1
break
elif '**' not in line:
if (':' in line):
try:
filename, lin_no, sev, div, warning = line.split(':',4)
warning_record[file_name]['Active'].append((filename, lin_no, sev, div, warning))
except ValueError:
print(line.split(':'))
elif '**' in line and count == 1:
while(True):
line = f.readline().strip()
if '**' in line:
count = 2
break
elif '**' not in line:
if (':' in line):
try:
filename, lin_no, sev, div, warning = line.split(':',4)
warning_record[file_name]['Noise'].append((filename, lin_no, sev, div, warning))
except ValueError:
print(line.split(':'))
elif '**' in line and count == 2:
while(True):
line = f.readline().strip()
if '**' in line:
count = 3
break
elif '@@' in line :
break
elif line == '' :
break
elif '**' not in line:
if (':' in line):
try:
filename, lin_no, sev, div, warning = line.split(':',4)
warning_record[file_name]['Parent'].append((filename, lin_no, sev, div, warning))
except ValueError:
print(line.split(':'))
elif 'Total' in line :
break
else:
line = f.readline().strip()
#Getting the labels for classification of SA Warnings
div_set = set()
for key in warning_record.keys():
for warning_list in warning_record[key].values():
for item in warning_list:
div_set.add(item[3])
#Creating a dictionary for maintaing count for each warning type
div_type_count = {}
for item in div_set:
div_type_count[item] = 0
#Iterating through the warnings to get the count of eah warning type
for key in warning_record.keys():
for warning_list in warning_record[key].values():
for item in warning_list:
div_type_count[item[3]] = div_type_count[item[3]] + 1
final_report = []
for div, div_count in div_type_count.items():
if div_count > 0:
final_report.append((div, div_count))
print('\n********** ' + file + ' **********')
final_report.sort(key = lambda x:x[1], reverse = True)
df = pd.DataFrame(final_report, columns = [str(file.replace('.','/')), ''])
with open('report2.csv', 'a') as report:
df.to_csv(report)
for item in final_report:
print( item[0] + '-'*(50-len(item[0])) + ' ' + str(item[1]) )
print('\n')
if __name__ == "__main__":
if 'report2.csv' in os.listdir():
os.remove('report2.csv')
for file in glob.glob('*'):
if file not in ['analyzer1', 'analyzer2', 'report1.csv', 'report2.csv']:
sa_warning_analysis(file)
print('\nFor more info, please refer : https://wiki.cisco.com/display/DC3SW/SA+Warning+Analyser ')