Last active
December 14, 2015 19:01
-
-
Save ajayhn/8389a1e9764c4998fd52 to your computer and use it in GitHub Desktop.
ip-dup-check-audit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import netaddr | |
import kazoo.client | |
from vnc_api import vnc_api | |
| |
lib = vnc_api.VncApi() | |
| |
zk = kazoo.client.KazooClient('localhost:2181') | |
zk.start() | |
| |
def get_zk_node_path(vn_obj, ip_addr): | |
for ipam_ref in vn_obj.get_network_ipam_refs() or []: | |
for ipam_subnet in ipam_ref['attr'].ipam_subnets: | |
pfx = ipam_subnet.subnet.ip_prefix | |
pfx_len = ipam_subnet.subnet.ip_prefix_len | |
ip_net = netaddr.IPNetwork('%s/%s' %(pfx, pfx_len)) | |
if netaddr.IPAddress(ip_addr) not in ip_net: | |
continue | |
| |
ip_str = "%(#)010d" % {'#': int(netaddr.IPAddress(ip_addr))} | |
return '/api-server/subnets/%s:%s/%s/%s' %( | |
vn_obj.get_fq_name_str(), pfx, pfx_len, ip_str) | |
| |
vn_list = lib.virtual_networks_list()['virtual-networks'] | |
for vn in vn_list: | |
vn_iip_info = {} | |
vn_fip_info = {} | |
vn_obj = lib.virtual_network_read(id=vn['uuid']) | |
#print 'Reading IP details for VN %s' %(vn_obj.fq_name) | |
for iip in vn_obj.get_instance_ip_back_refs() or []: | |
iip_obj = lib.instance_ip_read(id=iip['uuid']) | |
if iip_obj.instance_ip_address in vn_iip_info: | |
print "Dup iip uuid %s for iip uuid %s for address %s" %( | |
vn_iip_info[iip_obj.instance_ip_address], iip_obj.uuid, | |
iip_obj.instance_ip_address) | |
else: | |
vn_iip_info[iip_obj.instance_ip_address] = iip_obj.uuid | |
node_path = get_zk_node_path(vn_obj, iip_obj.instance_ip_address) | |
try: | |
zk.get(node_path) | |
except Exception as e: | |
print '*** ZK reservation check exc: %s node_path: %s' %(e, node_path) | |
| |
for fip_pool in vn_obj.get_floating_ip_pools() or []: | |
for fip in lib.floating_ip_pool_read(id=fip_pool['uuid']).get_floating_ips() or []: | |
fip_obj = lib.floating_ip_read(id=fip['uuid']) | |
if fip_obj.floating_ip_address in vn_iip_info: | |
print "Dup iip uuid %s for fip uuid %s for address %s" %( | |
vn_iip_info[fip_obj.floating_ip_address], fip_obj.uuid, | |
fip_obj.floating_ip_address) | |
elif fip_obj.floating_ip_address in vn_fip_info: | |
print "Dup fip uuid %s for fip uuid %s for address %s" %( | |
vn_fip_info[fip_obj.floating_ip_address], fip_obj.uuid, | |
fip_obj.floating_ip_address) | |
else: | |
vn_fip_info[fip_obj.floating_ip_address] = fip_obj.uuid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment