-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.py
178 lines (169 loc) · 8.2 KB
/
file.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
engine='python'
import re
import pandas as pd
import numpy as np
import csv
import matplotlib.pyplot as plt
from zipfile import ZipFile
# this function is used for filter with only one key values
def searchWithOneKey(dataset):
print("enter 1 if you want to search with IPADDRESS \nenter 2 if you want to search with
REQUEST_TYPE \nenter 3 if you want to search with STATUS_CODE.\nenter 4 if you want to search with
ERROR ")
select = int(input("enter your choice"))
if select==1:
ipAddress=input("enter IPADDRESS for fillter=> ")
p = dataset[(dataset['IP_ADDRESS'] == ipAddress)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==2:
requestType=input("enter REQUEST_TYPE for fillter=> ")
p = dataset[(dataset['REQUEST_TYPE'] == requestType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==3:
statusCode=input("enter STATUS_CODE for fillter=> ")
p = dataset[(dataset['STATUS_CODE'] == statusCode)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==4:
errorType=input("enter ERRORTYPE for fillter=> ")
p =dataset[(dataset['ERROR'].astype(str).str[:3].astype(np.str)== errorType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
else:
print("please inter valid operation")
p.to_csv('log.csv',index=False)
# this function is used for filter with only one key values
def searchWithTwoKeys(dataset):
print("enter 1 if you want to search with IPADDRESS AND ERROR. \nenter 2 if you want to search with
ERROR AND REQUEST_TYPE. \nenter 3 if you want search with REQUEST_TYPE AND STATUS_CODE.
\nenter 4 if you want search with IPADDRESS AND STATUS_CODE. \nenter 5 if you want search with
ERROR AND STATUS_CODE. \nenter 6 if you want to search with IPADDRESS AND REQUEST_TYPE.")
select = int(input("enter your choice"))
if select==1:
ipAddress=input("enter IPADDRESS for fillter=> ")
errorType=input("enter ERRORTYPE for fillter=> ")
p = dataset[(dataset['IP_ADDRESS'] == ipAddress) &
(dataset['ERROR'].astype(str).str[:3].astype(np.str)== errorType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==2:
errorType=input("enter ERRORTYPE for fillter=> ")
requestType=input("enter REQUEST_TYPE for fillter=> ")
p = dataset[(dataset['ERROR'].astype(str).str[:3].astype(np.str)== errorType) &
(dataset['REQUEST_TYPE'] == requestType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==3:
requestType=input("enter REQUEST_TYPE for fillter=> ")
statusCode=input("enter ERRORTYPE for fillter=> ")
p = dataset[(dataset['STATUS_CODE'] == statusCode) & (dataset['REQUEST_TYPE'] == requestType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==4:
ipAddress=input("enter IPADDRESS for fillter=> ")
statusCode=input("enter ERRORTYPE for fillter=> ")
p = dataset[(dataset['IP_ADDRESS'] == ipAddress) & (dataset['STATUS_CODE'] == statusCode)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==5:
errorType=input("enter ERRORTYPE for fillter=>")
statusCode=input("enter ERRORTYPE for fillter=>")
p = dataset[(dataset['STATUS_CODE'] == statusCode) &
(dataset['ERROR'].astype(str).str[:3].astype(np.str)== errorType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select == 6:
ipAddress=input("enter IPADDRESS for fillter=>")
requestType=input("enter REQUEST_TYPE for fillter=>")
p = dataset[(dataset['IP_ADDRESS'] == ipAddress) & (dataset['REQUEST_TYPE'] == requestType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
else:
print("plz choose the correct choice")
p.to_csv('log.csv',index=False)
#this function defination for the filter logs with three key values
def searchWithThreeKeys(dataset):
print("enter 1 if you want to search with IPADRESS AND ERROR AND REQUEST_TYPE.\nenter 2 if you
want to search with IPADDRESS AND REQUEST_TYPE AND STATUS_CODE. \nenter 3 if you want to
search with ERROR AND REQUEST_TYPE AND STATUS_CODE. \nenter 4 if you want to search with
IPADRESS AND ERROR AND STATUS_CODE")
select = int(input("enter your choice"))
if select==1:
ipAddress=input("enter IPADDRESS for fillter=> ")
errorType=input("enter ERRORTYPE for fillter=> ")
requestType=input("enter REQUEST_TYPE for fillter=> ")
p = dataset[(dataset['IP_ADDRESS'] == ipAddress) &
(dataset['ERROR'].astype(str).str[:3].astype(np.str)== errorType) & (dataset['REQUEST_TYPE'] ==
requestType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==2:
ipAddress=input("enter ERRORTYPE for fillter=> ")
requestType=input("enter REQUEST_TYPE for fillter=> ")
statusCode=input("enter ERRORTYPE for fillter=> ")
p = dataset[(dataset['ERROR'] == ipAddress) & (dataset['REQUEST_TYPE'] == requestType) &
(dataset['STATUS_CODE'] == statusCode)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==3:
errorType=input("enter ERRORTYPE for fillter=> ")
requestType=input("enter REQUEST_TYPE for fillter=> ")
statusCode=input("enter ERRORTYPE for fillter=> ")
p = dataset[(dataset['ERROR'].astype(str).str[:3].astype(np.str)== errorType) &
(dataset['STATUS_CODE'] == statusCode) & (dataset['REQUEST_TYPE'] == requestType)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
elif select==4:
ipAddress=input("enter IPADDRESS for fillter=> ")
errorType=input("enter ERRORTYPE for fillter=> ")
statusCode=input("enter STATUS_CODE for fillter=> ")
p=dataset[(dataset['IP_ADDRESS'] == ipAddress) &
(dataset['ERROR'].astype(str).str[:3].astype(np.str)== errorType) & (dataset['STATUS_CODE'] ==
statusCode)]
p.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
else:
print("please inter valid operation")
p.to_csv('log.csv',index=False)
# initialize the column names for dataframes
l=['DATE-TIME','ERROR', 'IP_ADDRESS' ,'d','USER_AGENT','f', 'REQUEST_FROM', 'h',
'REQUEST_TYPE','j','API','l','USER_LOGIN', 'n' ,'USER_NAME','p','ENTERPRISE_ID', 'r',
'ENTERPRISE_NAME', 't','AUTH_STATUS','v','STATUS_CODE','x', 'RESPONSE_TIME' ,'z','A']
#main loop of the code
fileName =input("enter the file in zip format with .zip=> ")
with ZipFile(fileName, 'r') as zip:
zip.extractall()
textFileName = fileName[:-4]+'.txt'
dataSet=pd.read_csv(textFileName,delimiter=",|=",names=l) #this line make the dataframe for test file
dataSet=dataSet.drop(columns=['d','f','h','j','l','n','p','r','t','v','x','z','A']) #this line for drop the
unnaccessary columns.
dataSet['ERROR']=dataSet['ERROR'].astype(str).str[:-10].astype(np.str) #For remove the extra line in
error_code
for j in ['DATETIME','ERROR','IP_ADDRESS','USER_AGENT','REQUEST_FROM','REQUEST_TYPE','API','USER_LOGIN','USE
R_NAME','ENTERPRISE_ID','ENTERPRISE_NAME','AUTH_STATUS','STATUS_CODE','RESPONSE_TIME']:
dataSet[j]=dataSet[j].astype(str).str[:-1].astype(np.str) #remove the last # from the valuse of the keys
# group by plot the graph beteween request type and the user login how many users are request type is
post and how many users request type is get
dataSet.groupby(['REQUEST_TYPE','USER_LOGIN']).size().unstack().plot(kind='bar',stacked=True)
plt.show()
#loop for perform the filter as many times u want
print ("Do you want to search")
userChoice = input("Type Y/y for yes and N/n for No=> ")
while(userChoice == 'Y' or userChoice=='y'):
print("\nselect 1 if you want with search with one key \nselect 2 if you want with search with two keys
\nselect 3 if you want with search with three keys")
select =int(input())
if select== 1:
searchWithOneKey(dataSet)
elif select==2:
searchWithTwoKeys(dataSet)
elif select== 3:
searchWithThreeKeys(dataSet)
print ("Do you want to search")
userChoice = input("Type Y/y for yes and N/n for No=> ")
#error=input("ERROR")
#user_agent=input("ENTER USER-AGENT ")
#user_name=input("ENTER USER-NAME ")
#enterprise_id=int(input("ENTERPRISE-ID"))