Verified Commit 1ce8e2e1 authored by Adrian Schollmeyer's avatar Adrian Schollmeyer
Browse files

generate-icinga-generic-host: Use Jinja2 for config templating



Generating the config with a bunch of string generation is tedious and
not so well to read. Thus, using Jinja2 to create a template and then
just passing the variables is a sensible choice. This commit removes all
string-generating functions and replaces it with Jinja2 templating.
Signed-off-by: Adrian Schollmeyer's avatarAdrian 'nex' Schollmeyer <adrian.schollmeyer@fem.tu-ilmenau.de>
parent 79b171a8
......@@ -5,6 +5,7 @@ import re
import json
from argparse import ArgumentParser
from fem_netbox import cli_common
import jinja2
LINUX_PLATFORMS=[
"linux",
......@@ -12,6 +13,10 @@ LINUX_PLATFORMS=[
"gentoo"
]
j2_env = jinja2.Environment(
loader=jinja2.PackageLoader("netbox_icinga_generator", "templates")
)
class Icinga2Host:
def __init__(
self,
......@@ -104,115 +109,9 @@ class Icinga2Host:
def create_icinga2_config(self):
text = "object Host \"" + f"{self.name}" + "\" {\n"
text += "\timport \"netbox-generated-host\"\n\n"
text += self.create_icinga2_address_fields()
text += self.create_icinga2_attribute_fields()
text += self.create_icinga2_ping_fields()
text += self.create_icinga2_ssh_port_field()
text += self.create_icinga2_check_fields()
text += "\timport \"netbox-generated-host-extras\"\n"
text += "}"
return text
def create_icinga2_attribute_fields(self):
text = ""
if self.parent:
text += "\tvars.parent = \"" + f"{self.parent}" + "\"\n"
if self.host_class:
text += "\tvars.host_class = \"" + f"{self.host_class}" + "\"\n"
if self.os:
text += "\tvars.os = \"" + f"{self.os}" + "\"\n"
if not text == "":
text += "\n"
return text
def create_icinga2_address_fields(self):
text = ""
if self.address:
text += "\taddress = \"" + f"{self.address}" + "\"\n"
if self.address6:
text += "\taddress6 = \"" + f"{self.address6}" + "\"\n"
text += self.create_paragraph(text)
return text
def create_icinga2_ping_fields(self):
text = ""
for ip in self.ips:
text += "\tvars.ping[\"" + f"{ip}" + "\"] = {}\n"
for ip6 in self.ip6s:
text += "\tvars.ping6[\"" + f"{ip6}" + "\"] = {}\n"
text += self.create_paragraph(text)
return text
def create_icinga2_ssh_port_field(self):
text = ""
if self.ssh_port:
text += "\tvars.ssh_port = \"" + f"{self.ssh_port}" + "\"\n\n"
extra_ssh_port_num = 0
for ssh_port in self.additional_ssh_ports:
text += "\tvars.ssh[\"" + \
f"{extra_ssh_port_num}" + \
"\"] = {ssh_port = \"" + \
f"{ssh_port}" \
+"\"}\n"
extra_ssh_port_num += 1
if extra_ssh_port_num > 0:
text += self.create_paragraph(text)
return text
def create_icinga2_check_fields(self):
text = ""
if self.apt:
text = "\tvars.apt_" + f"{self.apt}" + " = {}\n\n"
procs_text = ""
for procs_check in self.procs:
text += "\tvars.procs[\"" + f"{procs_check.name}" + "\"] = {}\n"
text += procs_text + self.create_paragraph(procs_text)
disk_text = ""
for disk in self.smart:
disk_text += "\tvars.smart[\"" + f"{disk}" + "\"] = {}\n"
text += disk_text + self.create_paragraph(disk_text)
disk_text = ""
for disk in self.smart_sectors:
disk_text += "\tvars.smart_sectors[\"" + f"{disk}" + "\"] = {}\n"
text += disk_text + self.create_paragraph(disk_text)
nrpe_text = ""
if self.nrpe:
nrpe_text += "\tvars.nrpe = true\n"
for nrpe_check in self.nrpe_checks:
nrpe_text += \
"\tvars.nrpe_check[\"" + f"{nrpe_check}" + "\"] = {}\n"
nrpe_text += "\n"
for nrpe_disk in self.nrpe_disks:
nrpe_text += "\tvars.nrpe_disk[\"" + \
f"check_{nrpe_disk}_disk" + "\"] = {}\n"
nrpe_text += "\n"
text += nrpe_text
return text
def create_paragraph(self, text):
if text == "":
return ""
else:
return "\n"
global j2_env
template = j2_env.get_template("generic-host.j2")
return template.render(vars=self)
class Icinga2ProcsCheck:
......@@ -357,12 +256,46 @@ def create_icinga2_host_object_from_netbox(netbox, device_name):
try:
monitoring_config_context = device.config_context["monitoring"]
if monitoring_config_context["nrpe_enabled"]:
host.enable_nrpe()
for nrpe_check in monitoring_config_context["nrpe_checks"]:
host.add_nrpe_check(nrpe_check)
for nrpe_disk in monitoring_config_context["nrpe_disks"]:
host.add_nrpe_disk(nrpe_disk)
try:
apt = monitoring_config_context["apt"]
if apt == "active":
host.add_apt_active()
elif apt == "passive":
host.add_apt_passive()
else:
raise Exception(
f"Invalid apt configuration '{apt}'! "
"Must be either active or passive")
except KeyError:
pass
try:
for smart in monitoring_config_context["smart"]:
host.add_smart(smart)
except KeyError:
pass
try:
for smart_sector in monitoring_config_context["smart_sectors"]:
host.add_smart_sectors(smart_sector)
except KeyError:
pass
try:
if monitoring_config_context["nrpe_enabled"]:
host.enable_nrpe()
try:
for nrpe_check in monitoring_config_context["nrpe_checks"]:
host.add_nrpe_check(nrpe_check)
except KeyError:
pass
try:
for nrpe_disk in monitoring_config_context["nrpe_disks"]:
host.add_nrpe_disk(nrpe_disk)
except KeyError:
pass
except KeyError:
pass
except KeyError as e:
pass # So there is no extra monitoring config. Who cares anyway?
......
object Host "{{ vars.name }}" {
import "netbox-generated-host"
// === ADDRESSING ===
{% if vars.address is defined %}address = "{{ vars.address }}"{% endif %}
{% if vars.address6 is defined %}address6 = "{{ vars.address6 }}"{% endif %}
// === SYSTEM ATTRIBUTES ===
{%- if vars.parent is defined %}
vars.parent = "{{ vars.parent }}"{% endif %}
{%- if vars.host_class is defined %}
vars.host_class = "{{ vars.host_class }}"{% endif %}
{%- if vars.os is defined %}
vars.os = "{{ vars.os }}"{% endif %}
// === CONNECTIVITY CHECKS ===
{%- for ip in vars.ips %}
vars.ping["{{ ip }}"] = {}{% endfor %}
{%- for ip6 in vars.ip6s %}
vars.ping6["{{ ip6 }}"] = {}{% endfor %}
{% if vars.ssh_port %}vars.ssh_port = "{{ vars.ssh_port }}"{% endif %}
// === SYSTEM HEALTH CHECKS ===
{%- if vars.apt %}vars.apt_{{ vars.apt }} = {}{% endif %}
{% for check in vars.procs %}
vars.procs["{{ check }}"] = {}{% endfor %}
{% for disk in vars.smart %}
vars.smart["{{ disk }}"] = {}{% endfor %}
{%- for disk in vars.smart_sectors %}
vars.smart_sectors["{{ disk }}"] = {}{% endfor %}
{% if vars.nrpe is defined %}vars.nrpe = true{% endif %}
{% for check in vars.nrpe_checks %}
vars.nrpe_check["check_{{ check }}"] = {}{% endfor %}
{% for check in vars.nrpe_disks %}
vars.nrpe_disk["check_{{ check }}_disk"] = {}{% endfor %}
import "netbox-generated-host-extras"
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment