Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use look-up-table and threading for major speed improvements #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions netbox_dnsmasq.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -144,6 +147,13 @@ 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:
Expand All @@ -167,11 +177,15 @@ class DnsType(TypedDict):
for ip in prefix_ips:
ip_address = re.sub("/.*", "", ip.address)
if ip.assigned_object_id:
if (
ip.assigned_object.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 = ip.assigned_object.mac_address.lower()
mac = mac_address.lower()
if ip_address in nb_dhcp:
logger.warning(
f"Duplicate IP address {ip_address}"
Expand Down Expand Up @@ -340,7 +354,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(
Expand Down