Commit c7ea7c54 authored by Adrian Schollmeyer's avatar Adrian Schollmeyer
Browse files

Add error handling and SSH config support



Signed-off-by: Adrian Schollmeyer's avatarAdrian 'nex' Schollmeyer <adrian.schollmeyer@fem.tu-ilmenau.de>
parent b47214fd
......@@ -6,6 +6,12 @@ import json
from argparse import ArgumentParser
from fem_netbox import cli_common
LINUX_PLATFORMS=[
"linux",
"debian",
"gentoo"
]
class Icinga2Host:
def __init__(
self,
......@@ -152,8 +158,16 @@ class Icinga2Host:
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"{ssh_port}" + "\"] = {}\n"
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
......@@ -246,8 +260,14 @@ def create_argument_parser():
def create_icinga2_host_object_from_netbox(netbox, device_name):
device = netbox.dcim.devices.get(name=device_name)
primary_ip4 = device.primary_ip4.address
primary_ip6 = device.primary_ip6.address
if device.primary_ip4:
primary_ip4 = device.primary_ip4.address
else:
primary_ip4 = None
if device.primary_ip6:
primary_ip6 = device.primary_ip6.address
else:
primary_ip6 = None
device_interfaces = netbox.dcim.interfaces.filter(
device_id = device.id
......@@ -280,30 +300,71 @@ def create_icinga2_host_object_from_netbox(netbox, device_name):
if uplink_interface.connected_endpoint:
parent = uplink_interface.connected_endpoint.device.name
primary_ip4 = make_ip_without_prefix_length(primary_ip4)
primary_ip6 = make_ip_without_prefix_length(primary_ip6)
if primary_ip4:
primary_ip4 = make_ip_without_prefix_length(primary_ip4)
if primary_ip6:
primary_ip6 = make_ip_without_prefix_length(primary_ip6)
host = Icinga2Host(
device_name,
"server",
parent,
primary_ip=primary_ip4,
primary_ip6=primary_ip6
ssh_services = netbox.ipam.services.filter(
device_id = device.id,
name = "SSH"
)
ssh_port = None
ssh_ports = []
for ssh_service in ssh_services:
for p in ssh_service.ports:
if ssh_port is None:
ssh_port = p
if ssh_port == p:
continue
ssh_ports.append(p)
device_platform = device.platform.slug
device_is_linux = False
for linux_platform in LINUX_PLATFORMS:
if device_platform == linux_platform:
device_is_linux = True
break
if device_is_linux:
host = Icinga2Host(
device_name,
"server",
parent,
primary_ip4,
primary_ip6,
ssh_port,
"Linux"
)
else:
host = Icinga2Host(
device_name,
"server",
parent,
primary_ip4,
primary_ip6,
ssh_port
)
for ip4 in non_uplink_ip4s:
host.add_ip(ip4)
for ip6 in non_uplink_ip6s:
host.add_ip6(ip6)
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)
for ssh_port in ssh_ports:
host.add_ssh(ssh_port)
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)
except KeyError as e:
pass # So there is no extra monitoring config. Who cares anyway?
return host
......
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