From 375d41e9436abfce4b43f340087b564d992ac487 Mon Sep 17 00:00:00 2001 From: Carsten Aulbert Date: Mon, 24 Jul 2023 06:41:14 +0000 Subject: [PATCH 1/5] Fix first lines to enable direct call after chmod --- netbox_dnsmasq.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) mode change 100644 => 100755 netbox_dnsmasq.py diff --git a/netbox_dnsmasq.py b/netbox_dnsmasq.py old mode 100644 new mode 100755 index 7d24648..b0d1445 --- a/netbox_dnsmasq.py +++ b/netbox_dnsmasq.py @@ -1,5 +1,8 @@ +#!/usr/bin/env python3 +# noqa: E265 + """Main script for netbox_dnsmasq.""" -# !/usr/bin/env python3 # noqa: E265 + from __future__ import annotations import argparse From 704fcd49a31f323e5998cc434856c0d674b90248 Mon Sep 17 00:00:00 2001 From: Carsten Aulbert Date: Tue, 25 Jul 2023 08:40:39 +0000 Subject: [PATCH 2/5] Use look up table cache to prohibit frequent API calls --- netbox_dnsmasq.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/netbox_dnsmasq.py b/netbox_dnsmasq.py index b0d1445..07fe99b 100755 --- a/netbox_dnsmasq.py +++ b/netbox_dnsmasq.py @@ -147,6 +147,12 @@ class DnsType(TypedDict): nb_dns = {} dhcp_prefixes = nb.ipam.prefixes.filter(tag=[dhcp_tag]) ip_addresses = nb.ipam.ip_addresses.filter(tag=[dhcp_ignore_tag]) + + # create cache for dcim.interfaces as looking up mac addresses within loop is quite slow + interface_cache = {} + for iface in nb.dcim.interfaces.all(): + interface_cache[iface.id] = iface + for ip in ip_addresses: ip_address = re.sub("/.*", "", ip.address) if len(ip.dns_name) > 0: @@ -170,11 +176,13 @@ class DnsType(TypedDict): for ip in prefix_ips: ip_address = re.sub("/.*", "", ip.address) if ip.assigned_object_id: + mac_address = interface_cache[ip.assigned_object_id].mac_address \ + if ip.assigned_object_id in interface_cache else ip.assigned_object.mac_address if ( - ip.assigned_object.mac_address is not None + mac_address is not None and dhcp_ignore_tag not in list(t.name for t in ip.tags) ): - mac = ip.assigned_object.mac_address.lower() + mac = mac_address.lower() if ip_address in nb_dhcp: logger.warning( f"Duplicate IP address {ip_address}" From 518aa77b44b2abafe61d9843ff74de2d7aba9bed Mon Sep 17 00:00:00 2001 From: Carsten Aulbert Date: Tue, 25 Jul 2023 08:41:40 +0000 Subject: [PATCH 3/5] Use threading for further speed up (with MAX_PAGE_SIZE=1000) --- netbox_dnsmasq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox_dnsmasq.py b/netbox_dnsmasq.py index 07fe99b..a9049e9 100755 --- a/netbox_dnsmasq.py +++ b/netbox_dnsmasq.py @@ -351,7 +351,7 @@ def main() -> None: DNS_hosts_location, ) = import_config(args.dev) - api = pynetbox.api(url=NETBOX_ENDPOINT, token=NETBOX_TOKEN) + api = pynetbox.api(url=NETBOX_ENDPOINT, token=NETBOX_TOKEN, threading=True) dhcp, dns = get_ip_data(args.dns_tag, args.tag, nb=api) check_duplicates( From c674b28679a19dd57abdb020a8af77669b2f314f Mon Sep 17 00:00:00 2001 From: Carsten Aulbert Date: Tue, 25 Jul 2023 12:58:09 +0200 Subject: [PATCH 4/5] Address 'black' linter --- netbox_dnsmasq.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/netbox_dnsmasq.py b/netbox_dnsmasq.py index a9049e9..0e740c2 100755 --- a/netbox_dnsmasq.py +++ b/netbox_dnsmasq.py @@ -176,11 +176,13 @@ class DnsType(TypedDict): for ip in prefix_ips: ip_address = re.sub("/.*", "", ip.address) if ip.assigned_object_id: - mac_address = interface_cache[ip.assigned_object_id].mac_address \ - if ip.assigned_object_id in interface_cache else ip.assigned_object.mac_address - if ( - mac_address is not None - and dhcp_ignore_tag not in list(t.name for t in ip.tags) + mac_address = ( + interface_cache[ip.assigned_object_id].mac_address + if ip.assigned_object_id in interface_cache + else ip.assigned_object.mac_address + ) + if mac_address is not None and dhcp_ignore_tag not in list( + t.name for t in ip.tags ): mac = mac_address.lower() if ip_address in nb_dhcp: From d68766691b73bf53c9533f1796b4d3687648014e Mon Sep 17 00:00:00 2001 From: Carsten Aulbert Date: Tue, 25 Jul 2023 13:10:42 +0200 Subject: [PATCH 5/5] Split comment to trigger workflow --- netbox_dnsmasq.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netbox_dnsmasq.py b/netbox_dnsmasq.py index 0e740c2..c8405e4 100755 --- a/netbox_dnsmasq.py +++ b/netbox_dnsmasq.py @@ -148,7 +148,8 @@ class DnsType(TypedDict): dhcp_prefixes = nb.ipam.prefixes.filter(tag=[dhcp_tag]) ip_addresses = nb.ipam.ip_addresses.filter(tag=[dhcp_ignore_tag]) - # create cache for dcim.interfaces as looking up mac addresses within loop is quite slow + # create cache for dcim.interfaces as looking up mac addresses within + # loop is quite slow interface_cache = {} for iface in nb.dcim.interfaces.all(): interface_cache[iface.id] = iface