Learn How We Can Help You Keep Teleworkers Protected During the COVID-19 Crisis

DNS DHCP IPAM

Reply
Highlighted

Export Infoblox Subnet utilization

Techie
Posts: 1
2769     0

We won't have the reporting software until 3rd quarter next year.  Is there any way that we can export Subnet/subnet name/subnet utilization from the Infoblox appliance for all subnets?

 

Thanks in advance for the help!

 

Regards,

Jason

Highlighted

Re: Export Infoblox Subnet utilization

Adviser
Posts: 134
2770     0

I did a test program a while back that I think is relatively close to what you want to do. Here it is:

 

# Import the required Python modules.
import requests
import json
import csv
import getpass
import sys


# Set parameters to access the Infoblox API for your own grid master.
url = 'https://gm.example.com/wapi/v1.2/'
id = 'api'  # Userid with WAPI access
valid_cert = False  # True if GM uses certificate from commercial CA

# Prompt for the API user password.
pw = getpass.getpass('Password for user ' + id + ': ')


# Retrieve all network objects (up to a max of 5000). Return the
# extensible attribute values (if any) in addition to the standard
# fields.
network_view = 'default'
max_results = -5000
req_params = {'network_view': network_view,
              '_max_results': str(max_results),
              '_return_fields+': 'extattrs'}

r = requests.get(url + 'network',
                 params=req_params,
                 auth=(id, pw),
                 verify=valid_cert)

# Verify that the request succeeded. If not print error info and exit.
if r.status_code != requests.codes.ok:
    print r.text
    exit_msg = 'Error {} finding networks: {}'
    sys.exit(exit_msg.format(r.status_code, r.reason))

# Record the results and save the authentication cookie for future use.
networks = r.json()
ibapauth_cookie = r.cookies['ibapauth']

# For each network count all used addresses (up to a max of 10000)
# and store utilization statistics along with other network info.
stats = []
for network in networks:
    # Retrieve used ipv4address objects for this network.
    # Use ibapauth cookie to authenticate instead of userid/password.
    req_params={'network': network['network'],
                'network_view': network['network_view'],
                'status': 'USED',
                '_return_fields': '',
                '_max_results': str(10000)}
    req_cookies = {'ibapauth': ibapauth_cookie}

    r = requests.get(url + 'ipv4address',
                     params=req_params,
                     cookies=req_cookies,
                     verify=valid_cert)

    if r.status_code != requests.codes.ok:
        print r.text
        exit_msg = 'Error {} finding ipv4address objects: {}'
        sys.exit(exit_msg.format(r.status_code, r.reason))

    # Count the number of ipv4address objects and save the count.
    ipv4addresses = r.json()
    used = len(ipv4addresses)

    # Convert the network CIDR into network address and mask.
    netaddr, prefix = network['network'].split('/')
    prefix = int(prefix)
    netmask = '.'.join([str((0xffffffff << (32 - prefix) >> i) & 0xff)
                        for i in [24, 16, 8, 0]])

    # Calculate the percentage utilization for the network, ignoring
    # the two non-assignable addresses (network and broadcast).
    size = 2**(32 - prefix) - 2
    pct_used = round((100. * used) / size, 1)

    # Get values of the desired extensible attributes, if present.
    country = state = ''
    if 'Country' in network['extattrs']:
        country = network['extattrs']['Country']['value']
    if 'State' in network['extattrs']:
        state = network['extattrs']['State']['value']


    # Append desired info as a tuple to stats list for easy output.
    stats.append((netaddr,
                  netmask,
                  size,
                  used,
                  pct_used,
                  country,
                  state))


# Export the results.
with open('ipam-stats.csv', 'wb') as out_file:
    out_csv = csv.writer(out_file,
                         delimiter=',',
                         quotechar='"',
                         quoting=csv.QUOTE_MINIMAL)

    # Header row with field names.
    out_csv.writerow(['Network',
                      'Netmask',
                      'Size',
                      'Used',
                      'Pct Used',
                      'Country',
                      'State'])

    # One data row per network.
    for item in stats:
        out_csv.writerow(list(item))

 

I I wrote and tested it on NIOS 6.8, but it should work on later versions. (Feel free to update the WAPI version number to match your version of NIOS.) You should be able to take the resulting CSV file and load it into Excel or other spreadsheet applications.

Showing results for 
Search instead for 
Do you mean 

Recommended for You