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()