TMSH Scripting in v10.1

With the release of v10.1, tmsh has added a scripting language to accompany the shell introduced in v10.  To steal Yoda’s linguistic skills…Powerful, this will be.  So powerful in fact that we here at DevCentral have created a wiki namespace and a forum for tmsh related content and questions.

Last Spring when v10 was released, I wrote an article on how to use the new traffic management shell to configure GTM.  In this article, we’ll build the same configuration from a questionnaire generated by a tmsh script.  A couple caveats before getting started:

  • Error checking is lacking here.  The script is for demonstration purposes.  It would need serious work for use in a production environment.
  • There are plenty of features not highlighted or optional in the script.  Those that are used outside of pool and member ratios are hardcoded.

Getting Started

You can launch the script editor in tmsh by entering “edit cli script <scriptname>.tcl”.  Once in the editor, if it’s a new script, you’ll see a create script <scriptname.tcl { } wrapper with four procedures:

  • script::init – optional.  Can initialize global variables here.
  • script::run – required.  This is the main program loop.
  • script::help – optional.  Hitting the “?” key will provide context sensitive help.
  • script::tabc – optional. Hitting the tab key will provide context sensitive help.

Our script will create a procedure of our own and utilize the script::run procedure, so you can remove the others for this effort.

 

Working with User Input

When I first started this script effort, there was an awful lot of repetition with regards to stdout.  With that in mind, I created a proc to handle the user feedback:

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(204, 102, 51);">proc</span> getFeedback { question } {
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span>     <span style="color: rgb(204, 102, 51);">puts</span> -nonewline $question
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>     <span style="color: rgb(204, 102, 51);">flush</span> stdout
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>     <span style="color: rgb(204, 102, 51);">return</span> [gets stdin]
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span> }
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span> <span style="color: rgb(0, 0, 255);">script::run</span> {
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>     <span style="color: rgb(204, 102, 51);">set</span> dc_count [getFeedback <span style="color: rgb(0, 96, 128);">"How many datacenters? "</span>]
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span> }

Each time we need to get feedback, now instead of repeating the puts <string> and the stdout flush, we just set the variable to the returned data from the procedure.

GTM Configuration Tasks

To get to a resolving system, several tasks need to be accomplished:

  1. Create a datacenter
  2. Create a server, and if applicable, virtual servers.  Since the server is a BIG-IP, we’ll create a few virtual servers.
  3. Create a pool
  4. Create a wideIP
  5. Create a listener (already accomplished in our case)

In tmsh, the commands for these tasks are all under the gtm module, and we’ll need the syntax for the commands for our script.

Creating the Datacenters

This one’s really easy.  The only required information for the datacenter is the name.  In the tmsh shell, this would be create gtm datacenter <dc name>.  In script, we’ll use the tmsh::create command to achieve the same result.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 128, 0);"># Enable stateless so existing objects can be overwritten</span>
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> tmsh::stateless enabled
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span> <span style="color: rgb(0, 128, 0);">#Build Datacenters</span>
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span> <span style="color: rgb(204, 102, 51);">set</span> dc_count [getFeedback <span style="color: rgb(0, 96, 128);">"How many datacenters do you wish to create? "</span>]
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span> <span style="color: rgb(204, 102, 51);">for</span> {set x 0} {$x<$dc_count} {incr x} {
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>     <span style="color: rgb(204, 102, 51);">lappend</span> dc_names [getFeedback <span style="color: rgb(0, 96, 128);">"Datacenter [expr $x +1] name? "</span>]
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span> }
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span> <span style="color: rgb(0, 0, 255);">tmsh::create</span> /gtm datacenter $dc_names
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span> <span style="color: rgb(204, 102, 51);">puts</span> <span style="color: rgb(0, 96, 128);">"\nDatacenters created...\n\n"</span>

Here we grab the number of datacenters desired, loop through the count number and append the datacenter names to a variable we’ll use to create the datacenters with the tmsh::create command.  One other note, since I’m using this script for demonstration purposes, running through it repeatedly would error out because these objects already exist.  Rather than deleting them all after each iteration of the script, I’m using the tmsh::stateless command to overwrite any objects already in place.

Creating the Servers

