GTM Monitor Weight Change

Problem this snippet solves:

This iCall script can be used to change the virtual server score (VS Score) of an LTM virtual server that is being monitored by a Global Traffic Manager. Normally, when a virtual server has a default pool assigned, the GTM administrator would select the VS Capacity load balancing method to choose a virtual server based on the number of available pool members in the default pool. When there is no default pool (the pool assignment is being made by an iRule, for example), LTM does not associate the pool capacity information with the virtual server, and GTM cannot access it. This script will express the ratio of available pool members to total pool members as a percentage, and set the VS Score on the specified virtual server to that percentage, or set it to 1 if there are no pool members available. The GTM admin can then select VS Score as the GTM load balancing method.

How to use this snippet:

Implementation Details

This iCall script requires v11.4 or higher.

Code :

###periodic handler to run the script every 10 seconds
###tmsh command to disable script for maintenance, etc: modify sys icall handler periodic gtm_score_changer status inactive
sys icall handler periodic gtm_score_changer_periodic {
    interval 10
    script gtm_score_changer

###script to change virtual server VS Score based on number of available pool members in an arbitrary pool (that is, not the default pool of the VS)
###this script will express the ratio of available pool members to total pool members as a percentage, and set the VS Score to that percentage or 1 if there are no pool members available
sys icall script gtm_score_changer {
    app-service none
    definition {
###name of virtual server for which we will change VS Score
###replace icall_virtual with the name of your virtual server
        set vs icall_virtual
###get current VS Score for comparison
set vs_list [tmsh::get_config ltm virtual $vs]
        if { [llength $vs_list] != 0 } {
            set vs_config [lindex $vs_list 0]
        } else {
            puts "virtual server not found"
            return 0
        set current_vs_score [tmsh::get_field_value $vs_config "gtm-score"]
###get number of pool members that are both enabled and healthy
set enabled_count 0
###replace icall_pool with the name of your pool
set pool_list [[tmsh::get_config ltm pool icall_pool members]
if { [llength $pool_list] != 0 }  {
set pool_members [lindex $pool_list 0] 
set total_count 0
} else {
puts "pool not found"
return 0
###iterate through pool members to get their state and health status
        foreach member [tmsh::get_field_value $pool_members "members"] {
incr total_count
            set name [tmsh::get_name $member]
            set abled [tmsh::get_field_value $member "session"]
            set state [tmsh::get_field_value $member "state"]
            if { $abled eq "monitor-enabled" && $state eq "up" } {
                incr enabled_count
###convert integers to floating point numbers to calculate percentage
        set enabled_count [expr double($enabled_count)]
        set total_count [expr double($total_count)]
        if { $enabled_count != 0 } {
##divide number of available members by number of total members, convert to a percentage, then convert back to an integer for use in tmsh command
            set new_vs_score [expr (int(($enabled_count/$total_count)*100))]
            if { $current_vs_score != $new_vs_score } {
###modify VS Score on virtual server if score has changed
                tmsh::modify ltm virtual $vs gtm-score $new_vs_score
                puts "VS Score changed to: $new_vs_score."
            } else {
                puts "VS Score unchanged."
        } else {
###since a gtm-score value is required, set VS Score to 1 if no pool members are available
            tmsh::modify ltm virtual $vs gtm-score 1
            puts "No pool members available; VS Score is 1."
    description none
    events none
Published Mar 09, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment