vADC Docs

Tech Tip: Using the RESTful Control API with Ruby - listpoolnodes

by ricknelson on ‎03-26-2013 06:02 PM - edited on ‎07-14-2015 04:39 PM by PaulWallace (748 Views)

The following code uses Stingray's RESTful API to list all the pools defined for a cluster and for each pool it lists the nodes defined for that pool, including draining and disabled nodes. The code is written in Ruby. This example builds on the previous listpools.rb example.  This program does a GET request for the list of pool and then while looping through the list of pools, a GET is done for each pool to retrieve the configuration parameters for that pool.

 

listpoolnodes.rb

 

require 'rest_client'

require 'base64'

require 'json'


puts "Pools:\n\n"


url = 'https://stingray.example.com:9070/api/tm/1.0/config/active/pools'

auth = 'Basic ' + Base64.encode64('admin:admin')

begin

    # Do the HTTP GET to get the lists of pools

    response = RestClient.get(url, {:authorization => auth})

    data = JSON.parse(response.body)

    pools = data['children']

    pools.each do |pool|

        poolName = pool['name']

        begin

            # Do the HTTP GET to get the properties of a pool

            response = RestClient.get(url + '/' + URI.escape(poolName), {:authorization => auth})

            poolConfig = JSON.parse(response.body)

            # Since we are getting the properties for a pool we expect the first element to be 'properties'.

            # The value of the key 'properties' will be a hash containing property sections.  All the properties

            # that this program cares about are in the 'basic' section.  'nodes' is the array of all active or

            # draining nodes in this pool.  'draining' the array of all draining nodes in this pool.  'disabled'

            # is the array of all disabled nodes in this pool.

            nodes = poolConfig['properties']['basic']['nodes']

            draining = poolConfig['properties']['basic']['draining']

            disabled = poolConfig['properties']['basic']['disabled']

            puts "Pool: #{poolName}"

            print "   Node: "

            nodes.each do |node|

                print node + ' '

            end

            puts

            if draining.length > 0

                print "   Draining: "

                draining.each do |node|

                    print node + ' '

                end

                puts

            end

            if disabled.length > 0

                print "   Disabled: "

                disabled.each do |node|

                    print node + ' '

                end

                puts

            end

            puts

        rescue => e

            puts "Error getting pool data: URL=#{url + '/' + URI.escape(poolName)} Error: #{e.message}"

        end

    end

rescue => e

    puts "Error getting pool list: URL=#{url} Error: #{e.message}"

end

 

Running the example

 

This code was tested with Ruby 1.9.3 and version 1.6.7 of the rest-client module.

 

Run the Ruby script as follows:

 

$ listpoolnodes.rb

Pools:

 

Pool1

    Nodes:  192.168.1.100 192.168.1.101

    Draining:  192.168.1.101

    Disabled:  192.168.1.102

 

Pool2

    Nodes:  192.168.1.103 192.168.1.104

 

Read More

 

Contributors