This one is slightly more difficult as there are many different options that can be applied at the server level.  To keep this short, we’ll hard code the monitor and ignore the other options.  In the tmsh shell, we’d enter create gtm server <server name> addresses add { <ip> } monitor bigip datacenter <dc name> virtual-servers add { <virtual server ip:port> }.  In script, we’ll approach it this way:

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 128, 0);">#Build Servers</span>
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> <span style="color: rgb(204, 102, 51);">set</span> srv_count [getFeedback <span style="color: rgb(0, 96, 128);">"How many servers are you adding? "</span>]
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span> <span style="color: rgb(204, 102, 51);">for</span> {set x 0} {$x<$srv_count} {incr x} {
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>     <span style="color: rgb(204, 102, 51);">set</span> srv_name [getFeedback <span style="color: rgb(0, 96, 128);">"Server [expr $x +1] Name? "</span>]
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>     <span style="color: rgb(204, 102, 51);">set</span> srv_ip [getFeedback <span style="color: rgb(0, 96, 128);">"Server [expr $x +1] IP? "</span>]
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     <span style="color: rgb(204, 102, 51);">set</span> srv_dc_loc [getFeedback <span style="color: rgb(0, 96, 128);">"Datacenter server belongs to (tmsh::get_config /gtm datacenter)? "</span>]
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>     <span style="color: rgb(204, 102, 51);">set</span> v_count [getFeedback <span style="color: rgb(0, 96, 128);">"How mnay virtuals for $srv_name? "</span>]
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>     <span style="color: rgb(204, 102, 51);">for</span> {set y 0} {$y<$v_count} {incr y} {
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> vmembers($srv_name) [getFeedback <span style="color: rgb(0, 96, 128);">"Virtual Server [expr $y +1] IP:Port? "</span>]
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>     }
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>     <span style="color: rgb(0, 0, 255);">tmsh::create</span> /gtm server $srv_name addresses add \{ \
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>         $srv_ip \} monitor bigip datacenter $srv_dc_loc \
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span>         virtual-servers add \{ $vmembers($srv_name) \}
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span> }
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span> <span style="color: rgb(204, 102, 51);">puts</span> <span style="color: rgb(0, 96, 128);">"\nServers created...\n\n"</span>

This requires nested for loops, one to create the server, and the other to create the virtual servers within each server.

Creating the Pools

This code block is very similar to the previous one, with a couple exceptions.  Because the tmsh create pool command expects the ratio to be set (if being set) within the context of the { ip:port { ratio x } ip:port { ratio x} } etc, I needed to make sure that part of the string was accounted for when iterating through.  The foreach block takes each argument of the array and builds a single string so that the format is appropriate for the gtm pool command.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 128, 0);">#Build Pools</span>
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> <span style="color: rgb(204, 102, 51);">set</span> pl_count [getFeedback <span style="color: rgb(0, 96, 128);">"How many pools are you adding? "</span>]
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span> <span style="color: rgb(204, 102, 51);">for</span> {set x 0} {$x<$pl_count} {incr x} {
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>     <span style="color: rgb(204, 102, 51);">set</span> pl_name [getFeedback <span style="color: rgb(0, 96, 128);">"Pool [expr $x +1] name? "</span>]
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>     <span style="color: rgb(204, 102, 51);">set</span> pm_count [getFeedback <span style="color: rgb(0, 96, 128);">"How many pool members? "</span>]
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     <span style="color: rgb(204, 102, 51);">for</span> {set y 0} {$y<$pm_count} {incr y} {
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>         <span style="color: rgb(204, 102, 51);">set</span> pm_raw [getFeedback <span style="color: rgb(0, 96, 128);">"Pool member [expr $y +1] IP:Port and ratio (Ex. 1.1.1.1:80 1)? "</span>]
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>         <span style="color: rgb(204, 102, 51);">set</span> pm_value <span style="color: rgb(0, 96, 128);">"[lindex [split $pm_raw "</span> <span style="color: rgb(0, 96, 128);">"] 0] \{ ratio [lindex [split $pm_raw "</span> <span style="color: rgb(0, 96, 128);">"] 1] \}"</span>
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> pmembers($pl_name) $pm_value
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>     }
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>     <span style="color: rgb(204, 102, 51);">foreach</span> z $pmembers($pl_name) { <span style="color: rgb(204, 102, 51);">append</span> pmmod <span style="color: rgb(0, 96, 128);">"$z "</span> }
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>     <span style="color: rgb(0, 0, 255);">tmsh::create</span> /gtm pool $pl_name members add \{ $pmmod \} \
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span>         load-balancing-mode ratio verify-member-availability disabled
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span> }
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span> <span style="color: rgb(204, 102, 51);">puts</span> <span style="color: rgb(0, 96, 128);">"\nPools created...\n\n"</span>

