From d14dbce550770525e0fa17e3577298741744c709 Mon Sep 17 00:00:00 2001 From: Sheppy Date: Wed, 28 Dec 2022 15:50:12 +0100 Subject: [PATCH] feat: icinga ansible dumper --- icinga.py | 18 ++++++++++++++++++ main.py | 4 ++++ templates/icinga_host.conf.j2 | 31 +++++++++++++++++++++++++++++++ vm.py | 25 +++++++++---------------- 4 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 icinga.py create mode 100644 templates/icinga_host.conf.j2 diff --git a/icinga.py b/icinga.py new file mode 100644 index 0000000..4f3e60a --- /dev/null +++ b/icinga.py @@ -0,0 +1,18 @@ +import jinja2 +environment = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="./templates")) + +def createMasterHostConfig(vmList): + template = environment.get_template("icinga_host.conf.j2") + with open("build/icinga_master_hosts.conf", "w") as f: + for vmo in vmList: + + if not vmo.check: + continue + + checkDomains = filter(lambda x: not x.get("nocheck"), vmo.subdomains) + websites = [ (s["name"], s.get("url")) for s in checkDomains] + f.write(template.render(hostname=vmo.hostname, address=vmo.ip, websites=websites)) + +def createMasterServiceConfig(vmList): + pass + diff --git a/main.py b/main.py index f80d401..0282547 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import json import vm import sys import jinja2 +import icinga ACME_CONTENT = ''' location /.well-known/acme-challenge/ { @@ -74,3 +75,6 @@ if __name__ == "__main__": content = template.render(nginxJson) f.write(content) + + # dump icinga master + icinga.createMasterHostConfig(vmList) diff --git a/templates/icinga_host.conf.j2 b/templates/icinga_host.conf.j2 new file mode 100644 index 0000000..76a8a1a --- /dev/null +++ b/templates/icinga_host.conf.j2 @@ -0,0 +1,31 @@ +object Host "{{ hostname }}" { + + import "generic-host" + + name = "{{ hostname }}" + address = "{{ address }}" + vars.remote = "true" + vars.linux = "true" + + max_check_attempts = 7 + retry_interval = 1m + + vars.notification["mail"] = { + groups = ["icingaadmins"] + } + + {% for website_name, url in websites %} + vars.http_vhosts["{{ website_name }}"] = { + {% if url %} + http_uri = "{{ url }}" + {% else %} + http_uri = "/" + {% endif %} + http_address = "https://{{ website_name }}" + http_ssl = true + http_onredirect = "follow" + } + {% endfor %} + +} + diff --git a/vm.py b/vm.py index 8d67c95..650110b 100644 --- a/vm.py +++ b/vm.py @@ -10,6 +10,7 @@ class VM: self.hostname = args.get("hostname") self.subdomains = args.get("subdomains") self.ports = args.get("ports") + self.check = not args.get("nocheck") self.terminateSSL = args.get("terminate-ssl") self.network = args.get("network") or "default" self.isExternal = args.get("external") @@ -95,25 +96,17 @@ class VM: # https components # components = [] template = self.environment.get_template("nginx_server_block.conf.j2") - targetport = 80 - if all([type(e) == dict for e in self.subdomains]): - for subdomain in self.subdomains: - compositeName = "-".join((self.hostname, subdomain["name"].replace(".","-"))) - targetport = subdomain["port"] - component = template.render(targetip=self.ip, targetport=targetport, - servernames=[subdomain["name"]], comment=compositeName, - proxy_pass_blob=self.proxy_pass_blob, acme=not self.noTerminateACME) - components.append(component) + for subdomain in self.subdomains: - elif any([type(e) == dict for e in self.subdomains]): - raise ValueError("Mixed subdomains not allowed - must be all complex or all simple") - else: - compositeName = "-".join((self.hostname, self.subdomains[0].replace(".","-"))) + if type(subdomain) != dict: + raise ValueError("Subdomain must be object containing 'name' ") + + compositeName = "-".join((self.hostname, subdomain["name"].replace(".","-"))) + targetport = subdomain.get("port") or 80 component = template.render(targetip=self.ip, targetport=targetport, - servernames=self.subdomains, comment=compositeName, - proxy_pass_blob=self.proxy_pass_blob, acme= not self.noTerminateACME) + servernames=[subdomain["name"]], comment=compositeName, + proxy_pass_blob=self.proxy_pass_blob, acme=not self.noTerminateACME) components.append(component) return components -