Merge branch 'dev' of github.com:FAUSheppy/icinga-webhook-gateway into dev

This commit is contained in:
2023-07-07 09:08:03 +02:00
4 changed files with 92 additions and 78 deletions

View File

@@ -1,74 +0,0 @@
import flask
import icinga2api
import icinga2api.client
def _create_client():
api_user = app.config["ICINGA_API_USER"]
api_pass = app.config["ICINGA_API_PASS"]
api_url = app.config["ICINGA_API_URL"]
client = icinga2api.client.Client(api_url, api_user, api_pass)
return client
def create_master_host():
client = _create_client()
host_config = {
"templates": ["generic-host"],
"attrs": {
"display_name": "ASYNC_ICINGA_SINK"
"address": "localhost",
"check_command": "ping",
}
}
# Create the host
response = client.objects.create("Host",
app.config["ASYNC_ICINGA_DUMMY_HOST"], **host_config)
# Check the response
if not response.success:
raise RuntimeError("Failed to create Icinga Dummy-Host: '{}'".format(response.response)
def _build_service_name(async_service_name, user):
return "{}_async_{}".format(user, async_service_name)
def create_service(async_service_name, user):
client = _create_client()
name = _build_service_name(async_service_name, user)
response = client.objects.get("Host", filters={'name': host_name})
if not response.success
raise RuntimeError("Failed to query Icinga Server to check for dummy host")
if len(response.response) > 0:
print("Dummy host already exists")
return
service_config = {
"templates": ["generic-service"],
"attrs": {
"display_name": name,
"check_command": "gateway"
}
"vars" : {
"host" : flask.app.config["ASYNC_ICINGA_DUMMY_HOST"],
"service_name" : async_service_name,
"protocol" : "https"
}
}
# Create the service
response = client.objects.create("Service", name,
flask.app.config["ASYNC_ICINGA_DUMMY_HOST"], **service_config)
# Check the response
if not response.success:
raise RuntimeError("Failed to create Icinga service: '{}'".format(response.response)
def delete_service(async_service_name):

74
icingatools.py Normal file
View File

@@ -0,0 +1,74 @@
import icinga2api
import icinga2api.client
def _create_client(app):
api_user = app.config["ICINGA_API_USER"]
api_pass = app.config["ICINGA_API_PASS"]
api_url = app.config["ICINGA_API_URL"]
client = icinga2api.client.Client(api_url, api_user, api_pass)
return client
def create_master_host(app):
client = _create_client(app)
host_name = app.config["ASYNC_ICINGA_DUMMY_HOST"]
try:
response = client.objects.get("Host", host_name)
return
except icinga2api.exceptions.Icinga2ApiException as e:
ICINGA_NO_OBJECTS_RESPONSE = "No objects found"
if not len(e.args) >= 1 or ICINGA_NO_OBJECTS_RESPONSE not in e.args[0]:
raise RuntimeError("Failed to query Icinga Server to check for dummy host: {}".format(e))
host_config = {
"templates": ["generic-host"],
"attrs": {
"display_name": "ASYNC_ICINGA_SINK",
"address": "localhost",
"check_command": "ping",
}
}
# Create the host
response = client.objects.create("Host", host_name, **host_config)
def _build_service_name(async_service_name):
return "ais_{}".format(async_service_name)
def create_service(async_service_name, app):
client = _create_client(app)
name = _build_service_name(async_service_name)
host_name = app.config["ASYNC_ICINGA_DUMMY_HOST"]
service_config = {
"templates": ["generic-service"],
"attrs": {
"display_name": name,
"check_command": "gateway",
"host_name" : host_name,
"vars" : {
"host" : "async-icinga.atlantishq.de",
"service_name" : async_service_name,
"protocol" : "https"
}
}
}
# Create the service (name is required in this format)
service_api_helper_name = "{}!{}".format(host_name, name)
print(service_api_helper_name)
response = client.objects.create("Service", service_api_helper_name, **service_config)
def delete_service(async_service_name, app):
client = _create_client(app)
name = _build_service_name(async_service_name)
host_name = app.config["ASYNC_ICINGA_DUMMY_HOST"]
service_api_helper_name = "{}!{}".format(host_name, name)
client.objects.delete("Service", service_api_helper_name)

View File

@@ -4,3 +4,4 @@ flask-sqlalchemy
flasl-wtf
waitress
requests
icinga2api

View File

@@ -23,6 +23,8 @@ import sqlalchemy
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql.expression import func
import icingatools
app = flask.Flask("Icinga Report In Gateway")
@@ -117,6 +119,9 @@ def create_entry(form, user):
service = Service(service=service_name, timeout=int(form.timeout.data),
owner=user, token=token)
icingatools.create_service(service_name, app)
db.session.merge(service)
db.session.commit()
@@ -153,9 +158,11 @@ def create_interface():
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)
icingatools.delete_service(service_delete_name, app)
db.session.delete(service_del_object)
db.session.commit()
@@ -298,8 +305,12 @@ def create_app():
with open(fullpath) as f:
config |= json.load(f)
# create dummy host #
icingatools.create_master_host(app)
if not config:
raise ValueError("No valid configuration found - need at least one service")
print("No valid configuration found - need at least one service")
return
for key in config:
timeout = timeparse.timeparse(config[key]["timeout"])
@@ -324,9 +335,11 @@ if __name__ == "__main__":
args = parser.parse_args()
app.config["ASYNC_ICINGA_DUMMY_HOST"] = args.icinga_dummy_host
app.config["ICINGA_API_USER"] = args.icinga_dummy_host
app.config["ICINGA_API_PASS"] = args.icinga_dummy_host
app.config["ICINGA_API_URL"] = args.icinga_dummy_host
#app.config["ASYNC_ICINGA_QUERY_URL"] = args.icinga_api_url
app.config["ICINGA_API_USER"] = args.icinga_api_user
app.config["ICINGA_API_PASS"] = args.icinga_api_pass
app.config["ICINGA_API_URL"] = args.icinga_api_url
with app.app_context():
create_app()