Creating the WideIP

To create the WideIP, the block looks nearly identical to creating the pools, except for the elimination of the nested for loop and the different tmsh command in use.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 128, 0);">#Build WideIP</span>
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> <span style="color: rgb(204, 102, 51);">set</span> wip_name [getFeedback <span style="color: rgb(0, 96, 128);">"WideIP Name? "</span>]
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span> <span style="color: rgb(204, 102, 51);">set</span> wip_pl_count [getFeedback <span style="color: rgb(0, 96, 128);">"How many pools? "</span>]
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span> <span style="color: rgb(204, 102, 51);">for</span> {set x 0} {$x<$wip_pl_count} {incr x} {
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>     <span style="color: rgb(204, 102, 51);">set</span> pl_raw [getFeedback <span style="color: rgb(0, 96, 128);">"Pool [expr $x +1] name a ratio (Ex. pool1 1)? "</span>]
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     <span style="color: rgb(204, 102, 51);">set</span> pl_value <span style="color: rgb(0, 96, 128);">"[lindex [split $pl_raw "</span> <span style="color: rgb(0, 96, 128);">"] 0] \{ ratio [lindex [split $pl_raw "</span> <span style="color: rgb(0, 96, 128);">"] 1] \}"</span>
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>     <span style="color: rgb(204, 102, 51);">lappend</span> wip_pools($wip_name) $pl_value
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span> }
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span> <span style="color: rgb(204, 102, 51);">foreach</span> z $wip_pools($wip_name) { <span style="color: rgb(204, 102, 51);">append</span> wipplmod <span style="color: rgb(0, 96, 128);">"$z "</span> }
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span> <span style="color: rgb(0, 0, 255);">tmsh::create</span> /gtm wideip $wip_name pool-lb-mode ratio pools add \{ $wipplmod \} \
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>     persistence enabled ttl-persistence 300 rules add \{ testwip-rule \}
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span> <span style="color: rgb(204, 102, 51);">puts</span> <span style="color: rgb(0, 96, 128);">"\nWideIP created, configuration is complete.\n\n"</span>

And that’s a wrap for the script!  Now let’s run it.

Running the Script

root@golgotha(Active)(tmos)# run cli script test1.tcl
How many datacenters do you wish to create? 2
Datacenter 1 name? dc1
Datacenter 2 name? dc2

Datacenters created…

How many servers are you adding? 2
Server 1 Name? ltm1
Server 1 IP? 10.10.100.1
Datacenter server belongs to (tmsh::get_config /gtm datacenter)? dc1
How mnay virtuals for ltm1? 3
Virtual Server 1 IP:Port? 10.10.100.10:80
Virtual Server 2 IP:Port? 10.10.100.11:80
Virtual Server 3 IP:Port? 10.10.100.12:80
Server 2 Name? ltm2
Server 2 IP? 10.10.200.1
Datacenter server belongs to (tmsh::get_config /gtm datacenter)? dc2
How mnay virtuals for ltm2? 3
Virtual Server 1 IP:Port? 10.10.200.10:80
Virtual Server 2 IP:Port? 10.10.200.11:80
Virtual Server 3 IP:Port? 10.10.200.12:80

Servers created…

How many pools are you adding? 2
Pool 1 name? gpool1
How many pool members? 3
Pool member 1 IP:Port and ratio (Ex. 1.1.1.1:80 1)? 10.10.100.10:80 1
Pool member 2 IP:Port and ratio (Ex. 1.1.1.1:80 1)? 10.10.100.11:80 2
Pool member 3 IP:Port and ratio (Ex. 1.1.1.1:80 1)? 10.10.100.12:80 3
Pool 2 name? gpool2
How many pool members? 3
Pool member 1 IP:Port and ratio (Ex. 1.1.1.1:80 1)? 10.10.200.10:80 1
Pool member 2 IP:Port and ratio (Ex. 1.1.1.1:80 1)? 10.10.200.11:80 2
Pool member 3 IP:Port and ratio (Ex. 1.1.1.1:80 1)? 10.10.200.12:80 3

