vADC Docs

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

by ricknelson on ‎03-28-2013 03:13 PM - edited on ‎07-30-2015 01:26 PM by Community Manager (1,209 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 Perl. This example builds on the previous listpools.pl 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.pl

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/perl
 
 
use REST::Client;
 
use MIME::Base64;
 
use JSON;
 
 
print "Pools:\n\n";
 
 
# Since Stingray is using a self-signed certificate we don't need to verify it
 
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
 
 
my $url = "/api/tm/1.0/config/active/pools";
 
# Set up the connection
 
my $client = REST::Client->new();
 
 
$client->addHeader("Authorization", "Basic " . encode_base64("admin:admin"));
 
 
# Request a list of pools
 
$client->GET($url);
 
 
# Decode the json response
 
my $response = decode_json($client->responseContent());
 
if ($client->responseCode() == 200) {
 
    # Obtain a reference to the children array
 
    my $poolArrayRef = $response->{children};
 
    foreach my $pool (@$poolArrayRef) {
 
        my $poolName = $pool->{name};
 
        $client->GET("$url/$poolName");
 
        my $poolConfig = decode_json $client->responseContent();
 
        if ($client->responseCode() == 200) {
 
            my $nodes = $poolConfig->{properties}->{basic}->{nodes};
 
            my $draining = $poolConfig->{properties}->{basic}->{draining};
 
            my $disabled = $poolConfig->{properties}->{basic}->{disabled};
 
            print "Pool: $poolName\n";
 
            print "    Nodes: ";
 
            foreach my $node (@$nodes) {
 
                print "$node ";
 
            }
 
            print "\n";
 
            if (scalar(@$draining) gt 0) {
 
                print "    Draining Nodes: ";
 
                foreach my $node (@$draining) {
 
                    print "$node ";
 
                }
 
                print "\n";
 
            }
 
            if (scalar(@$disabled) gt 0) {
 
                print "    Diabled Nodes: ";
 
                foreach my $node (@$disabled) {
 
                    print "$node ";
 
                }
 
                print "\n";
 
            }
 
            print "\n";
 
        } else {
 
            print "Error getting pool config: status=" . $client->responseCode() . " Id=" . $poolConfig->{error_id} . ": " . $poolConfig->{error_text} . "\n"
 
        }
 
    }
 
} else {
 
    print "Error getting list of pools: status=" . $client->responseCode() . " Id=" . $response->{error_id} . ": " . $response->{error_text} . "\n";
 
}

 

Running the example

 

This code was tested with Perl 5.14.2 and version 249 of the REST::Client module.

 

Run the Perl script as follows:

 

$ listpoolnodes.pl
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