From 7c164d713294e8376411c0e5fe19dcb027861647 Mon Sep 17 00:00:00 2001 From: Daniel Vincze Date: Wed, 12 Jan 2022 17:22:50 +0200 Subject: [PATCH] Update oVirt VM IP address fetching script Added new method that fetches the IP addresses directly from the VM's reported devices, instead of getting the reported devices from the VM's NICs. --- ovirt/ovirt-get-vm-ips.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ovirt/ovirt-get-vm-ips.py b/ovirt/ovirt-get-vm-ips.py index 1d470eb..20e71eb 100644 --- a/ovirt/ovirt-get-vm-ips.py +++ b/ovirt/ovirt-get-vm-ips.py @@ -46,12 +46,15 @@ def get_vm_addresses(nics): addresses = [] for nic in nics: rps = nic.reported_devices + LOG.debug("RP objects: %s", rps) if rps: for rp in rps: ips = rp.ips + LOG.debug("NIC IP objects: %s", ips) if ips: for ip in ips: addr = ip.address + LOG.debug("NIC IP address: %s", addr) if addr: addresses.append(addr) return addresses @@ -77,6 +80,23 @@ def get_vm_main_address(nics): return ip_address +def get_ips_from_vm_rps(conn, vm): + addresses = [] + vm_reported_devices = conn.follow_link(vm.reported_devices) + LOG.debug("RP objects: %s", vm_reported_devices) + for rp in vm_reported_devices: + ips = rp.ips + LOG.debug("IP objects: %s", ips) + if ips: + for ip in ips: + address = ip.address + LOG.debug("IP address: %s", address) + if address: + addresses.append(address) + + return addresses + + def main(): setup_logging() parser = argparse.ArgumentParser(description="oVirt VM IP Address fetcher") @@ -110,6 +130,8 @@ def main(): vms = conn.system_service().vms_service() vm = get_vm(vms, vm_id) vm_nics = get_vm_nics(conn, vm) + + LOG.info("Checking VM NIC reported devices...") vm_primary_addr = get_vm_main_address(vm_nics) vm_addresses = get_vm_addresses(vm_nics) @@ -121,6 +143,10 @@ def main(): LOG.info("IP addresses for VM '%s': %s" % (vm_id, vm_addresses)) + LOG.info("Fetching IP addresses directly from VM Reported Devices...") + vm_addresses = get_ips_from_vm_rps(conn, vm) + LOG.info("IP addresses for VM '%s': %s", vm_id, vm_addresses) + if __name__ == "__main__": main()