elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

Min_cardinality not really working with 0 values

Open watollop opened this issue 10 years ago • 1 comments

Hi there, I have an issue regardin the cardinality rule when I try to use the min_cardinality

This is my rule:

type: cardinality
cardinality_field: "user.uuid"
min_cardinality: 50
timeframe:
  hours: 24
filter:
  query:
    query_string:
      query: "event.action:signup"

And this is my output:

INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 02:44 CEST to 2016-10-29 03:29 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 03:29 CEST to 2016-10-29 04:14 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 04:14 CEST to 2016-10-29 04:59 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 04:59 CEST to 2016-10-29 05:44 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 05:44 CEST to 2016-10-29 06:29 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 06:29 CEST to 2016-10-29 07:14 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 07:14 CEST to 2016-10-29 07:59 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 07:59 CEST to 2016-10-29 08:44 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 08:44 CEST to 2016-10-29 09:29 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 09:29 CEST to 2016-10-29 10:14 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 10:14 CEST to 2016-10-29 10:59 CEST: 0 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 10:59 CEST to 2016-10-29 11:44 CEST: 7 / 7 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 11:44 CEST to 2016-10-29 12:29 CEST: 26 / 19 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 12:29 CEST to 2016-10-29 13:14 CEST: 28 / 2 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 13:14 CEST to 2016-10-29 13:59 CEST: 30 / 2 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 13:59 CEST to 2016-10-29 14:44 CEST: 42 / 12 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 14:44 CEST to 2016-10-29 15:29 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 15:29 CEST to 2016-10-29 16:14 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 16:14 CEST to 2016-10-29 16:59 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 16:59 CEST to 2016-10-29 17:44 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 17:44 CEST to 2016-10-29 18:29 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 18:29 CEST to 2016-10-29 19:14 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 19:14 CEST to 2016-10-29 19:59 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 19:59 CEST to 2016-10-29 20:44 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 20:44 CEST to 2016-10-29 21:29 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 21:29 CEST to 2016-10-29 22:14 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 22:14 CEST to 2016-10-29 22:59 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 22:59 CEST to 2016-10-29 23:44 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-29 23:44 CEST to 2016-10-30 00:29 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-30 00:29 CEST to 2016-10-30 01:14 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-30 01:14 CEST to 2016-10-30 01:59 CEST: 42 / 0 hits
INFO:elastalert:Queried rule Example cardinality rule from 2016-10-30 01:59 CEST to 2016-10-30 02:44 CET: 42 / 0 hits

As you can see at the begging I don't have any hits but at the end I have 42<50 and the alert doesn't trigger.

I just read what is in the issue #536

There is an undocumented feature where it will not alert until it sees at least one event. The flatline type works the same way. This is to prevent alerts immediately upon startup and continuous alerts while below the threshold.This should definitely be controlled by an option and documented better, sorry about that.

I tried their solution:

diff --git a/elastalert/ruletypes.py b/elastalert/ruletypes.py
index d3cfa89..348ac05 100644
--- a/elastalert/ruletypes.py
+++ b/elastalert/ruletypes.py
@@ -763,6 +763,7 @@ class CardinalityRule(RuleType):

     def garbage_collect(self, timestamp):
         """ Remove all occurrence data that is beyond the timeframe away """
        self.first_event.setdefault('all', timestamp)
        self.cardinality_cache.setdefault('all', {})
         for qk, terms in self.cardinality_cache.items():
             for term, last_occurence in terms.items():

But is not working.

My ideal rule while be something like this:

cardinality_field: "user.uuid"
min_cardinality: 1

timeframe:
  hours: 24
  • Any idea what can I do for checking if I had at least 1 register in the last 24 Hours.

Thank you so much!

watollop avatar Oct 30 '16 01:10 watollop

This might be an edge case where 24 hours is not strictly less than 24 hours. (https://github.com/Yelp/elastalert/blob/master/elastalert/ruletypes.py#L785)

I think if you lowered the timeframe and ran this experiment again, you would see an alert. Or if you had let it continue running, instead of only running EXACTLY 24 hours.

I think the behavior you expect is reasonable, so I can change this to a >= instead of >.

Qmando avatar Oct 31 '16 16:10 Qmando