Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

Boolean operation for iRule

I have an iRule which makes a call to a REST API to raise an alert when there are no active members within the pool, ie pool monitor reports negative for all members. The problem is: I have a meta header in the "maintenance" page which refreshes every 30s to bring the service back up for the user and for this reason, the system sends multiple alerts. Can i have the code wrapped around a boolean operator that switches to false when the pool is 'empty', send ONE alert and then switches back to true when the service comes back up. Thanks in advance.

0
Rate this Question
Comments on this Question
Comment made 4 months ago by Patrik Jonsson 3235

If you post the iRule it'd be easier to assist.

/Patrik

1
Comment made 4 months ago by playfair039 54

The code block looks something like this Patrick. I just need something that wraps around it so that when one alert is raised when the pool is empty. Thanks in advance

if {[active_members [LB::server pool]] < 1 } {
    set conn [connect -timeout 100 -idle 30 -status conn_status x.x.x.x:x]
    set postdata "POST /whatever/whatever/ HTTP/1.1\r\n"
    append postdata "Content-Type: application/json;charset=UTF-8\r\n"
    append postdata "Accept: */*\r\n"
    append postdata "Host: x.x.x.x:x\r\n"
    append postdata "accept-encoding: gzip, deflate\r\n"
    append postdata "content-length: $jsonlen\r\n\r\n"
    set send_info [send -timeout 100 -status send_status $conn $postdata]
    set recv_2kbytes [recv -timeout 300 20 $conn]
    set length_recv [string length $recv_2kbytes]
    close $conn   
0

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

I might look into global variables:

https://devcentral.f5.com/articles/irules-101-03-variables

I've never had the need to use them myself so you might want to load test it, but maybe it could look something like this:

when RULE_INIT {

    set ::alertSent 0

}

when HTTP_REQUEST {
    if {[active_members [LB::server pool]] < 1 } {

        if { $::alertSent == 0 } {
            set conn [connect -timeout 100 -idle 30 -status conn_status x.x.x.x:x]
            set postdata "POST /whatever/whatever/ HTTP/1.1\r\n"
            append postdata "Content-Type: application/json;charset=UTF-8\r\n"
            append postdata "Accept: */*\r\n"
            append postdata "Host: x.x.x.x:x\r\n"
            append postdata "accept-encoding: gzip, deflate\r\n"
            append postdata "content-length: $jsonlen\r\n\r\n"
            set send_info [send -timeout 100 -status send_status $conn $postdata]
            set recv_2kbytes [recv -timeout 300 20 $conn]
            set length_recv [string length $recv_2kbytes]
            close $conn

            set ::alertSent 1
        }
    } else {
        set ::alertSent 1
    }
}
0
Comments on this Answer
Comment made 4 days ago by playfair039 54

The solution works perfectly well the issue is non-static global variables drops the virtual server from CMP which is not ideal for the application i'm running here - hence solution is not longer accepted. Is there an optimal solution that maintains these transient variables without dropping the vs from cmp. Thanks in advance

0
Comment made 3 days ago by Patrik Jonsson 3235

I'll revise then. :)

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Here's a revised rule using tables. Please note that you might want to change the key from "alertSent" to something more unique as the tables are global (in order to ensure maximum performance).

when HTTP_REQUEST {

    if {[active_members [LB::server pool]] < 1 } {

        if { [table lookup "alertSent"] == "" } {

            set conn [connect -timeout 100 -idle 30 -status conn_status x.x.x.x:x]
            set postdata "POST /whatever/whatever/ HTTP/1.1\r\n"
            append postdata "Content-Type: application/json;charset=UTF-8\r\n"
            append postdata "Accept: */*\r\n"
            append postdata "Host: x.x.x.x:x\r\n"
            append postdata "accept-encoding: gzip, deflate\r\n"
            append postdata "content-length: $jsonlen\r\n\r\n"
            set send_info [send -timeout 100 -status send_status $conn $postdata]
            set recv_2kbytes [recv -timeout 300 20 $conn]
            set length_recv [string length $recv_2kbytes]
            close $conn

            table set "alertSent" "1" indef indef

        }
    } else {
        table set "alertSent" "" indef indef
    }

}

Now, may I please have my answer back? :)

0