Verified Commit 93798b25 authored by Adrian Schollmeyer's avatar Adrian Schollmeyer
Browse files

generate-icinga-switch-host: Add script for switch object creation


Signed-off-by: Adrian Schollmeyer's avatarAdrian 'nex' Schollmeyer <adrian.schollmeyer@fem.tu-ilmenau.de>
parent f1db7d44
#!/usr/bin/env python3
import sys
import re
from argparse import ArgumentParser
from fem_netbox import cli_common
ACCESS_SWITCH_ROLE="access-switch"
CORE_SWITCH_ROLE="core-switch"
class Icinga2SwitchHost:
def __init__(self, name, switch_type, primary_ip, parent = "bigsister"):
self.name = name
self.switch_type = switch_type
self.primary_ip = primary_ip
self.parent = parent
self.monitored_interfaces = []
self.monitored_power_supplies = []
def add_monitored_interface(self, interface_number, description):
self.monitored_interfaces.append((interface_number, description))
def add_monitored_power_supply(self, power_supply_number):
self.monitored_power_supplies.append(power_supply_number)
def create_icinga2_config(self):
text = "object Host \"" + f"{self.name}" + "\" {\n"
text += "\tvars.switch = \"" + f"{self.switch_type}" + "\"\n\n"
text += "\timport \"switch\"\n\n"
text += "\taddress = \"" + f"{self.primary_ip}" + "\"\n"
text += "\tvars.parent = \"" + f"{self.parent}" + "\"\n"
text += self.create_monitored_interface_config()
text += self.create_monitored_power_supply_config()
text += "\n\timport \"netbox-generated-host-extras\"\n"
text += "}"
return text
def create_monitored_interface_config(self):
text = ""
for (interface_number, description) in self.monitored_interfaces:
if text == "":
text += "\n"
text += "\tvars.uplink[\"" \
+ f"{interface_number}" \
+ "\"] = {description = \"" \
+ f"{description}" \
+ "\"}\n"
return text
def create_monitored_power_supply_config(self):
text = ""
for ps_number in self.monitored_power_supplies:
if text == "":
text += "\n"
text += "\tvars.ps[\"" \
+ f"{ps_number}" \
+ "\"] = {}\n"
return text
def main():
argument_parser = create_argument_parser()
args = argument_parser.parse_args()
config = cli_common.get_config_from_args(args)
netbox = cli_common.create_netbox_instance_from_config(config)
switches = get_switches(netbox, args.switch_name)
for switch in switches:
print(create_icinga2_config_for_switch(netbox, switch))
print('')
return 0
def create_argument_parser():
parser = ArgumentParser(
description="Generate Icinga2 switch config from NetBox")
cli_common.add_parser_default_args(parser)
parser.add_argument("--switch-name", "-s", type=str,
help="Switch name to generate config for")
return parser
def get_switches(netbox, switch_name):
if switch_name:
return [ netbox.dcim.devices.get(name=switch_name) ]
else:
switch_set = netbox.dcim.devices.filter(role=ACCESS_SWITCH_ROLE)
switches = []
for switch in switch_set:
switches.append(switch)
return switches
def create_icinga2_config_for_switch(netbox, switch):
name = switch.name
primary_ip = get_primary_ip(switch)
model = get_model_slug(netbox, switch)
try:
parent = switch.config_context["monitoring"]["parent"]
except KeyError as e:
parent = "newdawn"
host = Icinga2SwitchHost(
name,
model,
make_ip_without_prefix_length(primary_ip),
parent
)
try:
monitoring_config = switch.config_context["monitoring"]
add_monitored_interfaces_from_config(monitoring_config, host)
add_monitored_power_supplies_from_config(monitoring_config, host)
except KeyError as e:
# Nothing to do, if there is now config
pass
#switch_interfaces = netbox.dcim.interfaces.filter(
# device_id = switch.id
# )
#for interface in switch_interfaces:
# if not interface.connected_endpoint:
# continue
# connected_device = interface.connected_endpoint.device
return host.create_icinga2_config()
def get_primary_ip(switch):
if switch.primary_ip4:
return switch.primary_ip4.address
elif switch.primary_ip6:
return switch.primary_ip6.address
else:
return ""
def get_model_slug(netbox, switch):
manufacturer_slug = switch.device_type.manufacturer.slug
model_slug = switch.device_type.slug
return manufacturer_slug + "-" + model_slug
def add_monitored_interfaces_from_config(monitoring_config, icinga2_host):
switch_uplinks = monitoring_config["switch_uplinks"]
for switch_uplink in switch_uplinks:
icinga2_host.add_monitored_interface(
switch_uplink["port"],
switch_uplink["description"]
)
def add_monitored_power_supplies_from_config(monitoring_config, icinga2_host):
switch_power_supplies = monitoring_config["switch_power_supplies"]
for power_supply in switch_power_supplies:
icinga2_host.add_monitored_power_supply(power_supply)
def make_ip_without_prefix_length(ip):
return re.sub(r"^(.*)/(.*)$", "\\1", ip)
if __name__ == "__main__":
sys.exit(main())
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