Pools created…

WideIP Name? test.wip.com
How many pools? 2
Pool 1 name a ratio (Ex. pool1 1)? gpool1 1
Pool 2 name a ratio (Ex. pool1 1)? gpool2 2

WideIP created, configuration is complete.

Obviously, since there’s no error checking in this script, typos will kill, so I’d encourage you to include error checking on the data entry.  So now that our configuration is complete, I’ve modified this entry from the codeshare to watch our new pools as I run some test traffic against the new WideIP:

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(204, 102, 51);">proc</span> <span style="color: rgb(0, 0, 255);">script::init</span> {} {
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span>     <span style="color: rgb(204, 102, 51);">set</span> ::pool_ids <span style="color: rgb(0, 96, 128);">""</span>
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span> }
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span> <span style="color: rgb(204, 102, 51);">proc</span> get_stats { resultsArray } {
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>     <span style="color: rgb(204, 102, 51);">upvar</span> $resultsArray results
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>     <span style="color: rgb(204, 102, 51);">set</span> idx 0
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>     <span style="color: rgb(204, 102, 51);">set</span> objs [<span style="color: rgb(0, 0, 255);">tmsh::get_status</span> gtm pool $::pool_ids raw]
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>     <span style="color: rgb(204, 102, 51);">set</span> count [llength $objs]
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span>     <span style="color: rgb(204, 102, 51);">while</span> { $idx < $count } {
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span>
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span>         <span style="color: rgb(204, 102, 51);">set</span> obj [lindex $objs $idx]
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>         <span style="color: rgb(204, 102, 51);">set</span> pool [<span style="color: rgb(0, 0, 255);">tmsh::get_name</span> $obj]
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span>
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> results($pool) preferred
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> results($pool) \
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span>             [<span style="color: rgb(0, 0, 255);">tmsh::get_field_value</span> $obj <span style="color: rgb(0, 96, 128);">"preferred"</span>]
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span>
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> results($pool) alternate
<span id="lnum23" style="color: rgb(96, 96, 96);">  23:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> results($pool) \
<span id="lnum24" style="color: rgb(96, 96, 96);">  24:</span>             [<span style="color: rgb(0, 0, 255);">tmsh::get_field_value</span> $obj <span style="color: rgb(0, 96, 128);">"alternate"</span>]
<span id="lnum25" style="color: rgb(96, 96, 96);">  25:</span>
<span id="lnum26" style="color: rgb(96, 96, 96);">  26:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> results($pool) dropped
<span id="lnum27" style="color: rgb(96, 96, 96);">  27:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> results($pool) \
<span id="lnum28" style="color: rgb(96, 96, 96);">  28:</span>             [<span style="color: rgb(0, 0, 255);">tmsh::get_field_value</span> $obj <span style="color: rgb(0, 96, 128);">"dropped"</span>]
<span id="lnum29" style="color: rgb(96, 96, 96);">  29:</span>
<span id="lnum30" style="color: rgb(96, 96, 96);">  30:</span>         <span style="color: rgb(204, 102, 51);">incr</span> idx
<span id="lnum31" style="color: rgb(96, 96, 96);">  31:</span>     }
<span id="lnum32" style="color: rgb(96, 96, 96);">  32:</span> }
<span id="lnum33" style="color: rgb(96, 96, 96);">  33:</span>
<span id="lnum34" style="color: rgb(96, 96, 96);">  34:</span> <span style="color: rgb(204, 102, 51);">proc</span> <span style="color: rgb(0, 0, 255);">script::run</span> {} {
<span id="lnum35" style="color: rgb(96, 96, 96);">  35:</span>     <span style="color: rgb(204, 102, 51);">for</span> {set idx 1} {$idx < $<span style="color: rgb(0, 0, 255);">tmsh::argc</span>} {incr idx} {
<span id="lnum36" style="color: rgb(96, 96, 96);">  36:</span>         <span style="color: rgb(204, 102, 51);">lappend</span> ::pool_ids [lindex $<span style="color: rgb(0, 0, 255);">tmsh::argv</span> $idx]
<span id="lnum37" style="color: rgb(96, 96, 96);">  37:</span>     }
<span id="lnum38" style="color: rgb(96, 96, 96);">  38:</span>
<span id="lnum39" style="color: rgb(96, 96, 96);">  39:</span>     <span style="color: rgb(204, 102, 51);">array</span> <span style="color: rgb(204, 102, 51);">set</span> r1 {}
<span id="lnum40" style="color: rgb(96, 96, 96);">  40:</span>     <span style="color: rgb(204, 102, 51);">array</span> <span style="color: rgb(204, 102, 51);">set</span> r2 {}
<span id="lnum41" style="color: rgb(96, 96, 96);">  41:</span>
<span id="lnum42" style="color: rgb(96, 96, 96);">  42:</span>     <span style="color: rgb(204, 102, 51);">set</span> interval 2
<span id="lnum43" style="color: rgb(96, 96, 96);">  43:</span>     <span style="color: rgb(204, 102, 51);">set</span> delay [expr $interval * 1000]
<span id="lnum44" style="color: rgb(96, 96, 96);">  44:</span>
<span id="lnum45" style="color: rgb(96, 96, 96);">  45:</span>     get_stats r1
<span id="lnum46" style="color: rgb(96, 96, 96);">  46:</span>
<span id="lnum47" style="color: rgb(96, 96, 96);">  47:</span>     <span style="color: rgb(204, 102, 51);">while</span> { true } {
<span id="lnum48" style="color: rgb(96, 96, 96);">  48:</span>         <span style="color: rgb(204, 102, 51);">after</span> $delay
<span id="lnum49" style="color: rgb(96, 96, 96);">  49:</span>         get_stats r2
<span id="lnum50" style="color: rgb(96, 96, 96);">  50:</span>         <span style="color: rgb(0, 0, 255);">tmsh::clear_screen</span>
<span id="lnum51" style="color: rgb(96, 96, 96);">  51:</span>
<span id="lnum52" style="color: rgb(96, 96, 96);">  52:</span>         <span style="color: rgb(204, 102, 51);">foreach</span> { pool } [lsort [array names r1]] {
<span id="lnum53" style="color: rgb(96, 96, 96);">  53:</span>
<span id="lnum54" style="color: rgb(96, 96, 96);">  54:</span>             <span style="color: rgb(204, 102, 51);">if</span> { [string length [array names r2 -exact $pool]] == 0 } {
<span id="lnum55" style="color: rgb(96, 96, 96);">  55:</span>                 <span style="color: rgb(204, 102, 51);">puts</span> <span style="color: rgb(0, 96, 128);">"$pool: no sample"</span>
<span id="lnum56" style="color: rgb(96, 96, 96);">  56:</span>                 <span style="color: rgb(204, 102, 51);">continue</span>
<span id="lnum57" style="color: rgb(96, 96, 96);">  57:</span>             }
<span id="lnum58" style="color: rgb(96, 96, 96);">  58:</span>
<span id="lnum59" style="color: rgb(96, 96, 96);">  59:</span>             <span style="color: rgb(204, 102, 51);">set</span> line [format <span style="color: rgb(0, 96, 128);">"%-20s"</span> $pool]
<span id="lnum60" style="color: rgb(96, 96, 96);">  60:</span>
<span id="lnum61" style="color: rgb(96, 96, 96);">  61:</span>             <span style="color: rgb(204, 102, 51);">set</span> s1 $r1($pool)
<span id="lnum62" style="color: rgb(96, 96, 96);">  62:</span>             <span style="color: rgb(204, 102, 51);">set</span> s2 $r2($pool)
<span id="lnum63" style="color: rgb(96, 96, 96);">  63:</span>
<span id="lnum64" style="color: rgb(96, 96, 96);">  64:</span>             <span style="color: rgb(204, 102, 51);">set</span> idx 0
<span id="lnum65" style="color: rgb(96, 96, 96);">  65:</span>             <span style="color: rgb(204, 102, 51);">set</span> count [llength $s1]
<span id="lnum66" style="color: rgb(96, 96, 96);">  66:</span>             <span style="color: rgb(204, 102, 51);">while</span> { $idx < $count } {
<span id="lnum67" style="color: rgb(96, 96, 96);">  67:</span>                 <span style="color: rgb(204, 102, 51);">append</span> line <span style="color: rgb(0, 96, 128);">"[lindex $s1 $idx] "</span>
<span id="lnum68" style="color: rgb(96, 96, 96);">  68:</span>                 <span style="color: rgb(204, 102, 51);">incr</span> idx
<span id="lnum69" style="color: rgb(96, 96, 96);">  69:</span>
<span id="lnum70" style="color: rgb(96, 96, 96);">  70:</span>                 <span style="color: rgb(204, 102, 51);">set</span> stat \
<span id="lnum71" style="color: rgb(96, 96, 96);">  71:</span>                     [expr ([lindex $s2 $idx] - [lindex $s1 $idx]) / $interval]
<span id="lnum72" style="color: rgb(96, 96, 96);">  72:</span>                 <span style="color: rgb(204, 102, 51);">append</span> line <span style="color: rgb(0, 96, 128);">"[format "</span>%-12s<span style="color: rgb(0, 96, 128);">" $stat]"</span>
<span id="lnum73" style="color: rgb(96, 96, 96);">  73:</span>                 <span style="color: rgb(204, 102, 51);">incr</span> idx
<span id="lnum74" style="color: rgb(96, 96, 96);">  74:</span>             }
<span id="lnum75" style="color: rgb(96, 96, 96);">  75:</span>             <span style="color: rgb(204, 102, 51);">puts</span> $line
<span id="lnum76" style="color: rgb(96, 96, 96);">  76:</span>         }
<span id="lnum77" style="color: rgb(96, 96, 96);">  77:</span>
<span id="lnum78" style="color: rgb(96, 96, 96);">  78:</span>         <span style="color: rgb(0, 128, 0);"># use the most recent results as the next previous results</span>
<span id="lnum79" style="color: rgb(96, 96, 96);">  79:</span>         <span style="color: rgb(204, 102, 51);">array</span> <span style="color: rgb(204, 102, 51);">set</span> r1 [array get r2]
<span id="lnum80" style="color: rgb(96, 96, 96);">  80:</span>         <span style="color: rgb(204, 102, 51);">array</span> <span style="color: rgb(204, 102, 51);">unset</span> r2
<span id="lnum81" style="color: rgb(96, 96, 96);">  81:</span>     }
<span id="lnum82" style="color: rgb(96, 96, 96);">  82:</span> }
<span id="lnum83" style="color: rgb(96, 96, 96);">  83:</span>
<span id="lnum84" style="color: rgb(96, 96, 96);">  84:</span> <span style="color: rgb(204, 102, 51);">proc</span> <span style="color: rgb(0, 0, 255);">script::help</span> {} {
<span id="lnum85" style="color: rgb(96, 96, 96);">  85:</span>     <span style="color: rgb(0, 0, 255);">tmsh::add_help</span> <span style="color: rgb(0, 96, 128);">"enter zero or more pool names"</span>
<span id="lnum86" style="color: rgb(96, 96, 96);">  86:</span> }
<span id="lnum87" style="color: rgb(96, 96, 96);">  87:</span>
<span id="lnum88" style="color: rgb(96, 96, 96);">  88:</span> <span style="color: rgb(204, 102, 51);">proc</span> <span style="color: rgb(0, 0, 255);">script::tabc</span> {} {
<span id="lnum89" style="color: rgb(96, 96, 96);">  89:</span>     <span style="color: rgb(204, 102, 51);">foreach</span> {pool} [<span style="color: rgb(0, 0, 255);">tmsh::get_config</span> /gtm pool] {
<span id="lnum90" style="color: rgb(96, 96, 96);">  90:</span>         <span style="color: rgb(0, 0, 255);">tmsh::add_tabc</span> [<span style="color: rgb(0, 0, 255);">tmsh::get_name</span> $pool]
<span id="lnum91" style="color: rgb(96, 96, 96);">  91:</span>     }
<span id="lnum92" style="color: rgb(96, 96, 96);">  92:</span> }

The output from the script above looks like this when running “run cli script watch_gtmPools.tcl”:

gpool1              preferred 5           alternate 0           dropped 0
gpool2              preferred 9           alternate 0           dropped 0

The complete script for the gtm configuration example above can be found here in the codeshare.  The GTM pool monitor script is also in the codeshare.  Happy scripting!

i a running v11. is there any equivalent?

scripting is still supported in v11.