diff --git a/icinga-api-interface.py b/icinga-api-interface.py deleted file mode 100644 index bad2127..0000000 --- a/icinga-api-interface.py +++ /dev/null @@ -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): diff --git a/icingatools.py b/icingatools.py new file mode 100644 index 0000000..0d6c206 --- /dev/null +++ b/icingatools.py @@ -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) diff --git a/req.txt b/req.txt index 8c53034..a96d500 100644 --- a/req.txt +++ b/req.txt @@ -4,3 +4,4 @@ flask-sqlalchemy flasl-wtf waitress requests +icinga2api diff --git a/server.py b/server.py index c5769b6..b156bd6 100755 --- a/server.py +++ b/server.py @@ -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()