wip: api connection for icinga services

This commit is contained in:
2023-07-05 13:47:30 +02:00
parent 8eb4271c04
commit 0bc32a11fe
2 changed files with 84 additions and 0 deletions

74
icinga-api-interface.py Normal file
View File

@@ -0,0 +1,74 @@
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):
return "ais_{}".format(async_service_name)
def create_service(async_service_name):
client = _create_client()
name = _build_service_name(async_service_name)
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):

View File

@@ -311,8 +311,18 @@ if __name__ == "__main__":
parser.add_argument('--interface', default="localhost", help='Interface to run on') parser.add_argument('--interface', default="localhost", help='Interface to run on')
parser.add_argument('--port', default="5000", help='Port to run on') parser.add_argument('--port', default="5000", help='Port to run on')
parser.add_argument('--icinga-dummy-host', required=True)
parser.add_argument('--icinga-api-pass', required=True)
parser.add_argument('--icinga-api-user', required=True)
parser.add_argument('--icinga-api-url', required=True)
args = parser.parse_args() 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
with app.app_context(): with app.app_context():
create_app() create_app()