mirror of
https://github.com/FAUSheppy/icinga-webhook-gateway
synced 2025-12-06 15:31:38 +01:00
fix: show stateless services in overview
This commit is contained in:
47
server.py
47
server.py
@@ -23,19 +23,25 @@ app.config['JSON_CONFIG_DIR'] = 'config'
|
|||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
class Service(db.Model):
|
class Service(db.Model):
|
||||||
|
|
||||||
__tablename__ = "services"
|
__tablename__ = "services"
|
||||||
|
|
||||||
service = Column(String, primary_key=True)
|
service = Column(String, primary_key=True)
|
||||||
token = Column(String)
|
token = Column(String)
|
||||||
timeout = Column(Integer)
|
timeout = Column(Integer)
|
||||||
|
owner = Column(String)
|
||||||
|
|
||||||
class Status(db.Model):
|
class Status(db.Model):
|
||||||
|
|
||||||
__tablename__ = "states"
|
__tablename__ = "states"
|
||||||
|
|
||||||
service = Column(String, primary_key=True)
|
service = Column(String, primary_key=True)
|
||||||
timestamp = Column(Integer, primary_key=True)
|
timestamp = Column(Integer, primary_key=True)
|
||||||
status = Column(String)
|
status = Column(String)
|
||||||
info_text = Column(String)
|
info_text = Column(String)
|
||||||
|
|
||||||
def buildReponseDict(status, service=None):
|
def buildReponseDict(status, service=None):
|
||||||
|
|
||||||
if not status:
|
if not status:
|
||||||
return { "service" : service,
|
return { "service" : service,
|
||||||
"status" : "UNKOWN",
|
"status" : "UNKOWN",
|
||||||
@@ -49,20 +55,35 @@ def buildReponseDict(status, service=None):
|
|||||||
|
|
||||||
@app.route('/overview')
|
@app.route('/overview')
|
||||||
def overview():
|
def overview():
|
||||||
baseQuery = db.session.query(Status, func.max(Status.timestamp))
|
|
||||||
query = baseQuery.group_by(Status.service).order_by(Status.service)
|
|
||||||
results = query.all()
|
|
||||||
|
|
||||||
for status in results:
|
# query all services #
|
||||||
serviceObj = db.session.query(Service).filter(Service.service == status[0].service).first()
|
services = db.session.query(Service).all()
|
||||||
timeParsed = datetime.datetime.fromtimestamp(status[0].timestamp)
|
|
||||||
totalSeconds = (datetime.datetime.now() - timeParsed).total_seconds()
|
|
||||||
delta = datetime.timedelta(seconds=int(totalSeconds))
|
|
||||||
timeout = datetime.timedelta(seconds=serviceObj.timeout)
|
|
||||||
if delta > timeout:
|
|
||||||
status[0].status = "WARNING"
|
|
||||||
|
|
||||||
return flask.render_template("overview.html", services=results, datetime=datetime.datetime)
|
status_unique_results = []
|
||||||
|
|
||||||
|
for service in services:
|
||||||
|
|
||||||
|
# query latest status for service #
|
||||||
|
status_query = db.session.query(Status)
|
||||||
|
status_filter = status_query.filter(Status.service == service.service)
|
||||||
|
status = status_filter.order_by(sqlalchemy.desc(Status.timestamp)).first()
|
||||||
|
|
||||||
|
# parse time #
|
||||||
|
if not status:
|
||||||
|
status = Status(service=service.service, timestamp=0, status="UNKOWN")
|
||||||
|
else:
|
||||||
|
status_time_parsed = datetime.datetime.fromtimestamp(status.timestamp)
|
||||||
|
status_age_seconds = datetime.datetime.now() - time_parsed
|
||||||
|
|
||||||
|
# check service timeout #
|
||||||
|
timeout = datetime.timedelta(seconds=service.timeout)
|
||||||
|
if delta > timeout:
|
||||||
|
status.status = "WARNING"
|
||||||
|
|
||||||
|
status_unique_results.append(status)
|
||||||
|
|
||||||
|
return flask.render_template("overview.html", status_list=status_unique_results,
|
||||||
|
datetime=datetime.datetime)
|
||||||
|
|
||||||
@app.route('/alive')
|
@app.route('/alive')
|
||||||
def alive():
|
def alive():
|
||||||
@@ -197,4 +218,4 @@ if __name__ == "__main__":
|
|||||||
with app.app_context():
|
with app.app_context():
|
||||||
create_app()
|
create_app()
|
||||||
|
|
||||||
app.run(host=args.interface, port=args.port)
|
app.run(host=args.interface, port=args.port, debug=True)
|
||||||
|
|||||||
@@ -26,20 +26,24 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
{% for service in services %}
|
{% for status in status_list %}
|
||||||
<div class="col-md-5 m-3 p-2 border rounded"
|
<div class="col-md-5 m-3 p-2 border rounded"
|
||||||
{% if service[0].status == "OK" %}
|
{% if status.status == "OK" %}
|
||||||
style="background-color: lightgreen;"
|
style="background-color: lightgreen;"
|
||||||
{% elif service[0].status == "WARNING" %}
|
{% elif status.status == "WARNING" %}
|
||||||
style="background-color: orange;"
|
style="background-color: orange;"
|
||||||
{% elif service[0].status == "CRITICAL" %}
|
{% elif status.status == "CRITICAL" %}
|
||||||
style="background-color: #ff00005c;"
|
style="background-color: #ff00005c;"
|
||||||
{% else %}
|
{% else %}
|
||||||
style="background-color: magenta;"
|
style="background-color: magenta;"
|
||||||
{% endif %}
|
{% endif %}
|
||||||
>
|
>
|
||||||
<p class="w-100 font-weight-bold">{{ service[0].service }}</p>
|
<p class="w-100 font-weight-bold">{{ status.service }}</p>
|
||||||
{{ datetime.fromtimestamp(service[0].timestamp).strftime("%H:%M %d.%m.%y") }}
|
{% if status.timestamp == 0 %}
|
||||||
|
Service never reported in
|
||||||
|
{% else %}
|
||||||
|
{{ datetime.fromtimestamp(status.timestamp).strftime("%H:%M %d.%m.%y") }}
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user