Report on missing primary addresses #680

Closed
opened 2025-12-29 16:24:42 +01:00 by adam · 1 comment
Owner

Originally created by @candlerb on GitHub (Jan 31, 2017).

It would be helpful to identify:

  • Devices which have at least one IPv4 address, but no Primary IPv4 address has been chosen
  • Devices which have at least one IPv6 address, but no Primary IPv6 address has been chosen

These can be identified using an ad-hoc query:

select dev.name,addr.family,addr.address
from dcim_device dev
join dcim_interface intf on dev.id = intf.device_id
join ipam_ipaddress addr on intf.id = addr.interface_id
where (dev.primary_ip4_id is null and addr.family = 4)
   or (dev.primary_ip6_id is null and addr.family = 6)
order by dev.name;

Ideally if there is only one choice, I'd like to be able to connect the primary address automatically. These candidates can be identified:

select dev.name,addr.family,min(addr.address),count(*)
from dcim_device dev
join dcim_interface intf on dev.id = intf.device_id
join ipam_ipaddress addr on intf.id = addr.interface_id
where (dev.primary_ip4_id is null and addr.family = 4)
   or (dev.primary_ip6_id is null and addr.family = 6)
group by dev.id,addr.family
having count(*) = 1;

However my SQL knowledge doesn't extend to nesting this with an UPDATE :-)

Originally created by @candlerb on GitHub (Jan 31, 2017). It would be helpful to identify: * Devices which have at least one IPv4 address, but no Primary IPv4 address has been chosen * Devices which have at least one IPv6 address, but no Primary IPv6 address has been chosen These can be identified using an ad-hoc query: ~~~ select dev.name,addr.family,addr.address from dcim_device dev join dcim_interface intf on dev.id = intf.device_id join ipam_ipaddress addr on intf.id = addr.interface_id where (dev.primary_ip4_id is null and addr.family = 4) or (dev.primary_ip6_id is null and addr.family = 6) order by dev.name; ~~~ Ideally if there is only one choice, I'd like to be able to connect the primary address automatically. These candidates can be identified: ~~~ select dev.name,addr.family,min(addr.address),count(*) from dcim_device dev join dcim_interface intf on dev.id = intf.device_id join ipam_ipaddress addr on intf.id = addr.interface_id where (dev.primary_ip4_id is null and addr.family = 4) or (dev.primary_ip6_id is null and addr.family = 6) group by dev.id,addr.family having count(*) = 1; ~~~ However my SQL knowledge doesn't extend to nesting this with an UPDATE :-)
adam closed this issue 2025-12-29 16:24:42 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 31, 2017):

There's a lot of stuff we could do reporting on, and I've purposely avoided opening that can of worms for now. However, if you're reasonably versed in Python and interested in learning Django's ORM, it's pretty easy to generate whatever form of report you'd like. Here's an example.

Devices which have at least one IPv4 address, but no Primary IPv4 address has been chosen

./manage.py shell
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from dcim.models import Device
>>> Device.objects.filter(primary_ip4=None).exclude(interfaces__ip_addresses__isnull=True)
@jeremystretch commented on GitHub (Jan 31, 2017): There's a _lot_ of stuff we could do reporting on, and I've purposely avoided opening that can of worms for now. However, if you're reasonably versed in Python and interested in learning [Django's ORM](https://docs.djangoproject.com/en/1.10/intro/overview/), it's pretty easy to generate whatever form of report you'd like. Here's an example. > Devices which have at least one IPv4 address, but no Primary IPv4 address has been chosen ``` ./manage.py shell Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from dcim.models import Device >>> Device.objects.filter(primary_ip4=None).exclude(interfaces__ip_addresses__isnull=True) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#680