-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
66 lines (53 loc) · 1.92 KB
/
demo.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
import pandas as pd
import numpy as np
import datetime as dt
import statsmodels.api as sm
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
from matplotlib.pyplot import figure
import re
import pydotplus
import csv
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import plot_confusion_matrix
from os import walk
from sklearn import tree, preprocessing
import joblib
from utils import *
from bashUtils import *
import constants
import io
import sys
import json
variables = json.load(open(f'variables.json',));
demoDirPath = variables["demoData"];
def addMissingEncodedColumns(df):
treeColumns = joblib.load('columns.txt')
df = df.drop(columns=list(filter(lambda c: not (c in treeColumns), df.columns.tolist())))
for c in treeColumns:
if not (c in df):
df[c] = 0
df = df[treeColumns]
return df
process = createArgusDaemonOutput(demoDirPath)
clf = joblib.load('bag.pkl')
print("Real time netflow");
while True:
header=True
for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
if not header:
df = getFlowDataFrame(line)
output = '{:^22}'.format(df[constants.FECHA][0]) + '{:^10}'.format(df[constants.PROTO][0]) + '{:^35}'.format(df[constants.SRCADDR][0])+ '{:^35}'.format(df[constants.DSTADDR][0])+ '{:^5}'.format(df[constants.STATE][0])+ '{:^10}'.format(df[constants.SUM][0])+"\n"
df = preprocesDataFrame(df)
df = addMissingEncodedColumns(df)
if clf.predict(df) == 1:
sys.stdout.shell.write(output, "COMMENT")
else:
sys.stdout.shell.write(output, "STRING")
else:
header=False
if input() == 'STOP':
process.kill()
break;