on 10-Dec-2007 07:28
When dealing with iRules there is sometimes a need to store static information in lists that you can search when your iRule is executed. Are you looking to check every incoming connection for a certain list of Client IPs? Perhaps you want to parse the incoming URI and direct to different pools based on what URI parts are found. To perform inspections/actions like this you need to have a defined list of data to search for, and that list needs to remain constant across multiple connections. This is exactly what classes are designed for. Other articles in the series:
We'll be going over some of the common questions that seem to crop up when talking to/with people about classes in iRules. Hopefully by the time we're through here you'll have a clear understanding of what classes are, how you can use them, and perhaps even when/why you would. 😉
What is the difference between a "class" and a "Data Group" when dealing with F5 systems?
Nothing! These terms are interchangeable, which can sometimes throw people off. They are referred as "Data Groups" via the GUI, and "class"(es) via the configuration file. This can be a bit confusing, but I assure you they really mean the same thing. For the rest of this document, however, I will refer to them as classes.
Are there different types of classes?
Yes. There are four kinds of classes that you can choose to make use of via iRules. Each of these, as you might imagine, can serve different purposes:
How do I create a class?
Like most things you create in your F5 device configurations, there are a few main ways you can create classes for your iRule. You can create them via the GUI, CLI, or in this case, via the iRule Editor as well.
How can I search through classes?
The two main ways to search through a class are with the matchclass and findclass commands. These commands have similar syntax and functionality, but accomplish different tasks.
when HTTP_REQUEST { if { matchclass [HTTP::uri] equals $::uri_list } { ... } }
when HTTP_REQUEST { if { matchclass [HTTP::uri] starts_with $::uri_list } { set myPool [findclass [HTTP::uri] starts_with $::uri_list " "] pool $myPool ... } }
Can I modify a class real-time with my iRule?
Technically, yes. Once the configuration is loaded into memory, you can technically modify a class with TCL's list commands. Doing so, however, not only converts the data in the class from an efficient, hashed format into a simple list format, thereby slowing down queries; but the changes made are also not permanent, as they cannot be written back to the file that stores the class data. This makes the changes effective only until TMM is restarted. In general, there is usually another way of structuring your code to avoid this that would be preferred.
Create a sample list (same concept as a class for testing matchclass)
set ::test_list [list {one} {two} {three}]
Log the matchclass output for the first element which starts with "t"
log local0. "matched element [matchclass $::test_list starts_with "t"]"
Output:
Rule : matched element 2
Also, matchclass needs to be wrapped in []'s to execute it. So this:
if { matchclass [HTTP::uri] equals $::uri_list } {
Should be:
if { [matchclass [HTTP::uri] equals $::uri_list] } {
--Aaron
matchclass item condition class
where "condition" can be one of... well, I don't know. Are these hard-coded to matchclass, or are they essentially pass-by-reference function names, in which case any dyadic TCL operator would work? There's no way to even guess from this documentation.
And the paragraphs that say things like "the matchclass command" are rather confusing... it should be "the matchclass example below..." or some such. Matchclass by itself is not tied to a specific condition.
In general, the info in these lessons is great, but some of the writing is C+. I hope these comments prove useful.
set myPool [findclass ]HTTP::uri[ starts_with $::uri_list " "]
is supposed to read:
set myPool [findclass [HTTP::uri] starts_with $::uri_list " "]
https://support.f5.com/kb/en-us/solutions/public/7000/700/sol7751.html
Colin
Are there non-global variable prefixes that are more appropriate?
http://devcentral.f5.com/wiki/iRules.CMPCompatibility.ashx
Aaron
Example 1)
matchclass [HTTP::uri] contains $::cache_list
can be changed to:
class match [HTTP::uri] contains cache_list
if { [HTTP::path] starts_with $::pusus_pass_uri } {
ASM::disable
}
can be changed to:
if { [class match [HTTP::path] starts_with pusus_pass_uri] } {
ASM::disable
}
v10 - data groups
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086448/iRule...
v11: iRules Data Group Updates
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086510/v11-i...
And the class command:
http://devcentral.f5.com/wiki/iRules.class.ashx
Aaron
Could you please help me on this?