-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
280 lines (224 loc) · 8.98 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import multiprocessing
import numpy as np
import pandas as pd
import copy
import argparse
from os import getpid
from pandas.core.frame import DataFrame
from logLoader import loadLogFileToDF
from dataPreparation import dataPreparation
from dataExtraction import (
getStatusCodeCount,
getStatusCodeTimeLine,
getUsageHours,
getRequestCount,
getUsageDays,
getReferrer,
getOverallStats,
)
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import plotly.io as pio
import dash_bootstrap_components as dbc
import plotly.graph_objs as go
from datetime import date, datetime
from dash.exceptions import PreventUpdate
from multiprocessing import Process, Queue
app = dash.Dash("Apache SSL Log Analyzer & Dashboard",
external_stylesheets=[dbc.themes.DARKLY])
pio.templates.default = "plotly_dark"
def main(path):
try:
df = loadLogFileToDF(path)
df = dataPreparation(df)
ref, rc, scc, tscc, uh, ud, allg = getData(df)
fig, fig2, fig3, fig4, fig5, fig6, lists = getFigs(
ref, rc, scc, tscc, uh, ud, allg)
app.layout = html.Div(
[
dbc.Row([
dbc.Col(html.H1(children="Apache SSL Log Analyzer & Dashboard"), style={
'padding-top': '25px', 'padding-left': '50px'}),
dbc.Col([dcc.DatePickerRange(
id='date-picker-range',
display_format='DD.MM.YYYY',
start_date_placeholder_text='DD.MM.YYYY',
end_date_placeholder_text='DD.MM.YYYY'
), html.P("*Updating takes some time")
], style={'text-align': 'right', 'background': 'rgb(17, 17, 17)', 'padding-top': '25px', 'padding-right': '50px'}),
]),
html.Hr(style={'border-color': 'white'}),
dcc.Loading(
id="loading-1",
type="cube",
children=[
dbc.Row([
dbc.Col(html.Div([
html.H5('Overall Stats:', style={
'padding-top': '25px', 'padding-left': '50px'}),
html.Div([lists], id="overall-stats",),
]),),
dbc.Col(html.Div([
html.H5('Daily Calls:', style={
'padding-top': '25px', 'padding-left': '50px'}),
dcc.Graph(id="ud", figure=fig5),
]),),
]),
html.H5('Avg. Usages by quarter hours:', style={
'padding-top': '25px', 'padding-left': '50px'}),
dcc.Graph(id="ug", figure=fig3),
html.Hr(style={'border-color': 'white'}),
dbc.Row([
dbc.Col(html.Div([
html.H5('Status Code Time Line:', style={
'padding-top': '25px', 'padding-left': '50px'}),
dcc.Graph(id="time-series", figure=fig2),
]),),
dbc.Col(html.Div([
html.H5('Status Code Distribution:', style={
'padding-top': '25px', 'padding-left': '50px'}),
dcc.Graph(id="pie-chart", figure=fig),
]),),
]),
html.Hr(style={'border-color': 'white'}),
dbc.Row([
dbc.Col(html.Div([
html.H5('Most used API Routes:', style={
'padding-top': '25px', 'padding-left': '50px'}),
dcc.Graph(id="rc", figure=fig4),
]),),
dbc.Col(html.Div([
html.H5('Top 5 Referer:', style={
'padding-top': '25px', 'padding-left': '50px'}),
dcc.Graph(id="ref", figure=fig6),
]),),
]),
html.Hr(style={'border-color': 'white'}),
html.Div(["Developed by ", html.A(children=["Niklas Ullmann"], href="https://niklas-ullmann.de/", target="_blank")],
style={'padding-left': '50px', })
]
),
],
style={'background': 'rgb(17, 17, 17)'}
)
@app.callback(
[dash.dependencies.Output('pie-chart', 'figure'),
dash.dependencies.Output('time-series', 'figure'),
dash.dependencies.Output('ug', 'figure'),
dash.dependencies.Output('rc', 'figure'),
dash.dependencies.Output('ud', 'figure'),
dash.dependencies.Output('ref', 'figure'),
dash.dependencies.Output('overall-stats', 'children'), ],
[dash.dependencies.Input('date-picker-range', 'start_date'),
dash.dependencies.Input('date-picker-range', 'end_date')],
)
def update_output(start_date, end_date):
if start_date is not None and end_date is not None:
x = copy.deepcopy(df)
start = datetime.strptime(start_date, '%Y-%m-%d')
end = datetime.strptime(end_date, '%Y-%m-%d')
x['Datetime'] = pd.to_datetime(x[['year', 'month', 'day']].astype(
str).apply(' '.join, 1), format='%Y %m %d')
y = x[x['Datetime'].between(start, end)]
if(y.shape[0] <= 0):
print("No Logs for this time period")
raise PreventUpdate
ref, rc, scc, tscc, uh, ud, allg = getData(y)
fig, fig2, fig3, fig4, fig5, fig6, lists = getFigs(
ref, rc, scc, tscc, uh, ud, allg)
return fig, fig2, fig3, fig4, fig5, fig6, lists
else:
raise PreventUpdate
except Exception as err:
print("Failed to analyze log file:")
print(err)
app.run_server(debug=False)
def getData(df):
print("Number of cpu : ", multiprocessing.cpu_count())
q1 = Queue()
q2 = Queue()
q3 = Queue()
q4 = Queue()
q5 = Queue()
q6 = Queue()
q7 = Queue()
p1 = Process(target= getReferrer, args=(copy.deepcopy(df), q1,))
p1.start()
p2 = Process(target= getRequestCount, args=(copy.deepcopy(df), q2,))
p2.start()
p3 = Process(target= getStatusCodeCount, args=(copy.deepcopy(df), q3,))
p3.start()
p4 = Process(target= getStatusCodeTimeLine, args=(copy.deepcopy(df), q4,))
p4.start()
p5 = Process(target= getUsageHours, args=(copy.deepcopy(df), q5,))
p5.start()
p6 = Process(target= getUsageDays, args=(copy.deepcopy(df), q6,))
p6.start()
p7 = Process(target= getOverallStats, args=(copy.deepcopy(df), q7,))
p7.start()
p1.join()
p2.join()
p3.join()
p4.join()
p5.join()
p6.join()
p7.join()
ref = q1.get()
rc = q2.get()
scc = q3.get()
tscc = q4.get()
uh = q5.get()
ud = q6.get()
allg = q7.get()
return ref, rc, scc, tscc, uh, ud, allg
def getFigs(ref, rc, scc, tscc, uh, ud, allg):
fig = px.pie(scc, values="count", names="status")
fig2 = px.line(tscc, x="date", y=tscc.columns)
fig3 = go.Figure(data=[
go.Bar(
x=uh['time'],
y=uh['counts']
),
go.Scatter(
x=uh['time'],
y=uh['bestfit']
)
])
fig3.update_layout(showlegend=False)
fig4 = px.bar(rc, x="counts", y="request", orientation="h")
fig5 = go.Figure(data=[
go.Scatter(
x=ud['date'],
y=ud['counts']
),
go.Scatter(
x=ud['date'],
y=ud['bestfit']
)
])
fig5.update_layout(showlegend=False)
#fig5 = px.line(ud, x="date", y="counts")
fig6 = px.pie(ref, values="counts", names="referrer")
lists = createOverallList(allg)
return fig, fig2, fig3, fig4, fig5, fig6, lists
def createOverallList(allg):
return html.Ul(
children=[html.H3(html.Li('Amount of Logs: {}'.format(allg["length"])), style={'padding-left': '50px', }),
html.H3(html.Li('Starting from: {}'.format(
allg["start"])), style={'padding-left': '50px', }),
html.H3(html.Li('Ending with: {}'.format(allg["end"])), style={
'padding-left': '50px', }),
html.H3(html.Li('Evaluating Logs off: {} Days'.format(
allg["delta"])), style={'padding-left': '50px', }),
], style={'padding-left': '50px', }
)
if __name__ == "__main__":
multiprocessing.set_start_method('fork')
parser = argparse.ArgumentParser(description='Path to LogFile')
parser.add_argument("--path", type=str, default="access_ssl_log")
args = parser.parse_args()
path = args.path
main(path)