chg: display last logs

This commit is contained in:
Terrtia 2018-07-25 16:48:44 +02:00
parent 8beb63b6a8
commit 5ad5939bd5
No known key found for this signature in database
GPG key ID: 1E1B1F50D84613D0
4 changed files with 143 additions and 12 deletions

View file

@ -79,8 +79,6 @@ if __name__ == "__main__":
content = paste.get_p_content()
creds = set(re.findall(regex_cred, content))
publisher.warning('to_print')
if len(creds) == 0:
continue

View file

@ -5,9 +5,12 @@
Flask functions and routes for the dashboard page
'''
import json
import os
import datetime
import flask
from Date import Date
from flask import Flask, render_template, jsonify, request, Blueprint
# ============ VARIABLES ============
@ -35,6 +38,8 @@ def event_stream():
msg = {'channel': channel, 'type': type, 'pattern': pattern, 'data': data}
level = (msg['channel']).split('.')[1]
#if msg['type'] == 'pmessage' and (level == "WARNING" or level == "CRITICAL"):
#print(msg)
if msg['type'] == 'pmessage' and level != "DEBUG":
yield 'data: %s\n\n' % json.dumps(msg)
@ -62,12 +67,77 @@ def get_queues(r):
return newData
def get_date_range(date_from, num_day):
date = Date(str(date_from[0:4])+str(date_from[4:6]).zfill(2)+str(date_from[6:8]).zfill(2))
date_list = []
for i in range(0, num_day+1):
new_date = date.substract_day(i)
date_list.append(new_date[0:4] +'-'+ new_date[4:6] +'-'+ new_date[6:8])
return date_list
def dashboard_alert(log):
# check if we need to display this log
if len(log)>50:
date = log[1:5]+log[6:8]+log[9:11]
time = log[12:20]
log = log[46:].split(';')
if len(log) == 6:
res = {'date': date, 'time': time, 'script': log[0], 'domain': log[1], 'date_paste': log[2], 'paste': log[3], 'message': log[4]}
return res
else:
return False
else:
return False
# ============ ROUTES ============
@dashboard.route("/_logs")
def logs():
return flask.Response(event_stream(), mimetype="text/event-stream")
@dashboard.route("/_get_last_logs_json")
def get_last_logs_json():
date = datetime.datetime.now().strftime("%Y%m%d")
max_day_search = 6
day_search = 0
warning_found = 0
warning_to_found = 10
last_logs = []
date_range = get_date_range(date, max_day_search)
while max_day_search != day_search | warning_found != warning_to_found:
filename_warning_log = 'logs/Script_warn-'+ date_range[day_search] +'.log'
filename_log = os.path.join(os.environ['AIL_HOME'], filename_warning_log)
try:
with open(filename_log, 'r') as f:
lines = f.read().splitlines()
curr_index = -1
while warning_found != warning_to_found:
try:
# get lasts warning logs
log_warn = dashboard_alert(lines[curr_index])
if log_warn != False:
last_logs.append(log_warn)
warning_found = warning_found + 1
curr_index = curr_index - 1
except IndexError:
# check previous warning log file
day_search = day_search + 1
break
except FileNotFoundError:
# check previous warning log file
day_search = day_search + 1
return jsonify(last_logs)
@dashboard.route("/_stuff", methods=['GET'])
def stuff():

View file

@ -31,6 +31,14 @@
};
update_values();
</script>
<style>
.tableQueue tbody tr td,
.tableQueue tbody tr th,
.tableQueue thead tr td,
.tableQueue thead tr th{
padding: 1px;
}
</style>
</head>
<body>
@ -182,6 +190,58 @@
<script>
activePage = "page-index";
$("#"+activePage).addClass("active");
var tableBody = document.getElementById('tab_body')
$.getJSON('/_get_last_logs_json', function(data) {
console.log(data);
data.forEach(function (d) {
var tr = document.createElement('TR')
var time = document.createElement('TD')
var chan = document.createElement('TD')
var level = document.createElement('TD')
var scrpt = document.createElement('TD')
var src = document.createElement('TD')
var pdate = document.createElement('TD')
var nam = document.createElement('TD')
var msage = document.createElement('TD')
var inspect = document.createElement('TD')
time.appendChild(document.createTextNode(d.time))
chan.appendChild(document.createTextNode('Script'))
level.appendChild(document.createTextNode('WARNING'))
scrpt.appendChild(document.createTextNode(d.script))
src.appendChild(document.createTextNode(d.domain))
pdate.appendChild(document.createTextNode(d.date_paste))
nam.appendChild(document.createTextNode(d.paste))
var iconspan = document.createElement('SPAN');
var message = d.message.split(" ")
if (message[0] == "Detected"){
iconspan.className = "glyphicon glyphicon-eye-open";
}
else if (message[0] == "Checked"){
iconspan.className = "glyphicon glyphicon-thumbs-up";
}
iconspan.innerHTML = "&nbsp;";
msage.appendChild(iconspan);
msage.appendChild(document.createTextNode(message.join(" ")));
inspect.appendChild(document.createTextNode(' '))
tr.appendChild(time)
tr.appendChild(chan);
tr.appendChild(level);
tr.appendChild(scrpt);
tr.appendChild(src);
tr.appendChild(pdate);
tr.appendChild(nam);
tr.appendChild(msage);
tr.appendChild(inspect);
tableBody.appendChild(tr);
});
});
</script>
<script type="text/javascript">

View file

@ -134,6 +134,7 @@ function pad_2(number) {
}
function create_log_table(obj_json) {
console.log(obj_json);
tableBody = document.getElementById("tab_body")
var tr = document.createElement('TR')
var time = document.createElement('TD')
@ -281,7 +282,7 @@ function create_queue_table() {
document.getElementById("queueing").innerHTML = "";
var Tablediv = document.getElementById("queueing")
var table = document.createElement('TABLE')
table.className = "table table-bordered table-hover table-striped";
table.className = "table table-bordered table-hover table-striped tableQueue";
var tableHead = document.createElement('THEAD')
var tableBody = document.createElement('TBODY')
@ -295,7 +296,11 @@ function create_queue_table() {
for (i = 0; i < heading.length; i++) {
var th = document.createElement('TH')
if (heading[i] == "Amount") {
th.width = '50';
} else {
th.width = '100';
}
th.appendChild(document.createTextNode(heading[i]));
tr.appendChild(th);
}
@ -494,5 +499,3 @@ function manage_undefined() {
$(document).ready(function () {
manage_undefined();
});