<<

K5917: Using regular expressions in a health monitor receive string

Non-Diagnostic

Original Publication Date: Oct 20, 2015

Update Date: Aug 24, 2021

Topic

Some BIG-IP application health monitors, such as the HTTP monitor, include a Receive String field. This field specifies a string for comparison with the server response. You can specify a in that field to provide some flexibility in matching an expected response from a healthy server. Health monitors that support regular expressions include TCP, HTTP, HTTPS, and User Datagram Protocol (UDP).

Regular expressions provide the flexibility for identifying strings of text that are of interest, such as certain characters, words, or a pattern of characters. With some exceptions, health monitors on BIG-IP systems support the use of POSIX Extended Regular Expressions (ERE). ERE syntax treats most characters as literals, meaning that they match only themselves, and defines metacharacters, which can be used to represent special characters, multiple characters, or a sequences of characters.

To reduce operating overhead for the BIG-IP system, F5 recommends that, whenever possible, you use static strings for monitoring. Using static strings instead of regular expressions for monitoring is simpler and more efficient. However, the performance impact is minimal in comparison to the administrative flexibility offered from using regular expressions.

For example, your HTTP health monitor requests the /admin/monitor. page from the web server, which invokes a simple local script. The script checks whether vital local operating parameters, such as connections, memory, processor, or disk space, are within acceptable limits. If so, the script returns the monitor.html page, which includes a single line of body text that varies for each server, based on the server name:

Server1: OK Server2: OK Server3: OK

If you do not use a regular expression to match this response, you must create three separate monitors, each with a different receive string, and ensure the correct monitor is applied to each server. However, you can create a single monitor that uses ERE metacharacters in a receive string regular expression that will match the expected healthy response regardless of the variation in the server name. The following examples match all of the server responses above, with varying levels of specificity:

Server.: OK Server[0-9]: OK Server[123]: OK Constructing BIG-IP monitor Receive Strings using regular expressions

Two differences exist between standard regular expression logic and that implemented in the BIG-IP receive rule engine:

The BIG-IP LTM receive rule engine is case insensitive; a lowercase letter will match both the uppercase and lowercase of the same letter.

Note: The receive rule engine functionality differs between BIG-IP LTM and BIG-IP GTM; while the BIG-IP LTM receive rule engine is not case-sensitive, the BIG-IP GTM receive rule engine is case- sensitive.

The entire server response is processed as one data stream, which the receive rule engine evaluates against the regular expression as a single line. As a result, there is only one line beginning (the beginning of the response line), there is only one line ending (the end of the response), and white space or boundary characters may not match, as expected.

The following sections detail the use of metacharacters in receive strings. The examples in each section assume that the following response is expected from a healthy server:

HTTP/1.0 200 OK\r\n Server: Microsoft-IIS/5.0 ...


win2000.gif

Alternation

You can use the pipe metacharacter (|) to match on one of a set. A match is achieved if one of the set matches. You may observe the results listed in the following table.

Receive string Match? Explanation http|OK Yes Both http and OK are present in the response. OK|htp Yes OK is present in the response. bbb|htp No Neither bbb nor htp is present in the response. bbb|http Yes HTTP is present in the response.

Line anchors

A caret (^) signifies the start of line. A dollar sign ($) signifies the end of line anchor.

The line anchors do not work as they do in standard regular expressions: the rule engine sees the beginning of the response as the start of line, and the end of the response data as the end of line. You may observe the results listed in the following table.

Receive Match? Explanation string OK\r\n$ No In the example response above, the receive rule engine sees only a single start-of-line followed by the string HTTP. In the example response above, the receive rule engine sees only a single end-of-line >$ Yes preceded by the string . In the example response above, the receive rule engine sees only a single start-of-line ^http Yes followed by the string HTTP. ^server No "SERVER" is not the start of a line, even though it comes after a CR/LF sequence (\r\n).

Wildcards and quantification

Expression Matches . Any single character ? Zero, or one instance of the previous character * Zero, or more instances of the previous character .? Zero, or one instance of any character .* Zero, or more instances of any character

You may observe results listed in the following table.

Expression Matches hp https h.*p h123p "he ate a pie" etc? "HTTP/1.0 200 OK" h.*k ?hong kong? etc? 10 1a 1. 1/ etc? nul Z? Z nul Z Z* Zz zzzzzzz etc?

Bracket expressions A bracket expression matches any single character that is contained within the brackets. For example, [abc] matches a, b, or c. In addition, [a-z] specifies a range that matches any lowercase letter from a to z. These forms can be mixed.

Expression Matches [p-t] Any character in the set p, q, r, s, or t [eou] Any character in the set e, o, or u [\w] Any alphanumeric character

Negation

The [^value] works to negate the individual entry, as in the examples listed in the following table.

