Forum Discussion

James_Thomson's avatar
Jul 25, 2005

Question about No Nodes Available Rule

I recently posted this rule to emulate the 4.X feature that logged No Nodes available.


when LB_FAILED {
    if { [LB::server addr] eq "" } {
       log "No nodes available - pool [LB::server]"
    }
}

Can anyone think of a way to make it so it does not log every single time a connection hits? Maybe only every 50 times or something. I guess I just need to figure out how to set a variable, increment by 1 every connection and then only log when it equals 50.

Any easier way to do this that you can think of?

2 Replies

  • You could use a global variable and increment it's value for each request. When it reaches your threashold then log your message and reset it to zero.

     

     

    You could also use the modulus ( % ) operator but unRuleY mentioned previously that it was very expensive so I provided the example using the == operator.

     

     

    when RULE_INIT {
       Initialize the counter to 0.
      set ::log_ctr 0
    }
    when HTTP_REQUEST {
       Increment the counter
      incr ::log_ctr
       If counter has reached 50, log the message and reset counter
      if { $::log_ctr == 50 } {
        log "Rule has been called $::log_ctr times"
        set ::log_ctr 0
      }
    }

     

     

    In this code, I'm using the default global namespace. You could define your own namespace if you wanted to. This would be useful if you are using global variables in different rules and want to somehow distinguish them.

     

     

    set my_ns::log_ctr 0

     

     

    -Joe
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    Improved version -

    To take Joe's example one step further, you could use an array to count the errors per pool and only report the error every 50 times within each pool that fails (as opposed to every 50 times any pool fails).

    
    when RULE_INIT {
        Initialize the counter to 0.
       array set ::log_ctr { }
    }
    when LB_FAILED {
       if { [LB::server addr] eq "" } {
          set pool [LB::server]
          if { not [info exists ::log_ctr($pool)] or $::log_ctr($pool) >= 50 } {
             log "No nodes available - pool $pool"
             set ::log_ctr($pool) 0
          } else {
             incr ::log_ctr($pool)
          }
       }
    }