Forum Discussion
Jul 20, 2022
I wrote this one a while ago (there is room for improvement with the array access):
when RULE_INIT {
array set static::srvid_node {
node1 172.29.32.160
node2 172.29.32.161
node3 172.29.32.162
node4 172.29.32.163
}
set static::debug_map_srvid 1
}
when HTTP_REQUEST {
set original_request "[IP::client_addr]:[HTTP::method]:[HTTP::uri]:HTTP/[HTTP::version]"
if {([HTTP::cookie exists JSESSIONID])} {
set session_id [HTTP::cookie value JSESSIONID]
set node_id [getfield ${session_id} . 2]
set node_ip [getfield [array get static::srvid_node ${node_id}] " " 2]
if {(${node_ip} ne "")} {
if {(${static::debug_map_srvid} > 0)} { log local0. "info: valid header-cookie JSESSIONID found (${session_id}) in request (${original_request}); mapping to ${node_ip}" }
} else {
log local0. "error: broken header-cookie JSESSIONID found (${session_id}) in request (${original_request}); balancing to default pool ([LB::server pool])"
pool [LB::server pool]
return
}
} elseif {([findstr [HTTP::path] JSESSIONID 11] ne "")} {
set session_id [findstr [HTTP::path] JSESSIONID 11]
set node_id [getfield ${session_id} . 2]
set node_ip [getfield [array get static::srvid_node ${node_id}] " " 2]
if {(${node_ip} ne "") and (${static::debug_map_srvid} > 0)} {
log local0. "info: valid url-cookie JSESSIONID found (${session_id}) in request (${original_request}); mapping to ${node_ip}"
} else {
log local0. "error: broken url-cookie JSESSIONID found (${session_id}) in request (${original_request}); balancing to default pool ([LB::server pool])"
pool [LB::server pool]
return
}
} else {
if {(${static::debug_map_srvid} > 0)} { log local0. "info: no addressed node found in request (${original_request}); balancing to default pool ([LB::server pool])" }
pool [LB::server pool]
return
}
if {([LB::status pool [LB::server pool] member ${node_ip} 80] equals "down")} {
log local0. "warning: addressed node (${node_ip}) in request (${original_request}) is unavailable; sending 503"
HTTP::respond 503 noserver Server "test service" Connection Close Date "[clock format [clock seconds] -format {%a, %b %d %Y %H:%M:%S %Z} -gmt 1]"
return
} else {
pool [LB::server pool] member ${node_ip}:80
return
}
}
when HTTP_RESPONSE {
if {([HTTP::cookie exists JSESSIONID]) and ($static::debug_map_srvid > 0) and ([info exists original_request])} {
log local0. "info: response cookie JSESSIONID ([HTTP::cookie value JSESSIONID]) found in response for request (${original_request})"
}
}
when LB_FAILED {
# Ausloesung des Events bei Nichtverfuegbarkeit innerhalb Monitoring Timeout
if {${static::debug_map_srvid} > 0} {
log local0. "error: selected server not available (${node_ip}:80) for request (${original_request}); sending 503"
}
HTTP::respond 503 noserver Server "test service" Connection Close Date "[clock format [clock seconds] -format {%a, %b %d %Y %H:%M:%S %Z} -gmt 1]"
return
}