Expression Matches [^e] Any character except e [^p-t] Any character except p, q, r, s, or t [^eou] Any character except e, o, or u [^\w] Any non-alphanumeric character

Useful special sequences

Sequence Matches Equivalent to \d Any decimal digit [0-9] \D Any non-digit character [^0-9] \s Any whitespace character [\t\n\r\f\v] \S Any non-whitespace character [^\t\n\r\f\v] \w Any alphanumeric character [a-zA-Z0-9_] \W Any non-alphanumeric character [^a-zA-Z0-9_]

You may observe results listed in the following table.

Receive string Match? Explanation 200\sOK Yes There is a single space between 200 and OK. 200\s\sOK No There is only one space, not two. 200\wOK No Whitespace is not an alphanumeric character.

Character substitution and repetition

Expression Matches {n} Repeats the previous item exactly n times. {n,m} Repeats the previous item at least n, but no more than m times.

Note: Both n and m must be greater than zero (0). If m is null, infinity is assumed. You may observe results listed in the following table.

Expression Matches Equivalent to htp ht{1,2}p http htt?p https hp https h.*p h123p "he ate a pie" http ht{2,}p httttttttttp h.*k "HTTP/1.0 200 OK"

Grouping

You can use parentheses to group characters, which is especially useful for selective alternation, as listed in the following table.

Expression Matches http/1.0 (http|bb)/1 bb/1.

Example of using a combination of wildcards, grouping, and alternation

You can use a combination of wildcards, grouping, and alternation expressions to refine your required match results. For example, consider a pool that contains five members that return content similar to the following example:

Host1:10.10.10.201 Region:California, Alaska, Texas, New Jersey, Florida Host2:10.10.10.202 Region:California, Alaska, Texas Host3:10.10.10.203 Region:California, New Jersey, Florida Host4:10.10.10.204 Region:Florida, Texas, New Jersey, California Host5:10.10.10.205 Region:Florida, Texas, New Jersey

You may be able to use the combination of wildcard, grouping, and alternation expressions listed in the following table to create the monitor receive string.

Scenarios Expression Match results To match the resources that contain any of the two Host 1, 2, 3, 4 regions, (California)|(Florida) and 5 California or Florida, in any order. To match the resources that contain specifically two regions, (California.*Florida) Host 1 and 3. California and Florida, in the exact order. To match the resources with specifically two regions, (California.*Florida)|(Florida. Host 1, 3 and 4. California and Florida, in any order. *California)

Character expansion

Some special characters can be represented using the metacharacter (\) to invoke the appropriate escape sequence. For a list of escape sequences that can be used to represent special characters, such as the carriage return or linefeed characters, refer to K4186: Entering escape sequences in health monitor Send and Receive strings.

You may observe the results listed in the following table.

Receive Match? Explanation string \n Yes A newline exists in the response. OK\n No Each newline in the response is preceded by a carriage return. OK\r\n Yes This string exists in the response. Although this appears to be two input lines, the matching operator sees the entire OK\r\nServer Yes response as one stream.

Escaping metacharacters

The backslash metacharacter (\) may also be used to escape other metacharacters so that they can be interpreted literally. For example, if you want to find A+, you type the following:

A\+

Supplemental Information

K8718: Monitor strings containing specific characters may cause eXtremeDB error messages K8584: Using regular expressions within BIG-IP iRules The BIG-IP Local Traffic Manager: Monitors Reference manual

Note: For information about how to locate F5 product manuals, refer to K12453464: Finding product documentation on AskF5.

For more information about EREs and regular expressions, refer to the man pages for grep, or to the following documents:

Important: Only the modified POSIX ERE described in this article are supported in BIG-IP health monitors, and some examples from the sources in the following list may not be supported in a BIG-IP health monitor receive string.

Note: The following links take you to a resource outside of AskF5. The third party could remove the documents without our knowledge.

http://www.dreambank.net/regex.html http://en.wikipedia.org/wiki/Regular_expression http://en.wikipedia.org/wiki/Regular_expression#POSIX_Extended_Regular_Expressions http://www.regular-expressions.info/

Applies to:

Product: BIG-IP, BIG-IP AFM, BIG-IP APM, BIG-IP ASM, BIG-IP DNS, BIG-IP GTM, BIG-IP Link Controller, BIG-IP LTM, BIG-IP PEM, BIG-IP AAM 16.X.X, 15.1.X, 14.X.X, 13.X.X, 12.X.X, 11.X.X, 10.X.X, 9.X.X

Product: Legacy Products, BIG-IP WebAccelerator, BIG-IP PSM, BIG-IP Edge Gateway 16.X.X, 15.1.X, 14.X.X, 13.X.X, 12.X.X, 11.X.X, 10.X.X, 9.X.X