mirror of
https://github.com/FAUSheppy/icinga-webhook-gateway
synced 2025-12-07 16:01:38 +01:00
feat: service deletions & css
This commit is contained in:
47
server.py
47
server.py
@@ -11,8 +11,8 @@ import sys
|
|||||||
import secrets
|
import secrets
|
||||||
|
|
||||||
import flask_wtf
|
import flask_wtf
|
||||||
from flask_wtf import FlaskForm, CSRFProtect
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField, SubmitField, BooleanField, DecimalField
|
from wtforms import StringField, SubmitField, BooleanField, DecimalField, HiddenField
|
||||||
from wtforms.validators import DataRequired, Length
|
from wtforms.validators import DataRequired, Length
|
||||||
|
|
||||||
from sqlalchemy import Column, Integer, String, Boolean, or_, and_
|
from sqlalchemy import Column, Integer, String, Boolean, or_, and_
|
||||||
@@ -24,7 +24,6 @@ from flask_sqlalchemy import SQLAlchemy
|
|||||||
from sqlalchemy.sql.expression import func
|
from sqlalchemy.sql.expression import func
|
||||||
|
|
||||||
app = flask.Flask("Icinga Report In Gateway")
|
app = flask.Flask("Icinga Report In Gateway")
|
||||||
CSRFProtect(app)
|
|
||||||
|
|
||||||
|
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
|
||||||
@@ -89,11 +88,12 @@ def overview():
|
|||||||
status = Status(service=service.service, timestamp=0, status="UNKOWN")
|
status = Status(service=service.service, timestamp=0, status="UNKOWN")
|
||||||
else:
|
else:
|
||||||
status_time_parsed = datetime.datetime.fromtimestamp(status.timestamp)
|
status_time_parsed = datetime.datetime.fromtimestamp(status.timestamp)
|
||||||
status_age_seconds = datetime.datetime.now() - time_parsed
|
status_age = datetime.datetime.now() - status_time_parsed
|
||||||
|
|
||||||
# check service timeout #
|
# check service timeout #
|
||||||
|
print(service.timeout, service, service.token)
|
||||||
timeout = datetime.timedelta(seconds=service.timeout)
|
timeout = datetime.timedelta(seconds=service.timeout)
|
||||||
if delta > timeout:
|
if status_age > timeout:
|
||||||
status.status = "WARNING"
|
status.status = "WARNING"
|
||||||
|
|
||||||
status_unique_results.append(status)
|
status_unique_results.append(status)
|
||||||
@@ -103,15 +103,18 @@ def overview():
|
|||||||
|
|
||||||
class EntryForm(FlaskForm):
|
class EntryForm(FlaskForm):
|
||||||
|
|
||||||
service = StringField("Service Name", validators=[DataRequired()])
|
service = StringField("Service Name")
|
||||||
|
service_hidden = HiddenField("service_hidden")
|
||||||
timeout = DecimalField("Timeout in days", default=30)
|
timeout = DecimalField("Timeout in days", default=30)
|
||||||
|
|
||||||
def create_entry(form, user):
|
def create_entry(form, user):
|
||||||
|
|
||||||
# TODO add entry to icinga
|
# TODO add entry to icinga
|
||||||
token = secrets.token_urlsafe(16)
|
token = secrets.token_urlsafe(16)
|
||||||
print(form.timeout.data)
|
|
||||||
service = Service(service=form.service.data, timeout=int(form.timeout.data),
|
service_name = form.service.data or form.service_hidden.data
|
||||||
|
|
||||||
|
service = Service(service=service_name, timeout=int(form.timeout.data),
|
||||||
owner=user, token=token)
|
owner=user, token=token)
|
||||||
db.session.merge(service)
|
db.session.merge(service)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
@@ -137,11 +140,26 @@ def service_details():
|
|||||||
user=user)
|
user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/entry-form", methods=["GET", "POST"])
|
@app.route("/entry-form", methods=["GET", "POST", "DELETE"])
|
||||||
def create_interface():
|
def create_interface():
|
||||||
|
|
||||||
user = flask.request.headers.get("X-Preferred-Username")
|
user = flask.request.headers.get("X-Preferred-Username")
|
||||||
|
|
||||||
|
# check if is delete #
|
||||||
|
operation = flask.request.args.get("operation")
|
||||||
|
if operation and operation == "delete" :
|
||||||
|
|
||||||
|
service_delete_name = flask.request.args.get("service")
|
||||||
|
service_del_object = db.session.query(Service).filter(Service.service==service_delete_name,
|
||||||
|
Service.owner==user).first()
|
||||||
|
if not service_del_object:
|
||||||
|
return ("Failed to delete the requested service", 404)
|
||||||
|
|
||||||
|
db.session.delete(service_del_object)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return flask.redirect("/overview")
|
||||||
|
|
||||||
form = EntryForm()
|
form = EntryForm()
|
||||||
|
|
||||||
# handle modification #
|
# handle modification #
|
||||||
@@ -151,14 +169,17 @@ def create_interface():
|
|||||||
if service:
|
if service:
|
||||||
form.service.default = service.service
|
form.service.default = service.service
|
||||||
form.timeout.default = service.timeout
|
form.timeout.default = service.timeout
|
||||||
|
form.service_hidden.default = service.service
|
||||||
form.process()
|
form.process()
|
||||||
else:
|
else:
|
||||||
return ("Not a valid service to modify", 404)
|
return ("Not a valid service to modify", 404)
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if flask.request.method == "POST":
|
||||||
create_entry(form, user)
|
create_entry(form, user)
|
||||||
return flask.redirect('/service-details?service={}'.format(form.service.data))
|
service_name = form.service.data or form.service_hidden.data
|
||||||
return flask.render_template('add_modify_service.html', form=form,
|
return flask.redirect('/service-details?service={}'.format(service_name))
|
||||||
|
else:
|
||||||
|
return flask.render_template('add_modify_service.html', form=form,
|
||||||
is_modification=bool(modify_service_name))
|
is_modification=bool(modify_service_name))
|
||||||
|
|
||||||
@app.route('/alive')
|
@app.route('/alive')
|
||||||
|
|||||||
@@ -36,6 +36,46 @@ body{
|
|||||||
color: black;
|
color: black;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.overview-tile-titel{
|
||||||
|
color: black;
|
||||||
|
display: inline-block;
|
||||||
|
width: fit-content;
|
||||||
|
float: left;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.overview-tile-status{
|
||||||
|
color: black;
|
||||||
|
display: inline-block;
|
||||||
|
width: fit-content;
|
||||||
|
float: right;
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.overview-tile-info-text{
|
||||||
|
clear: both;
|
||||||
|
float: left;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.overview-tile-date{
|
||||||
|
clear: both;
|
||||||
|
float: right;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.unknown{
|
||||||
|
color: pink;
|
||||||
|
}
|
||||||
|
|
||||||
|
.warning{
|
||||||
|
color: orange;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error{
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
|
||||||
.last-status{
|
.last-status{
|
||||||
margin-top: 10px;
|
margin-top: 10px;
|
||||||
box-shadow: 0px 0px 4px 3px rgba(0,0,0,0.5);
|
box-shadow: 0px 0px 4px 3px rgba(0,0,0,0.5);
|
||||||
@@ -104,6 +144,10 @@ body{
|
|||||||
padding-top: 2px;
|
padding-top: 2px;
|
||||||
|
|
||||||
color: black;
|
color: black;
|
||||||
|
|
||||||
|
border: none;
|
||||||
|
outline: none;
|
||||||
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.box{
|
.box{
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
<form class="mt-4" method="POST" action="/entry-form">
|
<form class="mt-4" method="POST" action="/entry-form">
|
||||||
|
|
||||||
{{ form.csrf_token }}
|
{{ form.service_hidden }}
|
||||||
{{ form.service.label }}
|
{{ form.service.label }}
|
||||||
|
|
||||||
{% if is_modification %}
|
{% if is_modification %}
|
||||||
|
|||||||
@@ -17,12 +17,28 @@
|
|||||||
style="background-color: magenta;"
|
style="background-color: magenta;"
|
||||||
{% endif %}
|
{% endif %}
|
||||||
>
|
>
|
||||||
<p class="w-100 font-weight-bold">{{ status.service }}</p>
|
<div class="overview-tile-titel mb-2">{{ status.service }}</div>
|
||||||
|
<div class="overview-tile-status {{ status.status.lower() }}">
|
||||||
|
{{ status.status }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="overview-tile-info-text">
|
||||||
|
<small>
|
||||||
|
{% if status.info_text %}
|
||||||
|
{{ status.info_text }}
|
||||||
|
{% else %}
|
||||||
|
No extra info
|
||||||
|
{% endif %}
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<small class="overview-tile-date pt-2">
|
||||||
{% if status.timestamp == 0 %}
|
{% if status.timestamp == 0 %}
|
||||||
Service never reported in
|
Service never reported in
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ datetime.fromtimestamp(status.timestamp).strftime("%H:%M %d.%m.%y") }}
|
{{ datetime.fromtimestamp(status.timestamp).strftime("%H:%M on %d.%b.%y") }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
</small>
|
||||||
</a>
|
</a>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -8,8 +8,7 @@
|
|||||||
<a class="service-info-button mt-3" style="background-color: orange;"
|
<a class="service-info-button mt-3" style="background-color: orange;"
|
||||||
href="/entry-form?service={{ service.service }}">Modify</a>
|
href="/entry-form?service={{ service.service }}">Modify</a>
|
||||||
<a class="service-info-button mt-3" style="background-color: red;"
|
<a class="service-info-button mt-3" style="background-color: red;"
|
||||||
href="#"
|
href="/entry-form?service={{ service.service }}&operation=delete">Delete</a>
|
||||||
onclick="alert('Delete is not supported yet')">Delete</a>
|
|
||||||
|
|
||||||
<div class="last-status">
|
<div class="last-status">
|
||||||
{% if status_list | length > 0 %}
|
{% if status_list | length > 0 %}
|
||||||
@@ -30,8 +29,8 @@
|
|||||||
<div class="ml-3 example">
|
<div class="ml-3 example">
|
||||||
curl -X POST \ <br>
|
curl -X POST \ <br>
|
||||||
<div class="example-indent">
|
<div class="example-indent">
|
||||||
-H "application/json" \ <br>
|
-H "Content-Type: application/json" \ <br>
|
||||||
-d '{ "service_name" : "{{ service.service }}",
|
-d '{ "service" : "{{ service.service }}",
|
||||||
"token" : "{{ service.token }}", \<br>
|
"token" : "{{ service.token }}", \<br>
|
||||||
"status" : "OK", "info" : "Free Text Information here" }' \<br>
|
"status" : "OK", "info" : "Free Text Information here" }' \<br>
|
||||||
{{ flask.request.url_root }}
|
{{ flask.request.url_root }}
|
||||||
|
|||||||
Reference in New Issue
Block a user