Forum Discussion
kwondra34_26054
Nimbostratus
Jun 23, 2016F5 iControl REST - API calls required to get Load Balancer, Virtual Server URL, IP, Port, Pool Name/Status/Members
Hi everyone,
I am attempting to use the F5 REST API to create a csv containing the following fields (potentally others if they are available, but these will suffice):
- Virtual Server (URL/Name)...
JG
Cumulonimbus
Aug 05, 2016I have put together the following Ruby script for this purpose. There is certainly room for improvement.
!/usr/local/bin/ruby -w
require 'rubygems'
require 'rest-client'
require 'json'
define program-wide variables
BIGIP_ADDRESS = 'mgmt_IP_addr'
BIGIP_USER = 'admin'
BIGIP_PASS = 'admin'
SLEEP_TIME = 20
bigip = RestClient::Resource.new(
"https://{BIGIP_ADDRESS}/mgmt/tm/",
:user => BIGIP_USER,
:password => BIGIP_PASS,
:headers => { :content_type => 'application/json' },
:verify_ssl => false
)
Get virtual servers:
vservers = bigip['ltm/virtual'].get
vservers_obj = JSON.parse(vservers)
Get pools:
pools = bigip['ltm/pool?expandSubcollections=true'].get
pools_obj = JSON.parse(pools)
Output header:
puts "Virtual Server Name;Virtual Server Destination;Virtual Server Partition;Pool Name;Pool LB Mode;Pool Member Name (address)(state)"
$output = ''
Process data:
vservers_obj.each do |vserver_obj|
vserver_obj.each do |vserver_obj_element|
if vserver_obj_element.is_a?(Array)
vserver_obj_element.each do |vserver_obj_element_property|
if vserver_obj_element_property.is_a?(Hash)
if vserver_obj_element_property.has_key?("name")
vs_name = vserver_obj_element_property.fetch("name")
$output = $output + vs_name + ';'
end
if vserver_obj_element_property.has_key?("destination")
vs_destination = vserver_obj_element_property.fetch("destination")
$output = $output + vs_destination + ';'
end
if vserver_obj_element_property.has_key?("partition")
vs_partition = vserver_obj_element_property.fetch("partition")
$output = $output + vs_partition + ';'
end
if vserver_obj_element_property.has_key?("pool")
pool_name_from_vs = vserver_obj_element_property.fetch("pool")
pools_obj.each_pair do |key, val|
next if key == "kind"
next if key == "selfLink"
for x in 0..(val.length-1)
if val[x]["fullPath"] == pool_name_from_vs
$output = $output + val[x]["name"] + ";" + val[x]["loadBalancingMode"]
if val[x].has_key?("membersReference")
val[x]["membersReference"].each_pair do |mrefkey,mrefval|
next if mrefkey == "link"
next if mrefkey == "isSubcollection"
for i in 0..(mrefval.length-1)
if i == 0
$output = $output + ";" + mrefval[i]["name"] + "(" + mrefval[i]["address"] + ")(" + mrefval[i]["state"] + "),"
else
$output = $output + mrefval[i]["name"] + "(" + mrefval[i]["address"] + ")(" + mrefval[i]["state"] + "),"
end
if i == mrefval.length-1
$output.chop!
$output += "\n"
end
end
end
else
$output = $output + ";;\n"
end
break
end
end
end
else
$output = $output + ";;\n"
end
end
end
end
end
end
puts $output
---END---
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects