Example Rules

This section provides a series of examples to show how the Traffic Manager might use TrafficScript rules to inspect and manipulate the traffic to your services.

Routing by Content Type

This example inspects the URL path in an HTTP request. It chooses a pool to handle the request according to the content type found in the URL path.

Before creating the rule, set up pools called “windows”, “java”, and “linux” respectively.

$path = http.getPath();

if( string.endsWith( $path, ".jsp" )) {

   pool.use( "java" );

} else if( string.endsWith( $path, ".asp" )) {

   pool.use( "windows" );

} else {

   pool.use( "linux" );

}

Restricting Access Based on Time of Day

This example allows access to a particular service only during typical office hours (in this case, between 9am and 6pm, Monday to Friday). It discards all connections that occur outside these times.

$dayofweek = sys.time.weekDay();

$hourofday = sys.time.hour();

 

# $dayofweek: Sunday is 1, Saturday is 7

# $hourofday: office hours are between 9am and 5:59pm

if( $dayofweek == 1 || $dayofweek == 7 || $hourofday < 9 || $hourofday >= 18 ) {

   connection.discard();

}

In practice, it might be more appropriate to direct restricted traffic to a separate “error pool” of servers rather than just dropping the connection without warning. The servers in the error pool would be configured to return an appropriate error message before closing the connection. The procedure for doing this depends on the protocol being balanced.

Customer Prioritization

This example inspects the cookie in an HTTP request. It uses the value of the cookie to determine which pool to select. One pool is faster than the other because it contains machines reserved for premium users.

A company has a customer base divided into “gold” and “silver” membership. It wants to give priority to gold customers and has five servers, yellow, green, blue, black, and purple.

Two pools are created: “standard”, for silver customers, containing machines yellow, green and blue; and “premium”, for gold customers, which includes all five servers. Thus, black and purple are only available to gold customers.

The site uses a cookie login system, with the customer type encoded in the cookie. The Traffic Manager can differentiate between membership levels and send traffic to the correct pool:

$cookie = http.getHeader( "cookie" );

if( string.contains( $cookie, "gold" )) {

   pool.use( "premium" );

} else {

   pool.use( "standard" );

}

Managing Levels of Service

This example tags premium customers with a “premium” Service Level Monitoring (SLM) class, and directs them to the “premium” pool.

Non-premium customers can share the premium pool if the premium SLM class is functioning within its tolerance, but are directed to the “standard” pool if the premium SLM class is running too slowly.

# $isPremium could be based on the presence of a

# login cookie, the contents of the shopping cart,

# or even the remote address of the client.

 

if( $isPremium ) {

   connection.setServiceLevelClass( "premium" );

   pool.use( "premium" );

} else {

   if( slm.conforming( "premium" ) == 100 ) {

      pool.use( "premium" );

   } else {

      pool.use( "standard" );

   }

}

Routing Based on XML Traffic

TrafficScript includes support for parsing XML documents using XPath, an industry-standard language used to query XML documents.

XML documents are used by SOAP-based protocols such as Web services, and enable complex data to be exchanged and understood automatically without user intervention.

An XML document is organized into a tree structure of nodes1. Each node can contain a piece of data, or further nodes. XPath can navigate through these nodes to extract specific data from the XML document; this data can then be used by the Traffic Manager to make routing decisions on the traffic.

The XPath 1.0 specification is available at http://www.w3.org/TR/xpath.

Example: Google Search Request

The Google™ search engine has a Web services interface that accepts SOAP requests for search queries. A request for a search for Ivanti, Inc. would consist of an HTTP POST containing the following XML body data:

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope

   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"

   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

   xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"

   xmlns:xsd="http://www.w3.org/1999/XMLSchema">

   <SOAP-ENV:Body>

      <namesp1:doGoogleSearch xmlns:namesp1="urn:GoogleSearch">

         <key xsi:type="xsd:string">googleUniqueID</key>

         <q xsi:type="xsd:string">Ivanti, Inc.</q>

         <start xsi:type="xsd:int">0</start>

         <maxResults xsi:type="xsd:int">10</maxResults>

         <filter xsi:type="xsd:boolean">false</filter>

         <restrict xsi:type="xsd:string"/>

         <safeSearch xsi:type="xsd:boolean">false</safeSearch>

         <lr xsi:type="xsd:string"/>

         <ie xsi:type="xsd:string">latin1</ie>

         <oe xsi:type="xsd:string">latin1</oe>

      </namesp1:doGoogleSearch>

   </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

Note that the SOAP body contains a doGoogleSearch node. This contains the parameters of the search request.

An Internet service might implement or proxy doGoogleSearch requests, and the Traffic Manager might be used to manage the traffic to this service.

For example, it may be necessary to split doGoogleSearch requests according to the specified maximum number of results. If maxResults is greater than 100, the request is to be sent to pool googleLarge; otherwise it should be sent to pool google.

A TrafficScript rule can use the functions xml.XPath.MatchNodeSet() and xml.XPath.MatchNodeCount() to query the SOAP request body and test XML nodes:

# Read the entire body of the SOAP/HTTP request

$body = http.getBody();

 

# XML parameters lie in the "urn:GoogleSearch" XML

# namespace:

$googlens = "xmlns:googlens=\"urn:GoogleSearch\"";

 

# Test for the presence of a "doGoogleSearch" node.

# If present, get the value of the "maxResults"

# parameter and choose the pool

 

if( xml.XPath.MatchNodeCount( $body, $googlens, "//googlens:doGoogleSearch" ) ) {

   $maxResults = xml.XPath.MatchNodeSet( $body, $googlens,

                                         "//googlens:doGoogleSearch/maxResults/text()" );

 

   if( $maxResults >= 100 ) {

      pool.use( "googleLarge" )

   } else {

      pool.use( "google" );

   }

}