[class exists $redir_class]

Hello, all. I have an irule that needs to check if a class exists, then if it does, pull a record from it. If not, it should do nothing. in this case, the value contained in redir_class does not exist as a class name.

if { [class exists $redir_class] } {
        log local0. { [class match -value -- [HTTP::uri] starts_with $redir_class]}
      } else {
        log local0. { class $redir_class does not exist}
      }

I would expect to see " class x does not exist" in my logs, but instead i get this:

“TCL error: testirule - Could not find class x”

If i replace the [class match… with a simple text string to log, i get the expected behavior. Why would it try to process code in a block that doesn’t match?

Thanks,

David

Hi David,

It looks like you’re almost there. One thing that I think may be throwing you off is the logging messages. The curly braces around your log commands will prevent variable substitution and command execution. Here is the iRule and datagroup I used to test with.

ltm data-group internal /Common/test_vars {
    records {
        /var1 {
            data 4
        }
        /var2 {
            data 8
        }
    }
    type string
}

ltm rule /Common/test_vars_class_exists_2156869 {
when HTTP_REQUEST {
  set redir_class test_vars

  if { [class exists $redir_class] } {
    log local0. " [class match -value -- [HTTP::uri] starts_with $redir_class]"
  } else {
    log local0. " class $redir_class does not exist"
  }
}
}

Here are the results I got while testing various URIs:

http://test-http-vs/var1 => Nov 18 07:35:42 tmm info tmm[14987]: Rule /Common/test_vars_class_exists_2156869 :  4
http://test-http-vs/var2 => Nov 18 07:35:49 tmm info tmm[14987]: Rule /Common/test_vars_class_exists_2156869 :  8
http://test-http-vs/foo1 => Nov 18 07:35:55 tmm info tmm[14987]: Rule /Common/test_vars_class_exists_2156869 :

-George

With the exact same class and rule, what happens if you set redir_class to something other than “test_vars”? That’s the case I think he is trying to demonstrate.

I’m getting the expected behavior when $redir_class is undefined or the referenced class doesn’t exist on v11.0.0 (build 8037):

Nov 21 06:01:50 tmm err tmm[20592]: 01220001:3: TCL error: /Common/test_vars_class_exists_2156869  - can't read "redir_class": no such variable     while executing "class exists $redir_class"
Nov 21 06:02:14 tmm info tmm[20592]: Rule /Common/test_vars_class_exists_2156869 :  class ttt does not exist

This sounds like it could be a problem with the version of LTM you are running. What version are you testing on?

-George

mine is 10.2.3.

[root@ve1023:Active] config  b version|grep -iA 1 version
BIG-IP Version 10.2.3 112.0
Final Edition
[root@ve1023:Active] config  b virtual bar list
virtual bar {
   snat automap
   pool foo
   destination 172.28.19.79:80
   ip protocol 6
   rules myrule
   profiles {
      http {}
      tcp {}
   }
}
[root@ve1023:Active] config  b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set redir_class test_vars
        if {[class exists $redir_class]}{
                log local0. "[class match -value -- [HTTP::uri] starts_with $redir_class]"
        } else {
                log local0. "class $redir_class does not exist"
        }
}
}

[root@ve1023:Active] config  curl -I http://172.28.19.79/var1
HTTP/1.1 404 Not Found
Date: Tue, 22 Nov 2011 11:35:21 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

[root@ve1023:Active] config  
Nov 22 03:35:07 local/tmm info tmm[24220]: Rule myrule : 4

[root@ve1023:Active] config  curl -I http://172.28.19.79/var2
HTTP/1.1 404 Not Found
Date: Tue, 22 Nov 2011 11:35:24 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

[root@ve1023:Active] config  
Nov 22 03:35:10 local/tmm info tmm[24220]: Rule myrule : 8


[root@ve1023:Active] config  b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set redir_class fake_test_vars
        if {[class exists $redir_class]}{
                log local0. "[class match -value -- [HTTP::uri] starts_with $redir_class]"
        } else {
                log local0. "class $redir_class does not exist"
        }
}
}

[root@ve1023:Active] config  curl -I http://172.28.19.79/var1
HTTP/1.1 404 Not Found
Date: Tue, 22 Nov 2011 11:35:58 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

[root@ve1023:Active] config  
Nov 22 03:35:43 local/tmm info tmm[24220]: Rule myrule : class fake_test_vars does not exist