-
Notifications
You must be signed in to change notification settings - Fork 1
/
flask_app.py
109 lines (93 loc) · 2.95 KB
/
flask_app.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
import os
import logging
import logging.config
from logging import handlers
import json
import requests
from flask import Flask, jsonify, request, session, g, redirect, url_for, render_template, Response
app = Flask(__name__)
app.config.from_object(__name__)
app.logger.setLevel(logging.DEBUG)
handler = handlers.RotatingFileHandler(
os.path.join(app.instance_path, 'flask.log'),
maxBytes=1024 * 1024 * 100,
backupCount=20)
app.logger.addHandler(handler)
elasticsearch_url = os.getenv('ELASTICSEARCH_URL', default='http://localhost:9200')
app.config.update(dict(
# DATABASE=os.path.join(app.instance_path, 'sqlite3.db'),
DEBUG=True,
USERNAME='admin',
PASSWORD='default',
SECRET_KEY='INSECURE_DEVELOPMENT_KEY',
PROPAGATE_EXCEPTIONS=True
))
@app.errorhandler(404)
def page_not_found(error):
return 'This route does not exist {}'.format(request.url), 404
@app.route('/')
def welcome():
return render_template('index.html')
@app.route('/bytes-in-format')
def bytes_in_format():
return format_report(inbytes=True)
@app.route('/files-in-format')
def format_report(inbytes=False):
inventory_url = "{0}/ciber-file-extensions/_search".format(elasticsearch_url)
sort_field = 'bytes_in_format.value' if inbytes else 'doc_count'
q_extensions = '''
{{
"size": 100,
"_source": [ "mimetype", "key", "doc_count", "bytes_in_format.value" ],
"query": {{
"match_all": {{}}
}},
"sort": [
{{ "{0}": "desc" }}
]
}}'''.format(sort_field)
headers = {'cache-control': "no-cache"}
response = requests.request("POST", inventory_url, data=q_extensions, headers=headers).json()
return jsonify(response)
@app.route('/recent_sims')
def recent_sims():
url = "{0}/gatling-bd-%2A/_search".format(elasticsearch_url)
payload = '''
{
"size": 0,
"aggs": {
"simulations": {
"terms": {
"field": "_index",
"size": 10,
"order": {
"first_ts": "desc"
}
},
"aggs": {
"last_ts": {
"max": {
"field": "@timestamp"
}
},
"first_ts": {
"min": {
"field": "@timestamp"
}
},
"req_duration_stats" : { "stats" : { "field" : "responseDuration" } },
"status_codes": { "terms": { "field": "statusCode.keyword" } },
"sim_class": { "terms": { "field": "simulationClass.keyword" } }
}
}
}
}'''
headers = {
'content-type': "application/json",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers).json()
response = response['aggregations']['simulations']['buckets']
return jsonify(response)
if __name__ == '__main__':
app.run("0.0.0.0", processes=5)