Reply
Highlighted
Accepted Solution

WAPI IPAM Statistics

EDavidovich
Techie
Posts: 3
9637     0

Hey all,

I'm trying to get the IPAM Utilization statistics through the WAPI and I can't really find the right object or reference..
Is it even possible?

I have around 10 subnets that are already 99% utilized and we would like to build a simple dashboard that shows you the utilization (along with much more network information)

Basically I'm looking to have the exact information I get from the IPAM page under "Data Management".

Thanks!

One way to get IPAM statistics using WAPI

Adviser
Posts: 132
9638     0

I'm not sure this is exactly what you want, but I did figure out one way to get some IPAM statistics. The basic approach is first to get a list of all networks (assuming you want statistics for all networks):

curl --tlsv1 --insecure --user 'admin:infoblox' --header 'Content-Type:application/json' \
     'https://gm.example.com/wapi/v1.0/network?_max_results=5000'

and then for each network look for ipv4address objects marked as being used:

curl --tlsv1 --insecure --user 'admin:infoblox' --header 'Content-Type:application/json' \
'https://gm.example.com/wapi/v1.0/ipv4address?network=192.168.201.0/24;network_view=default;status=US...

If you then count the number of returned ipv4address objects and divide by the network size this will give you a percent utilization statistic.

Here's sample Python code to do the above steps and write the results to a CSV file:

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

# Set parameters to access the Infoblox API for your own grid master.
url = 'https://gm.example.com/wapi/v1.0/'
id = 'admin'
pw = 'infoblox'
valid_cert = True  # False if self-signed certificate

# Retrieve all network objects (up to a max of 5000).
r = requests.get(url + 'network',
                 params={'_max_results': str(5000)},
                 auth=(id, pw),
                 verify=valid_cert)
if r.status_code != requests.codes.ok:
    print 'Request failed with error code ', r.status_code
    exit()
networks = r.json()

# For each network count all used addresses (up to a max of 5000).
stats = []
for network in networks:
    # Retrieve used ipv4address objects for this network.
    r = requests.get(url + 'ipv4address',
                     params={'network': network['network'],
                             'network_view': network['network_view'],
                             'status': 'USED',
                             '_max_results': str(5000)},
                     auth=(id, pw),
                     verify=valid_cert)
    if r.status_code != requests.codes.ok:
        print 'Request failed with error code ', r.status_code
        exit()
    ipv4addresses = r.json()

    # Count the number of ipv4address objects and save the count.
    used = len(ipv4addresses)
    prefix = int(network['network'].split('/')[1])
    size = 2**(32-prefix) - 2
    pct_used = round((100. * used) / size, 1)
    stats.append((network['network'],
                  network['network_view'],
                  size,
                  used,
                  pct_used))

# Export the results.
with open('ipam-stats.csv', 'wb') as out_file:
    out_csv = csv.writer(out_file,
                         delimiter=',',
                         quotechar='"',
                         quoting=csv.QUOTE_MINIMAL)
    out_csv.writerow(['network',
                      'network_view',
                      'size',
                      'used',
                      'pct_used'])
    for item in stats:
        out_csv.writerow(list(item))

(A note on the percentage calculation: NIOS will not return ipv4address objects for the first and last addresses on the network, so I used the network size - 2 when calculating the percentage in order to ensure that a fully used network would in fact show as 100% utilized.)

The resulting output file will look something like the following, which was taken from my lab network (note the one network in a different network view):

network,network_view,size,used,pct_used
151.207.0.0/16,default,65534,364,0.6
192.168.201.0/24,default,254,118,46.5
192.168.202.0/24,default,254,64,25.2
192.168.203.0/24,default,254,50,19.7
10.1.0.0/24,default,254,8,3.1
192.168.201.0/26,test,62,1,1.6
172.17.0.0/17,default,32766,0,0.0
172.17.128.0/17,default,32766,0,0.0

The "used" and "pct_used" figures may or may not be what you actually want, depending on your definition of "used". In particular, all addresses in a DHCP range will apparently have status=USED. If you want to get more granular information on lease states you can use the parameter status=USED together with one of lease_state=FREE, lease_state=ACTIVE, and lease_state=BACKUP to return ipv4address objects in those states. For example,

curl --tlsv1 --insecure --user 'admin:infoblox' --header 'Content-Type:application/json' \
'https://gm.example.com/wapi/v1.0/ipv4address?network=192.168.201.0/24;network_view=default;status=US...

would return all ipv4address objects representing active leases in 192.168.201.0/24 in the default network view.

One final note: Do NOT do queries for ipv4address objects without including a status=USED parameter or some other search criteria to narrow down the returned results; otherwise you will get one object returned for every valid address in the network. You probably don't want this, espcially on a /16 or higher :-)

Thanks a lot!

EDavidovich
Techie
Posts: 3
9638     0

Thanks a lot!

That looks awesome!
Just a note, I had to change the params to be: params={'_return_fields': 'network'},  - because for some reason I got duplicated subnets.

Since 4 of my subnets are /20 and they are 99% full, I get a huge list.. causing the script to run around 7 minutes.. 
That's ok I just run it every hour.

 

Thanks a lot for your GREAT help!

Eran

Glad the WAPI example was useful

Adviser
Posts: 132
9638     0

Eran,

I'm glad the example WAPI code I provided proved useful to you. I do have one follow-up question: You wrote:

I had to change the params to be: params={'_return_fields': 'network'},  - because for some reason I got duplicated subnets.

Could you clarify where you added to (or replaced?) the params to include {'_return_fields': 'network'}? Was this in the call to get the list of networks, or in the call to get the used addreses for a particular network? I'm curious about the duplicated subnets issue you saw and why that might have been happening.

Sleaking of _return_fields, your comment prompted me to realize that since the program is just counting the number of returned ipv4address objects, there's no point in retrieving the actual field values for the objects. I changed my example program to retrieve the ipv4address objects as follows, setting the _return_fields parameter to the empty string:

r = requests.get(url + 'ipv4address',
                 params={'network': network['network'],
                         'network_view': network['network_view'],
                         'status': 'USED',
                         '_return_fields': '',
                         '_max_results': str(10000)},
                 auth=(id, pw),
                 verify=valid_cert)

This accomplshes the same goal and reduces the amount of data returned by the WAPI calls by a factor of 5-6x. (All you get is the _ref value for each ipv4address object.)

Frank

 

Hey,

EDavidovich
Techie
Posts: 3
9638     0

Hey,

Sorry for the delay..
I changed the following

r = requests.get(url + 'network',
                 params={'_return_fields': 'network'}, 

For some reason, I had duplicate lines of networks when I didn't filter it to _return_fields: network..

Not sure if it's better though..

 

Re: WAPI IPAM Statistics

moandersson
Techie
Posts: 4
9638     0

 

Hi,

 

this is an old post and already solved but if anyone else is looking for a solution you could use ipam:statistics as well:

 

curl -X GET \
  'https://<infoblox-host>/wapi/v2.3.1/ipam:statistics?_return_fields=network%2Ccidr%2Cnetwork_view%2Cutilization'

Utilization is for some reason in int and not float, so 948 = 94,8%

 

Enjoy!

Showing results for 
Search instead for 
Do you mean 

Recommended for You