elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

When reloading elastalert, flatline rule query on previous timestamp

Open Trustys opened this issue 9 years ago • 3 comments

Hello everybody,

I have a strange issue with a flatline rule. When I reload ElastAlert, the flatline rule query previous timestamp until it reach the present time.

For example, this morning (2017-06-12 around 10:00 AM), I reload the rule.

Here is the output :

`lastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:18 CEST to 2017-06-11 14:19 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:19 CEST to 2017-06-11 14:20 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:20 CEST to 2017-06-11 14:21 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:21 CEST to 2017-06-11 14:22 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:22 CEST to 2017-06-11 14:23 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:23 CEST to 2017-06-11 14:24 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:24 CEST to 2017-06-11 14:25 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:25 CEST to 2017-06-11 14:26 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:26 CEST to 2017-06-11 14:27 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:27 CEST to 2017-06-11 14:28 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:28 CEST to 2017-06-11 14:29 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:29 CEST to 2017-06-11 14:30 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:30 CEST to 2017-06-11 14:31 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:31 CEST to 2017-06-11 14:32 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:32 CEST to 2017-06-11 14:33 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:33 CEST to 2017-06-11 14:34 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:34 CEST to 2017-06-11 14:35 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:35 CEST to 2017-06-11 14:36 CEST: 6 buckets
elastalert_1     | INFO:elastalert:Queried rule ##Network_Log_Collection_Stopped from 2017-06-11 14:36 CEST to 2017-06-11 14:37 CEST: 6 buckets

As you can see, the rule stuck in previous timestamp (the previous day in this example) until it reach the present time.

The rule is never ran, there is only "Queried rule" (until the present time is reached, when present time is reached, the rule goes back normal)

We learn with the debug output that the rule query on two days (check the last line) :


elastalert_1     | ERROR:root:Traceback (most recent call last):
elastalert_1     |   File "/opt/elastalert/elastalert/elastalert.py", line 1072, in alert
elastalert_1     |     return self.send_alert(matches, rule, alert_time=alert_time)
elastalert_1     |   File "/opt/elastalert/elastalert/elastalert.py", line 1149, in send_alert
elastalert_1     |     alert.alert(matches)
elastalert_1     |   File "elastalert_modules/firalerter.py", line 137, in alert
elastalert_1     |     "subject":          self.create_title(matches),
elastalert_1     |   File "elastalert/alerts.py", line 211, in create_title
elastalert_1     |     return self.create_custom_title(matches)
elastalert_1     |   File "elastalert/alerts.py", line 232, in create_custom_title
elastalert_1     |     return alert_subject.format(*alert_subject_values)
elastalert_1     | KeyError: u'key'
elastalert_1     | 
elastalert_1     | ERROR:root:Uncaught exception running rule ##Network_Log_Collection_Stopped: u'key'
elastalert_1     | INFO:elastalert:Rule ##Network_Log_Collection_Stopped disabled


elastalert_1     | INFO:elastalert:Ignoring match for silenced rule ##Network_Log_Collection_Stopped
elastalert_1     | INFO:elastalert:Ignoring match for silenced rule ##Network_Log_Collection_Stopped
elastalert_1     | INFO:elastalert:Ignoring match for silenced rule ##Network_Log_Collection_Stopped
elastalert_1     | INFO:elastalert:Ignoring match for silenced rule ##Network_Log_Collection_Stopped
elastalert_1     | INFO:elastalert:Ignoring match for silenced rule ##Network_Log_Collection_Stopped
elastalert_1     | INFO:elastalert:Ignoring match for silenced rule ##Network_Log_Collection_Stopped
elastalert_1     | INFO:elastalert:Ran ##Network_Log_Collection_Stopped from 2017-06-10 08:32 CEST to 2017-06-12 09:37 CEST: 17674 query hits (0 already seen), 1897 matches, 0 alerts sent
elastalert_1     | WARNING:root:Querying from 2017-06-10 08:32 CEST to 2017-06-12 09:37 CEST took longer than 0:01:00!

Also, the debug output show an error (KeyError: u'key') but in fact the rule is OK and works finally

Here is the rule:

name: "##Network_Log_Collection_Stopped"
index: graylog_*

type: flatline

query_key: event_src

use_terms_query: true

doc_type:  message

threshold: 1

timeframe:
  minutes: 30

filter:
- query:
    query_string:
      query: 'is_network_device:true AND NOT tags:_jsonparsefailure'

realert:
    hours: 8`

Here is config.yaml:

rules_folder: /opt/rules

run_every:
  minutes: 1

buffer_time:
  minutes: 45

es_host: elasticsearch

es_port: 9200

writeback_index: elastalert_status

alert_time_limit:
  days: 2

I don't understand this behavior, anybody has the same issue ?

Trustys avatar Jun 12 '17 08:06 Trustys

Since we can't use buffer_time for use_terms_query, I don't see how I can avoid this behavior ? Or maybe this is a normal behavior ?

Trustys avatar Jun 14 '17 12:06 Trustys

This is standard behavior. If a rule is disabled or elastalert stops, when it starts again, it will pick up exactly where it left off. This is so you don't miss any alerts. Obviously this makes a little more sense for a rule that alerts on documents rather than a flatline alert..

However, you can set a limit to this using old_query_limit in config.yaml. The default is 1 week. If you set it to 1 hour, for example, when you start elastalert, it will only query up to 1 hour back at most.

http://elastalert.readthedocs.io/en/latest/elastalert.html?highlight=old_query_limit#configuration

Qmando avatar Jun 15 '17 17:06 Qmando

OK I got it. Thanks for your help @Qmando !

Trustys avatar Jun 16 '17 07:06 Trustys