This commit is contained in:
2024-01-03 00:48:44 +01:00
parent c487c769b0
commit ebb644fdf2
2 changed files with 35 additions and 25 deletions

View File

@@ -41,6 +41,9 @@ def _build_service_name(user, async_service_name):
def create_service(user, async_service_name, app): def create_service(user, async_service_name, app):
if not app.config.get("ICINGA_API_URL"):
return
client = _create_client(app) client = _create_client(app)
name = _build_service_name(user, async_service_name) name = _build_service_name(user, async_service_name)
host_name = app.config["ASYNC_ICINGA_DUMMY_HOST"] host_name = app.config["ASYNC_ICINGA_DUMMY_HOST"]
@@ -67,6 +70,9 @@ def create_service(user, async_service_name, app):
def delete_service(user, async_service_name, app): def delete_service(user, async_service_name, app):
if not app.config.get("ICINGA_API_URL"):
return
client = _create_client(app) client = _create_client(app)
name = _build_service_name(user, async_service_name) name = _build_service_name(user, async_service_name)
host_name = app.config["ASYNC_ICINGA_DUMMY_HOST"] host_name = app.config["ASYNC_ICINGA_DUMMY_HOST"]

View File

@@ -1,4 +1,4 @@
!/usr/bin/python3 #!/usr/bin/python3
import time import time
import flask import flask
@@ -24,7 +24,6 @@ from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql.expression import func from sqlalchemy.sql.expression import func
import icingatools import icingatools
import smartanalysis
app = flask.Flask("Icinga Report In Gateway") app = flask.Flask("Icinga Report In Gateway")
@@ -66,7 +65,7 @@ class SMARTStatus(db.Model):
service = Column(String, primary_key=True) service = Column(String, primary_key=True)
timestamp = Column(Integer, primary_key=True) timestamp = Column(Integer, primary_key=True)
power_cycles = Column(Integer) power_cycles = Column(Integer)
temperatur = Column(Integer) temperature = Column(Integer)
available_spare = Column(Integer) available_spare = Column(Integer)
unsafe_shutdowns = Column(Integer) unsafe_shutdowns = Column(Integer)
critical_warning = Column(Integer) critical_warning = Column(Integer)
@@ -175,7 +174,7 @@ def service_details():
smart_entry = smart_entry_list.order_by(SMARTStatus.timestamp.desc()).first() smart_entry = smart_entry_list.order_by(SMARTStatus.timestamp.desc()).first()
return flask.render_template("service_info.html", service=service, flask=flask, return flask.render_template("service_info.html", service=service, flask=flask,
user=user, status_list=status_list, icinga_link=icinga_link, smart_entry=smart_entry) user=user, status_list=status_list, icinga_link=icinga_link, smart=smart_entry)
@app.route("/entry-form", methods=["GET", "POST", "DELETE"]) @app.route("/entry-form", methods=["GET", "POST", "DELETE"])
@@ -299,8 +298,8 @@ def default():
# get variables # # get variables #
service = flask.request.json.get("service") service = flask.request.json.get("service")
token = flask.request.json.get("token") token = flask.request.json.get("token")
status = flask.request.json["status"] status = flask.request.json.get("status")
text = flask.request.json["info"] text = flask.request.json.get("info") or "no_info"
timestamp = datetime.datetime.now().timestamp() timestamp = datetime.datetime.now().timestamp()
smart = flask.request.json.get("smart") smart = flask.request.json.get("smart")
@@ -309,6 +308,8 @@ def default():
return ("'service' ist empty field in json", 400) return ("'service' ist empty field in json", 400)
elif not token: elif not token:
return ("'token' ist empty field in json", 400) return ("'token' ist empty field in json", 400)
elif not status and not smart:
return ("'status' is empty field in json", 400)
# verify token & service in config # # verify token & service in config #
verifiedServiceObj = db.session.query(Service).filter( verifiedServiceObj = db.session.query(Service).filter(
@@ -337,26 +338,26 @@ def record_and_check_smart(service, timestamp, smart):
raise AssertionError("Trying to record SMART-record for non-SMART service") raise AssertionError("Trying to record SMART-record for non-SMART service")
# record the status # # record the status #
smart_status = SMARTStatus(service=service.service_name, timestamp=timestamp, smart_status = SMARTStatus(service=service.service, timestamp=timestamp,
temperature=health_info["temperature"] temperature=health_info["temperature"],
critical_warning=health_info["critical_warning"] critical_warning=health_info["critical_warning"],
unsafe_shutdowns=health_info["unsafe_shutdowns"] unsafe_shutdowns=health_info["unsafe_shutdowns"],
power_cycles=health_info["power_cycles"] power_cycles=health_info["power_cycles"],
power_on_hours=health_info["power_on_hours"] power_on_hours=health_info["power_on_hours"],
available_spare=health_info.get("available_spare") available_spare=health_info.get("available_spare"),
model_number=smart.get("model_name"])) model_number=smart.get("model_name"))
db.add(smart_status) db.session.add(smart_status)
db.commit() db.session.commit()
# check the status # # check the status #
smart_last_query = db.session.query(SMARTStatus) smart_last_query = db.session.query(SMARTStatus)
smart_last_query = smart_last_query.filter(SMARTStatus.service_name==service.service_name) smart_last_query = smart_last_query.filter(SMARTStatus.service==service.service)
smart_last = smart_last_query.order_by(sqlalchemy.desc(SMARTStatus.timestamp)).first() smart_last = smart_last_query.order_by(sqlalchemy.desc(SMARTStatus.timestamp)).first()
smart_second_last = smart_last_query.order_by(sqlalchemy.desc(SMARTStatus.timestamp)).offset(1).first() smart_second_last = smart_last_query.order_by(sqlalchemy.desc(SMARTStatus.timestamp)).offset(1).first()
# last record (max 6 months ago) # # last record (max 6 months ago) #
timestampt_minus_6m = datetime.datetime.now() - datetime.timedelta(months=6) timestampt_minus_6m = datetime.datetime.now() - datetime.timedelta(days=180)
smart_old_query = smart_last_query.filter(SMARTStatus.timestamp > timestampt_minus_6m.timestamp()) smart_old_query = smart_last_query.filter(SMARTStatus.timestamp > timestampt_minus_6m.timestamp())
smart_old = smart_old_query.order_by(sqlalchemy.asc(SMARTStatus.timestamp)).first() smart_old = smart_old_query.order_by(sqlalchemy.asc(SMARTStatus.timestamp)).first()
@@ -365,7 +366,7 @@ def record_and_check_smart(service, timestamp, smart):
return ("Disk Temperatur {}".format(smart_last.temperature), "CRITICAL") return ("Disk Temperatur {}".format(smart_last.temperature), "CRITICAL")
# critial != 0 # # critial != 0 #
if smart_last_query.critial != 0: if smart_last.critical_warning != 0:
return ("SMART reports disk critical => oO better do something about this", "CRITICAL") return ("SMART reports disk critical => oO better do something about this", "CRITICAL")
# unsafe_shutdowns +1 # # unsafe_shutdowns +1 #
@@ -376,13 +377,13 @@ def record_and_check_smart(service, timestamp, smart):
spare = smart_old.available_spare - smart_last.available_spare spare = smart_old.available_spare - smart_last.available_spare
if spare >= 10: if spare >= 10:
return ("Strong degration in SSD spare space ({} in under 6 months)".format( return ("Strong degration in SSD spare space ({} in under 6 months)".format(
spare_change, "WARNING") spare_change, "WARNING"))
# available_spare # # available_spare #
if smart_last.available_spare < 50: if smart_last.available_spare < 50:
return ("Available SSD spare <50 ({})".format(spare_change, "WARNING") return ("SSD spare <50 ({})".format(spare_change, "WARNING"))
elif smart_last.available_spare <25: elif smart_last.available_spare < 25:
return ("Available SSD spare <25 ({}) YOUR DISK WILL DIE SOON".format(spare_change, "CRITIAL") return ("SSD spare <25 ({}) YOUR DISK WILL DIE SOON".format(spare_change, "CRITIAL"))
return ("OK", "") return ("OK", "")
@@ -426,8 +427,11 @@ def create_app():
owner=config[key]["owner"])) owner=config[key]["owner"]))
db.session.commit() db.session.commit()
# create dummy host # # create icinga host #
icingatools.create_master_host(app) if not app.config.get("ICINGA_API_URL"):
print("ICINGA_API_URL not defined. Not connecting Icinga", file=sys.stderr)
else:
icingatools.create_master_host(app)
if __name__ == "__main__": if __name__ == "__main__":