when RULE_INIT {
# Define a global variable which references the WA-enabled VIP name
set ::targetvip "back_vip"
}
when HTTP_REQUEST {
virtual $::targetvip
}
Tag Archives: irule
[irule]LB_SELECTED下的command:LB::server
LB::server
LB::server name
返回如下
| 20:26 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: baogong 10.7.20.240 80 |
—————
LB::server addr 返回如下
| 20:27 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 10.7.20.240 |
LB::server pool 返回如下
| 20:31 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: baogong |
LB::server port 返回如下
| 20:32 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 80 |
LB::server priority
| 20:33 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 1 |
LB::server priority
- Returns the priority of the currently selected pool member. If no server was selected (all servers down), returns null. If priority is not configured for the pool member, the default priority value of 1 is returned.
LB::server ratio
- Returns the ratio value of the currently selected pool member. If no server was selected (all servers down), returns null. If ratio is not configured for the pool member, the default ratio value of 1 is returned.
LB::server weight
- Returns the weighting of the currently selected pool member. If no server was selected (all servers down), returns null.
LB::server ripeness
- Returns the ripeness information of the currently selected pool member. If no server was selected (all servers down), returns null.
-
20:34 192.168.162.254 warnings tmm tmm[1045]: Rule test_length: 0
LTM,interface failsafe!
http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=166
利用外部脚本,探测物理端口状态或者trunk里的活动端口最小数量,然后进行failover。
脚本检索 b interface show输出中的status关键词来判断
9.3.0以上有通过~
———–
从这个例子中看,难道F5的gateway 的failsafe检测是依靠monitor的 ?不是failsafe自己对通信的检测?
2个连接数控制的irule
rule HTTP_session_limit { when RULE_INIT { set ::total_active_clients 0 set ::max_active_clients 100 log local0. "rule session_limit initialized: total/max: $::total_active_clients/$::max_active_clients" } when HTTP_REQUEST { ;# test cookie presence if {[HTTP::cookie exists "ClientID"]} { set need_cookie 0 set client_id [HTTP::cookie "ClientID"] ;# if cookie not present & connection limit not reached, set up client_id } else { if {$::total_active_clients < $::max_active_clients} { set need_cookie 1 set client_id [format "%08d" [expr { int(100000000 * rand()) }]] incr ::total_active_clients ;# otherwise redirect } else { HTTP::redirect "http://sorry.domain.com/" return } } } when HTTP_RESPONSE { ;# insert cookie if needed if {$need_cookie == 1} { HTTP::cookie insert name "ClientID" value $client_id path "/" } } when CLIENT_CLOSED { ;# decrement current connection counter for this client_id if {$::total_active_clients > 0} { incr ::total_active_clients -1 } } }这个rule主要是限制http并发总量,一个连接产生的计数在访问结束时自动被清0,因而可用来计算并发。
当并发总数达到限制后,没有cookie的新建链接则无法正常访问。一次访问只产生一个cookie,只要浏览器不关闭
cookie就没有失效,对这个人而言他后续的点击不再累加到连接数里。看下面日志:
| 18:40 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0 |
| 18:40 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接0 |
| 18:39 | 192.168.162.254 | informational | 这是首次打开首页,最终是为0。上面黑色日志,是后来刷新页面的,不产生 计数。 tmm tmm[1045]: Rule test_length: 关闭了减少一个连接,当前0 |
| 18:39 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 |
| 18:39 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 |
| 18:39 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 |
| 18:39 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 |
| 18:39 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 |
| 18:39 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 |
| 18:39 | 192.168.162.254 | warnings | tmm tmm[1045]: Rule test_length: 有cookie:05290852,当前连接1 |
分析下面这个代码:
when RULE_INIT {
触发建立一个数组,irule是什么时候被触发??!第一次访问触发,后面再刷新浏览器会触发吗。重新访问会触发吗? array set ::active_clients { }}when CLIENT_ACCEPTED { set client_ip [IP::remote_addr] puts "starting client---------$client_ip"
判断,如果数组元素变量::active_clients($client_ip)存在(第一次访问的时候这个数组肯定不存在,没人给这个数组元素赋值) if { [info exists ::active_clients($client_ip)] } { puts "origin connection is ==== $::active_clients($client_ip)"
如果存在,判断这个数组元素变量,如果大于指定的值则拒绝,否则加+1 if {$::active_clients($client_ip) > 3 } { reject puts "client connection is reject" return } else { incr ::active_clients($client_ip) puts "bynow connection is ==== $::active_clients($client_ip)" } } else {
第一次,给数组元素赋值 puts "client connection is the first one" set ::active_clients($client_ip) 1 }}
在事件最后,这个总被触发This event is fired at the end of any client connection, regardless of protocolwhen CLIENT_CLOSED { puts "closing_________[IP::remote_addr]" if { [info exists ::active_clients($client_ip)] } { incr ::active_clients($client_ip) -1 if { $::active_clients($client_ip) <= 0 } { unset ::active_clients($client_ip) } }}
这段代码,是分析一
[irule]LB::status
LB::status
- Returns the status of the currently-selected node (after LB_SELECTED event only). Possible values are: up | down | session_enabled | session_disabled
LB::status node <IP address>
- Returns the status of the node with the specified IP address. Possible values are: up | down | session_enabled | session_disabled
LB::status pool <pool name> member <IP address> <port>
- Returns the status of the specified pool member. Possible values are: up | down | session_enabled | session_disabled
LB::status <up | down | session_enabled | session_disabled>
- Returns TRUE if the status of the currently-selected node matches the specified status argument.
LB::status node <address> <up | down | session_enabled | session_disabled>
- Returns TRUE if the status of the specified node matches the specified status argument.
LB::status pool <pool name> member <address> <port> <up | down | session_enabled | session_disabled>
- Returns TRUE if the status of the specified pool member matches the specified argument. (This syntax doesn’t seem to be working as expected in 9.2.3 at least… use alternate syntax in Example below)
tcl 操作
c o m p a r e 按照词典的排序方式进行比较,根据s t r i n g 1 小于、等于或大于s t r i n g 2 ,分别返回-1 、0 或
1 (类似于C 库函数s t r c m p )
f i r s t 返回在s t r i n g 2 中第一次出现s t r i n g 1 的位置如果s t r i n g 1 没有出现在s t r i n g 2 中,则返回-1
l a s t 返回在s t r i n g 2 中最后一次出现s t r i n g 1 的位置。如果s t r i n g 1
s t r i n g 命令的如下选项将把s t r i n g 2 解释为要从s t r i n g l 中删除的一列字符:
t r i m 从s t r i n g 1 中删除开头和结尾的出现在s t r i n g 2 中的字符
t r i m l e f t 从s t r i n g 1 中删除开头的出现在s t r i n g 2 中的字符
t r i m r i g h t 从s t r i n g 1 中删除结尾的出现在s t r i n g 2 中的字符
s t r i n g 命令的如下选项只利用s t r i n g 1 作为变元:
l e n g t h 返回s t r i n g 1 包含的字符数
t o l o w e r 返回s t r i n g 1 中的所有字符被转换为小写字符后的新字符串
t o u p p e r 返回s t r i n g 1 中的所有字符被转换为大写字符后的新字符串
%取余
/取整
[]命令置换会执行命令
{}的引用中一般只是替换变量 不做命令执行
""的引用中会执行命令
T C L 对花括号和空格的使用是十分苛刻的。在i f ,e l s e i f 或e l s e 语句中的开始的花括号必须要和i f ,e l s e i f 或e l s e 在同一行中。而括号外必须是有一个空格。且e l s e 或e l s e i f语句必须要在前一个if 或elseif 的后括号的同一行中。
s w i t c h
switch $x {
0 {set x 10;}
10 {incr x -1;}
100 {set x 50;}
}
在缺省情况下,只有对应于匹配值的代码被执行,但如果代码块被指定为一个减号( – ),那么s w i t c h 语句将进行“下放”,从而执行后面的代码块
在过程中可以使用的三个重要命令是r e t u r n 、g l o b a l 和c a t c h 。g l o b a l 命令用来为过程提供对全程变量的访问权;r e t u r n 命令用来从过程返回值;c a t c h 命令用来探测错误并返回失败值。