Commit 44f7dc26 authored by Nicolas Herbaut's avatar Nicolas Herbaut
Browse files

folored matrix

parent 85a7c82c
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, render_template
from flowmatrix.flowlib import get_data_full, get_data
import argparse
import os
app = Flask(__name__)
parser = argparse.ArgumentParser(description='Start Flask app for flow matrix')
parser.add_argument('--influxdb_host', type=str, help='hostname or IP for the influxdb server', required=True)
parser.add_argument('--flow_matrix_port', type=int, help='the port on which to run the flow_matrix', default=5011)
parser.add_argument('--influxdb_port', type=str, help='port of the influxdb server', default="8086")
parser.add_argument('--influxdb_database', type=str, help='database to used in the influxdb server', default="telegraf")
args = parser.parse_args()
@app.route("/")
def data():
return render_template('index.html', data=get_data(args.influxdb_host, args.influxdb_port))
@app.route("/full")
def data_full():
return render_template('index.html', data=get_data_full(args.influxdb_host, args.influxdb_port))
app.template_folder=os.path.join(app.root_path,"../../flowmatrix/templates/")
app.run(host="0.0.0.0", port=args.flow_matrix_port)
from influxdb import DataFrameClient
import pandas as pd
from colour import Color
import numpy as np
white = Color("white")
to_blue = list(white.range_to(Color("blue"), 10))
to_red = list(white.range_to(Color("red"), 10))
query_template = """SELECT last("bytes") as value FROM "telegraf"."autogen"."nftables" WHERE time > now() - 1h AND "host_app_dst"='%s' AND "host_app_src"='%s' GROUP BY time(10w) FILL(null)"""
query_template_full = """SELECT last("bytes") as value FROM "telegraf"."autogen"."nftables" WHERE time > now() - 1h AND "host_app_dst_port"='%s' AND "host_app_src_port"='%s' GROUP BY time(10w) FILL(null)"""
def sizeof_get_color(num, matrix_mean=0, std_dev=999999999999):
if num < matrix_mean:
col = to_blue[min(int(abs(num - matrix_mean) / std_dev), len(to_blue)-1)]
else:
col = to_red[min(int(abs(num - matrix_mean) / std_dev), len(to_red)-1)]
return col.get_web()
def sizeof_fmt(num, suffix='B'):
if num == 0:
return ""
......@@ -17,7 +32,6 @@ def sizeof_fmt(num, suffix='B'):
def matrix_value(template, client, a, b):
query = template % (a, b)
print(query)
res = client.query(query)
if 'nftables' in res:
if 'value' in res["nftables"]:
......@@ -26,31 +40,39 @@ def matrix_value(template, client, a, b):
return 0
def get_data(influxdb_host, influxdb_port):
def get_data(influxdb_host, influxdb_port, formatter=sizeof_fmt):
client = DataFrameClient(influxdb_host, influxdb_port, "", "", "telegraf")
df = pd.DataFrame(data=list(range(30)))
apps = [app["value"] for app in list(client.query('SHOW TAG VALUES ON "telegraf" WITH KEY="host_app_src"'))[0] if
'salt' not in app["value"]]
matrix = pd.DataFrame.from_items(
items=[(a, [sizeof_fmt(matrix_value(query_template, client, a, b)) for b in apps]) for a in apps],
items=[(a, [matrix_value(query_template, client, a, b) for b in apps]) for a in apps],
columns=apps,
orient="index")
return matrix.to_dict()
matrix_mean = np.mean((matrix!=0).mean())
matrix_std = np.mean((matrix.std()))
def get_data_full(influxdb_host, influxdb_port):
client = DataFrameClient(influxdb_host, influxdb_port, "", "", "telegraf")
def custo_formatter(v):
col=sizeof_get_color(v,matrix_mean,matrix_std )
return (col,formatter(v))
df = pd.DataFrame(data=list(range(30)))
matrix = matrix.applymap(custo_formatter)
return matrix
def get_data_full(influxdb_host, influxdb_port, formatter=sizeof_fmt):
client = DataFrameClient(influxdb_host, influxdb_port, "", "", "telegraf")
apps = [app["value"] for app in list(client.query('SHOW TAG VALUES ON "telegraf" WITH KEY="host_app_src_port"'))[0]
if 'salt' not in app["value"]]
matrix = pd.DataFrame.from_items(
items=[(a, [sizeof_fmt(matrix_value(query_template_full, client, a, b)) for b in apps]) for a in apps],
items=[(a, [matrix_value(query_template_full, client, a, b) for b in apps]) for a in apps],
columns=apps,
orient="index")
return matrix.to_dict()
matrix.applymap(formatter)
return matrix
......@@ -30,7 +30,9 @@
<th scope="row">{{ key1 }}</th>
{% for key2 in data %}
<td>{{data[key2][key1]}}</td>
<td bgcolor="{{ data[key2][key1][0] }}">{{data[key2][key1][1]}}</td>
{% endfor %}
</tr>
......
......@@ -10,7 +10,7 @@ setup(
author='Nicolas Herbaut',
author_email='nicolas.herbaut@univ-grenoble-alpes.fr',
url='https://nextnet.top',
scripts=['bin/flow-matrix'],
scripts=['flow-matrix'],
include_package_data=True,
zip_safe=False,
install_requires=[
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment