XenApp Auto Scaling Script


I wrote a similar script for a customer running XenApp 6.5 in AWS. The script below has been heavily updated for XenApp 7.x and is much more advanced than the XenApp 6.5 script. It uses the Hypervisior Power Management commands from XenApp to turn machines on and off so will work in any environment that has a Hypervisior connection configured, not just AWS.

I have only tested this script in my lab environment so I am looking for some beta testers.

The script is designed to run in a infinite loop which is broken down into a Scale Up period and a Scale Down period.

During the Scale Up period the script monitors the average load across each Delivery Group and powers on a new server once the load reaches the value set in ScaleUpLoad parameter. If there are no servers powered on from a Delivery Group when the script starts it will power 1 server on to get the ball rolling, as such.

The script disables logons to a percentage of servers during the Scale Down period, and then shuts down a server when there are no more sessions remaining on the server. Only servers that are powered on, registered, and not in maintenance mode will be touched by the script during scale down. If 50% of servers in a Delivery Group are in maintenance mode then a percentage of the remaining 50% will be kept online and the rest will be shutdown by the script (when / if no users sessions remain).

The ReverseScaleLoad parameter defines at what load servers should be taken out of Maintenance Mode or powered back on if the remaining servers get overloaded during the Scale Down period.

If you want a server to be ignored by the script, tag it with NoAutoScaling.

The script logs information to the XenApp configuration logging database as can be seen in the screenshot below.

Auto Scaling Logging Auto Scaling Logging 2

 

I have built the following fail-safes into the script to try to prevent a production outage where servers don't get powered on or powered off due to the script having failed.

  • The script can be run multiple times on multiple controllers. The script will only ever stay running on 1 controller at a time (Use a scheduled task to run the script every 10 minutes)
  • The script periodically checks the list of controllers it is talking to and only selects controllers which are active. By default it will talk to the controller it is running on if it is active, if not active it will try a remote controller.
  • If the script starts on a controller where the broker services is down (and therefore cannot retrieve the list of other controllers) it will exit to allow another controller to run the script
  • The script saves key information to XenApp tags, allowing another controllers / script process to pick up where another script failed.
  • A kill switch has been built in to allow for emergencies where maximum capacity may be required. When a flag file is placed in the $Path directory, all servers are turned on and taken out of Maintenance Mode.

A few other notes.

  • Due to the way the script runs in an infinite loop, when you first run the script it may consume high CPU until it hits the first loop. So if your Scale Up time is going to start at 07:00, schedule the script to start at 06:50 the first time and repeat every 10 minutes. Once it enters the first loop, CPU will go down.
  • Don't rename the script file otherwise the check to see if the script is already running locally or on other controllers will fail.

Desktop Groups must be tagged with AutoScaling to be included in the script, this allows Desktop Groups to be added and removed from the script without having to change or stop the scheduled task on all the controllers. Desktop Groups tagged with NoAutoScaling will be ignored, even if tagged with AutoScaling as well.

Use the following command to create the tag

And this command to add the tag to the desired desktop group, in this example the Engineering Desktop Group

We don't have a code signing certificate yet so both script are available as .txt and .ps1 unsigned.

XenApp_Auto_Scaling - Unsigned with ps1 extension (236 downloads) XenApp_Auto_Scaling - Unsigned with txt Extension (178 downloads)

 

There may be times where you need to clear the tags and tag metadata during testing or if you manually stop the script one day and restart it the next day. For this reason I've written this other small script which will clear the tags for you.