Newer
Older
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Requirements:
pip3 install paramiko
pip3 install pandas
grep in PATH
"""
import os
import io
import re
import sys
import json
import paramiko
import numpy
import pandas as pd
from subprocess import Popen, PIPE
from datetime import datetime
def make_regex():
log_levels = [
"INF",
" \\* ",
"ERR",
"ATT", # For restart detection
#"DBG",
]
categories = [
"main",
"chain_net",
"dap_chain_net_srv_vpn",
"dap_chain_net_srv_vpn_cdb_auth",
]
log_levels_str = "|".join(log_levels)
categories_str = "|".join(categories)
return "\\[(%s)\\] \\[(%s)\\]" % (log_levels_str, categories_str)
def string_to_datetime(date_string):
dt = datetime.strptime(date_string, '%m/%d/%y-%H:%M:%S')
return numpy.datetime64(dt)
def parse_log(log_stream):
datetime_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\]")
mode_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[ATT\] \[main\] \*\*\* (DEBUG|NORMAL) MODE \*\*\*")
node_address_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[ \* \] \[chain_net\] Parse node addr ([\d\w:]{22}) successfully")
is_cdb_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[ \* \] \[main\] Central DataBase \(CDB\) is initialized")
cdb_login_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[INF\] \[dap_chain_net_srv_vpn_cdb_auth\] Login: Successfuly logined user")
cdb_register_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[ \* \] \[dap_chain_net_srv_vpn_cdb_auth\] Registration: new user")
node_connected_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[ \* \] \[chain_net\] Connected link")
vpn_connect_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[ \* \] \[dap_chain_net_srv_vpn\] VPN client address ([\d\.]+) leased")
vpn_disconnect_pattern = re.compile(r"\[(\d\d/\d\d/\d\d-\d\d:\d\d:\d\d)\] \[DBG\] \[dap_chain_net_srv_vpn\] Unlease address ([\d\.]+)")
def extract_datetime(log_Line):
m = datetime_pattern.match(log_Line)
if m:
return string_to_datetime(m.groups()[0])
def on_cbd_string(match, info):
info["is_cdb"] = True
def on_node_address_string(match, info):
node_address = match.groups()[1]
info["node_address"] = node_address
def on_login_string(match, info):
dt_str = match.groups()[0]
dt = string_to_datetime(dt_str)
info["cdb_logins"].append(dt)
def on_register_string(match, info):
dt_str = match.groups()[0]
dt = string_to_datetime(dt_str)
info["cdb_registrations"].append(dt)
def on_node_connection_string(match, info):
dt_str = match.groups()[0]
dt = string_to_datetime(dt_str)
info["node_connections"].append(dt)
def on_vpn_connect_string(match, info):
dt_str = match.groups()[0]
dt = string_to_datetime(dt_str)
some_id = match.groups()[1]
info["connection_dates"].append(dt)
info["connection_ids"].append(some_id)
def on_vpn_disconnect_string(match, info):
dt_str = match.groups()[0]
dt = string_to_datetime(dt_str)
some_id = match.groups()[1]
info["disconnection_dates"].append(dt)
info["disconnection_ids"].append(some_id)
actions = [
(is_cdb_pattern, on_cbd_string),
(node_address_pattern, on_node_address_string),
(cdb_login_pattern, on_login_string),
(cdb_register_pattern, on_register_string),
(node_connected_pattern, on_node_connection_string),
(vpn_connect_pattern, on_vpn_connect_string),
(vpn_disconnect_pattern, on_vpn_disconnect_string),
]
print("Parsing logs")
data = []
current_info = None
for line in log_stream:
if mode_pattern.match(line) is not None:
if current_info is not None:
data.append(current_info)
current_info = {
"is_cdb": False,
'node_address': '__UNKNOWN__',
"cdb_logins": [],
"cdb_registrations": [],
"node_connections" : [],
"connection_dates" : [],
"connection_ids": [],
"disconnection_dates" : [],
"disconnection_ids": [],
}
else:
for pattern, action in actions:
m = pattern.match(line)
if m is not None:
action(m, current_info)
if current_info:
data.append(current_info)
print("Done parsing logs")
return data
def local_run(log_path):
regex_str = make_regex()
proc = Popen(["grep", "-E", regex_str, log_path], stdout=PIPE, stderr=PIPE, stdin=PIPE)
proc.stdin.close()
data = parse_log(io.TextIOWrapper(proc.stdout))
proc.wait()
if proc.returncode != 0:
print("Error code:", proc.returncode)
print("Error msg:", proc.stderr.read())
return proc.returncode, data
def make_filtering_cmd(log_path):
regex_str = make_regex()
return 'grep -E "%s" "%s"' % (regex_str, log_path)
def remote_run(log_path, server, username, password):
cmd = make_filtering_cmd(log_path)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
ssh_stdin.close()
data = parse_log(ssh_stdout)
# TODO: error check?
errcode = 0
error_string = ssh_stderr.read()
if error_string:
print("Errors:", error_string)
ssh.close()
return errcode, data
def count_datetime_list(data, list_name):
count_label = 'count'
# Step #0: Filter irrelevant data out
filtered_data = filter(lambda x: len(x[list_name]) > 0, data)
# Step #1: Make dataframes
dicts = map(lambda x: {'node_address': x['node_address'], list_name: x[list_name]}, filtered_data)
dataframes = list(map(lambda x: pd.DataFrame(data=x), dicts))
if len(dataframes) == 0:
return []
elif len(dataframes) == 1:
df = dataframes[0]
else:
df = pd.concat(dataframes)
# Step #2: Calculate stats
days = df[list_name].dt.floor('D')
count_by_day = df.groupby(['node_address', days]).count()
count_by_day = count_by_day.rename(columns={list_name: count_label}).reset_index()
months = count_by_day[list_name].dt.month_name()
month_groups = count_by_day.groupby(['node_address', months])[count_label]
result = month_groups.agg(['min', 'median', 'max'])
result = result.reset_index().rename(columns={list_name: 'month'})
# total
result_total = count_by_day.groupby([months])[count_label].agg(['min', 'median', 'max'])
result_total = result_total.reset_index().rename(columns={list_name: 'month'})
result_total.insert(0, 'node_address', [' ALL NODES '])
return result_total.append(result)
def duration_for_paired_events(data, left_dates_column, left_id_column, right_dates_column, right_id_column):
count_label = 'count'
filtered_data = list(filter(lambda x: len(x[left_dates_column]) > 0 and len(x[right_dates_column]) > 0, data))
# TODO: check inconsistent data (if colum length != id length)
dicts_left = map(lambda x: {'node_address': x['node_address'], left_dates_column: x[left_dates_column], left_id_column: x[left_id_column]}, filtered_data)
dicts_right = map(lambda x: {'node_address': x['node_address'], right_dates_column: x[right_dates_column], right_id_column: x[right_id_column]}, filtered_data)
dataframes_left = map(lambda x: pd.DataFrame(data=x), dicts_left)
dataframes_right = map(lambda x: pd.DataFrame(data=x), dicts_right)
# merging (joining)
dataframes = []
for df_left, df_right in zip(dataframes_left, dataframes_right):
df = pd.merge(df_left, df_right, left_on=['node_address', left_id_column], right_on=['node_address', right_id_column])
durations = map(lambda lr: (lr[1] - lr[0]).total_seconds(), zip(df[left_dates_column], df[right_dates_column]))
df['durations'] = list(durations)
df_small = df.drop([left_id_column, right_id_column, right_dates_column], axis=1)
dataframes.append(df_small)
if len(dataframes) == 0:
return [], []
if len(dataframes) == 1:
df = dataframes[0]
else:
df = pd.concat(dataframes)
# Durations
months = df[left_dates_column].dt.month_name()
month_groups = df.groupby(['node_address', months])['durations']
result = month_groups.agg(['min', 'median', 'max'])
result = result.reset_index().rename(columns={left_dates_column: 'month'})
# total count
result_total = df.groupby([months])['durations'].agg(['min', 'median', 'max'])
result_total = result_total.reset_index().rename(columns={left_dates_column: 'month'})
result_total.insert(0, 'node_address', [' ALL NODES '])
return result_total.append(result)
def do_stats(data):
def print_stats(label, stats):
print()
print()
print(label)
if len(stats) > 0:
print(stats)
else:
print("NO DATA")
cdb_data = list(filter(lambda x: x['is_cdb'], data))
not_cdb_data = list(filter(lambda x: not x['is_cdb'], data))
logins = count_datetime_list(cdb_data, 'cdb_logins')
registrations = count_datetime_list(cdb_data, 'cdb_registrations')
node_connections_cdb = count_datetime_list(cdb_data, 'node_connections')
node_connections_not_cdb = count_datetime_list(not_cdb_data, 'node_connections')
vpn_connection_count = count_datetime_list(not_cdb_data, 'connection_dates')
#vpn_connection_durration = duration_for_paired_events(not_cdb_data, 'connection_dates', 'connection_ids', 'disconnection_dates', 'disconnection_ids')
print_stats("[CDB] Authorization count:", logins)
print_stats("[CDB] Registration count:", registrations)
print_stats("[CDB] Node connections count:", node_connections_cdb)
print_stats("[NOT CDB] Node connections count:", node_connections_not_cdb)
print_stats("[NOT CDB] VPN connections count:", vpn_connection_count)
#print_stats("[NOT CDB] VPN connections duration:", vpn_connection_durration)
def load_config():
print("Loading config...")
if not os.path.isfile("stats.cfg"):
print("Config file 'stats.cfg' is missing.")
exit(-1)
with open("stats.cfg") as f:
cfg = json.load(f)
if "run" not in cfg:
print('"run" key with value "local" or "remote" is missing')
exit(-1)
if cfg["run"] == "local":
if "local_files" not in cfg:
print('"local_files" key is missing')
exit(-1)
elif cfg["run"] == "remote":
if "nodes" not in cfg:
print('"nodes" key is missing')
exit(-1)
for node_info in cfg["nodes"]:
for key in ["address", "username", "password", "logfile_path"]:
if key not in node_info:
print('One of nodes does not contain', key, 'key')
exit(-1)
else:
print('"run" value must be "local" or "remote"')
exit(-1)
return cfg
def main():
cfg = load_config()
result_data = []
if cfg["run"] == "remote":
l = len(cfg["nodes"])
for i, node_info in enumerate(cfg["nodes"]):
print(i + 1, '/', l, "nodes")
errcode, data = remote_run(node_info["logfile_path"], node_info["address"], node_info["username"], node_info["password"])
if errcode == 0:
result_data.extend(data)
elif cfg["run"] == "local":
l = len(cfg["local_files"])
for i, log_path in enumerate(cfg["local_files"]):
print(i + 1, '/', l, "files")
errcode, data = local_run(log_path)
if errcode == 0:
result_data.extend(data)
do_stats(result_data)
if __name__ == '__main__':
main()