Forum Discussion

allenderj's avatar
allenderj
Icon for Nimbostratus rankNimbostratus
Sep 05, 2017

Extract info via Rest from BigIQ.

Hi all,

 

We are currently in the middle of a migration from VCMPs to VE's and from EM to Big-IQ.

 

Currently we do billing by of connections and we track this in the description field with other relevant info delimited by spaces.

 

We extract this information via a shell script that queries the EM database and then parses the output into a CSV file that is then periodically imported into our billing system. This script was set up before my time and do not have the time or the will to try and modify it to work with Big-IQ.

 

When we move to Big-IQ we will not be able to do this.

 

What I do have the will to do is to attempt to implement what I think would be a more "future proof" way extracting this information. So I was thinking about the RESTAPI.

 

I am a complete beginner when it comes to this sort of thing and was wondering if anyone could point me in the right direction.

 

I was looking at the python SDK and was wondering if this was a good place to start?

 

Thanks, Jeramy

 

4 Replies

  • Madhu_Rajagopa1's avatar
    Madhu_Rajagopa1
    Historic F5 Account

    Can you give specifics of what info you are trying to retrieve over the REST API?

     

  • You should definitely be considering using REST for this - as you say the SDK is easy to use, or I have a simpler Python module called iCR that you can use. Use a simple database table to store the device details and periodically retrieve the number of connections per VS then output it into a CSV. Simples!

     

  • I actually worked this out with the SDK. Ill post my code below for others to get ideas from, I am not a programmer by trade but I can make things work if I need too so the code is not the most elegant. I am sure it could be improved but it is working for my needs. It runs on a cron job and then another cron job takes the output and imports it into a database that our billing software uses.

     

    Thanks, -J

     

    !/usr/bin/python
    
    Import Regex module
    import re
     Import Datetime and parser for date normalization
    import datetime
    from dateutil import parser
    Import F5 SDK
    from f5.bigip import ManagementRoot
     This gets rid of the warning message about SSL certificate issues.
    import requests.packages.urllib3
    requests.packages.urllib3.disable_warnings()
    Read from the list Big IPs to query load it into the bigip list.
    with open("/home/f5_billing/load_balancing/bigip")as f:
        bigip = f.read().splitlines()
    Open csv file for modification
    file = open('/home/f5_billing/load_balancing/load_balancing.csv', 'w+')
    Write the CSV headers to the load_balancing.csv file Billing Code, Start Date , Notes and Quantity are all part of the description field
    file.write("instance_id,agency,region,name,ip_address,port,account_number,quantity,date_start,notes\n")
    Declare global variable instane_id to increment through for the database primary key
    instance_id = 1
    loop to query each server in the list
    for server in bigip:
        mgmt = ManagementRoot( server, "username", 'password')
        vservers = mgmt.tm.ltm.virtuals.get_collection()
        Loop through the contents of the current servers API output
        for vs in vservers:
            Parse through the description of the VIP to extract billing code, Billable Units, date installed and the notes.
            d = vs.description
            desc = []
            desc = d.split(" ", 3)
            desc.append ("")
            billing_code = desc[0]
            billable_units = desc[1]
            install_date = desc[2]
            Parse through the date to normalize it
            install_date = parser.parse(install_date)
            install_date = install_date.strftime("%Y-%m-%d")
            notes = desc[3]
            Check to see if there is a notes entry
            if len(desc[3]) > 0 :
                notes = desc[3]
            else:
                notes = "No Notes!"
            Define the s list, this will be what is printed to the CSV file
            s = ''
            appende the instance id and then increment it by 1
            instance_id = str(instance_id)
            s += instance_id + ','
            instance_id = int(instance_id)
            instance_id = instance_id + 1
            create and append a blank value for agency
            agency = " "
            s += agency + ','
            append the partition
            s += vs.partition + ','
            Append the Virtual Server Name to the s string
            s += vs.name + ','
            Split the destination and append the VIP address and VIP port to the s string
            The split is nessecary bacause there are seperate fields for ip address and port
            de = vs.destination
            dest = de.split(":", 1)
            vip_address = dest[0]
            vip_port = dest[1]
            Extract IP address from the vip_address so that the route domain or partition is not mentioned (cosmetic)
            vip_address = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', vip_address).group()
            s += vip_address + ','
            s += vip_port + ','
            s += billing_code + ','
            Append the billiable units to the s string
            s += billable_units + ','
            Append the start date to the s string
            s += install_date + ','
            Append the notes to the s string and create a new line
            s += notes + '\n'
            Write the s string to the load_balancing.csv file at end of loop interation.
            file.write(s)
        Close the file after the loop has exited
    file.close()
    • PeteWhite's avatar
      PeteWhite
      Icon for Employee rankEmployee

      Brilliant! Good job in posting, i'm sure it'll help others