elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

ES cluster CPU gets maxed on any rule test

Open Dmitry1987 opened this issue 9 years ago • 17 comments

Hi, I have a question regarding performance:

when I try simple queries in graylog like "loglevel: ERROR" the results are immediately shown, but if I run the same in elastalert rule (either simple count min/max, or spike, percentage, etc' I tried almost all) the elasticsearch cluster (3 large machines, usually working at 20-30% CPU) gets maxed on CPU for long time and the rule never returns output.

I suspect this might be related to timestamps, because elastalert is unable to read my timestamp so I use graylog pipeline to duplicate+edit that field (I convert from '2017-06-13T14:43:00.201Z' to '2017-06-13 14:43:00', which solves 'parse errors')

so I use

timestamp_field: elastalert_ts

and this field doesn't exist prior to yesterday for example. (the moment i added this field creation from original timestamp on every message.)

Can this cause the issue with querying? or it's not the right direction to look for solution to high cpu during rule run?

thanks

Dmitry1987 avatar Jun 13 '17 15:06 Dmitry1987

How many documents are you matching? You can find this out using elastalert-test-rule --count-only rule.yaml

Did you try adding --verbose to elastalert to see output?

Finally, I'm not sure why you were getting timestamp parse errors, 2017-06-13T14:43:00.201Z is ISO8601 which is what elastalert uses by default, unless you just wrote that backwards.

Can I see what you rule yaml looks like?

Qmando avatar Jun 13 '17 18:06 Qmando

I tried --count-only now but today it seems anything I query just returns 0 and quickly, like it's a parallel universe since yesterday lol.

es_host: xxxxxxxxxxxx
es_port: 9200
timestamp_field: elastalert_ts
index: graylog_*

alert:
- "email"

email:
- "xxxxxxxx"
from_addr: "xxxxxxxx"

name: Percentage ERROR rule test
type: metric_aggregation

max_threshold: 10

buffer_time:
  hours: 5

metric_agg_key: level
metric_agg_type: value_count
query_key: environment
doc_type: message

filter:
- term:
    level.raw: "ERROR"

yesterday it was killing the cluster on 1st attempt :D and today quickly returns with

INFO:elastalert:Ran Percentage ERROR rule test from 2017-06-14 08:08 UTC to 2017-06-14 08:11 UTC: 0 query hits (0 already seen), 0 matches, 0 alerts sent

totally weird...

then I tried a frequency (trying both 'level.raw' and 'level'):

type: frequency
timeframe:
  hours: 5
num_events: 5
filter:
- term:
    level: "ERROR"

same result of 0.

regarding the timestamp field, when I set back to original one I get:

RequestError(400, u'search_phase_execution_exception', {u'status': 400, u'error': {u'failed_shards': [{u'node': u'eEyK6W4RQPKgAoZvHalcFw', u'index': u'graylog_8461', u'reason': {u'caused_by': {u'reason': u'Invalid format: "2017-06-13T08:21:57.511811Z" is malformed at "T08:21:57.511811Z"', u'type': u'illegal_argument_exception'}, u'reason': u'failed to parse date field [2017-06-13T08:21:57.511811Z] with format [yyyy-MM-dd HH:mm:ss.SSS]', u'type': u'parse_exception'}, u'shard': 0}]

UPDATE: But then I found I have another field of timestamp, without extra nanoseconds (the one I pasted here yesterday accidentally from grayog dashboard), and it works!. cool... I never noticed those fields are different, one with and one without nanoseconds.

Got 9377 hits from the last 1 day

omg it behaves totally different today with this new field of timestamp... I think it all works... Thanks! =)

Dmitry1987 avatar Jun 14 '17 08:06 Dmitry1987

omg out of nowhere in middle of testing another rule I got

'Expected numeric type on field [startTime], but got [string]')

(the startTime is the field with good timestamp that I found, and thought it saved my day) ....

so now, this type of rule doesn't cause issue with new startTime field:

type: frequency
timeframe:
  hours: 5
num_events: 5
filter:
- term:
    level: "ERROR"

but this rule says the "got string" and doesn't work o_O :

type: percentage_match
description: "5% Errors"
buffer_time:
  hours: 1
bucket_interval:
  minutes: 30
# include these fields in alert
include: ["environment", "environment_type"]
# send alert per this key variations
query_key: "environment"
max_percentage: 5
filter:
- query:
    query_string:
       query: "environment_type:production"
match_bucket_filter:
- term:
    level: "ERROR"

Dmitry1987 avatar Jun 14 '17 10:06 Dmitry1987

was able to work around this by pointing percentage_match rule to other timestamp field, so now the 'spike' rule works based on 1 time format field, and percentage_match works only using other field.

Dmitry1987 avatar Jun 15 '17 17:06 Dmitry1987

@Qmando @Dmitry1987

I have one quick questions

Whenever i start my elastalert rule it starts to queried timing from 2017-06-20 18:21 . How do i make it real-time monitoring ?

root@ubuntu:/tmp/elastalert# python -m elastalert.elastalert --verbose --rule siem.yaml
INFO:elastalert:Starting up
INFO:elastalert:Queried rule Port Scan Detection from **2017-06-20 18:21 IST** to 2017-06-20 18:38 IST: 7779 / 7779 hits
^CINFO:elastalert:SIGINT received, stopping ElastAlert...
root@ubuntu:/tmp/elastalert# vim siem.yaml
root@ubuntu:/tmp/elastalert# python -m elastalert.elastalert --verbose --rule siem.yaml
INFO:elastalert:Starting up
INFO:elastalert:Queried rule Port Scan Detection from **2017-06-20 18:21 IST** to 2017-06-20 18:39 IST: 7828 / 7828 hits
^CINFO:elastalert:SIGINT received, stopping ElastAlert...
root@ubuntu:/tmp/elastalert# vim siem.yaml

sathishdsgithub avatar Jun 20 '17 13:06 sathishdsgithub

Use buffer_time

Trustys avatar Jun 20 '17 13:06 Trustys

I have set my buffer_time to 30 minutes. What value should I put for real-time monitoring ?. Basically it should start querying with current system time .

sathishdsgithub avatar Jun 20 '17 13:06 sathishdsgithub

use --start to set start time and date. pass your needed time formatted date:time in this argument.

Dmitry1987 avatar Jun 20 '17 14:06 Dmitry1987

for example:

python ../elastalert/elastalert/elastalert.py --start "2017-06-20 1
2:00"  --es_debug_trace debug.log --verbose  --config ../config/elastalert_confi
g.yaml  --rule errors-percentage.yaml

that's my current string i test with :)

Dmitry1987 avatar Jun 20 '17 14:06 Dmitry1987

regarding "Basically it should start querying with current system time " - it will, but all those rules go "back" in time a little to fetch the data and then compare to something. So it's ok for it to test some past metrics and on next runs it will not go that much backwards.

like if you set "30" minutes it'll always go back 30m and calculate what's needed from the results.

for the 'real-time' you just need to let it run as a daemon.
the "schedule" of running the rules is in main config file:

run_every:
  minutes: 30

you can even make it "1 minute" so it'll be kinda real time :) (it will check your past 30 minutes of data, every 1 minute)

Dmitry1987 avatar Jun 20 '17 14:06 Dmitry1987

For every rule, elastalert will start querying from the last time that rule ran.

Either add --start NOW or change the name of your rule or add a query limit to config.yaml, the default is a week I think

old_query_limit:
  hours: 1

Qmando avatar Jun 20 '17 17:06 Qmando

@Qmando Thanks for the reply

  • Any idea why the query hits counts are increasing randomly? and what is the difference between query hits and (already seen ) ?

  • I want to set the frequency of alert email. Let's say if the event occurrence reaches 50 , one email alert should be sent

Below are my rule and elastalert rule output

type: cardinality

query_key: [src_ip, dst_ip]
cardinality_field: dst_port
max_cardinality: 50

buffer_time:
    minutes: 120

timeframe:

    hours: 5

old_query_limit:

  minutes: 30

filter:
- query:
    query_string:
        query: "_type: message"

num_events: 50

run_every:

   seconds: 10

realert:
   seconds: 0
alert:
 - "email"



root@ubuntu:/tmp/elastalert# python -m elastalert.elastalert  --start NOW --verbose --rule siem.yaml
INFO:elastalert:Starting up
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:20 IST: 0 / 0 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:20 IST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.979394 seconds
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:21 IST: 0 / 0 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:21 IST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.978744 seconds
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:22 IST: 25 / 25 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:22 IST: 25 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.980196 seconds
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:23 IST: 51 / 51 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:23 IST: 51 query hits (25 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.852374 seconds
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:24 IST: 76 / 76 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:24 IST: 76 query hits (51 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.956863 seconds
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:25 IST: 76 / 76 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:25 IST: 76 query hits (76 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.778128 seconds
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:26 IST: 100 / 100 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:26 IST: 100 query hits (76 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.958724 seconds
INFO:elastalert:Queried rule Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:27 IST: 125 / 125 hits
INFO:elastalert:Ran Port Scan Detection from 2017-06-20 23:20 IST to 2017-06-20 23:27 IST: 125 query hits (100 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 59.836441 seconds

sathishdsgithub avatar Jun 20 '17 18:06 sathishdsgithub

The queries overlap, the (already seen) number is the number of documents the previous query already processed. This is because you have 1 minute run_every (it uses whats in config.yaml, NOT your rule yaml) and 120 minute buffer time. You can probably just ignore this.

You have two conflicting settings, cardinality and frequency. You can only have one of either

type: frequency
num_events: 50

or

type: cardinality
cardinality_field: dst_port
max_cardinality: 50

If you want both, you need to create a second rule. It's currently ignoring num_events because you have type set to cardinality.

Qmando avatar Jun 20 '17 18:06 Qmando

@Qmando Thanks again :-). Appreciate your help

Now I managed to remove the overlap and it works as expected.

Are there any options in "alert: email" to consolidate the 50 events and send in below format in?

src_ip : IP_ADDRESS src_port: PORT , dst_ip:IP_ADDRESS dst_port:PORT

Currently, my email alert looks like the below

Port Scan Detection

A maximum of 50 unique dst_port(s) occurred since last alert or between 2017-06-20 18:53 IST and 2017-06-20 23:53 IST

IPV4: 192.168.96.141
MONTHDAY: 20
MONTHNUM: 06
SECOND: 52.673821
YEAR: 2017
_id: 7d244450-55e5-11e7-a486-000c298681bd
_index: graylog_0
_type: message
dst_ip: 192.168.96.141
dst_port: 12900
gl2_remote_ip: 192.168.96.141
gl2_remote_port: 39782
gl2_source_input: 59317ae66e332b0c4df4f831
gl2_source_node: 6e3e4964-4439-4cbb-8e49-0baf1a006532
message: 2017-06-20 23:51:52.673821 IP 192.168.96.1.2413 > 192.168.96.141.12900: Flags [S], seq 1411019395, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
num_hits: 262
num_matches: 24
payload: : Flags [S], seq 1411019395, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
source: 192.168.96.141
src_dst_ip: 192.168.96.1-192.168.96.141
src_ip: 192.168.96.1
src_ip,dst_ip: 192.168.96.1, 192.168.96.141
src_port: 2413
streams: [
    "000000000000000000000001"
]
timestamp: 2017-06-20T18:23:00.507Z

sathishdsgithub avatar Jun 20 '17 18:06 sathishdsgithub

If you use frequency, add attach_related: true. You can set include: [ip_address, src_port, dst_port] to make it a little more readable.

If you want to format them in exactly that way, you need to write an enhancement.

Qmando avatar Jun 20 '17 18:06 Qmando

Should I use include: [ip_address, src_port, dst_port] within alert: function as shown below ?

alert:
 - "email"
alert_text: "ElastAlert has detected suspicious activity for .At this time, an error occured. Do somes thing about it!"
alert_text_type: "alert_text_only"
attach_related: true
include: [ip_address, src_port, dst_port]

sathishdsgithub avatar Jun 20 '17 18:06 sathishdsgithub

@Qmando

I got the email in below format. Are there any options to display all 50 events in a single email?

_``` _A maximum of 50 unique dst_port(s) occurred since last alert or between 2017-06-20 19:34 IST and 2017-06-21 00:34 IST

_id: 4217c8e1-55eb-11e7-a486-000c298681bd _index: graylog_0 type: message dst_ip: 192.168.96.141 dst_port: 259 num_hits: 85 num_matches: 10 src_ip: 192.168.96.1 src_ip,dst_ip: 192.168.96.1, 192.168.96.141 src_port: 36334 timestamp: 2017-06-20T19:04:17.358Z

sathishdsgithub avatar Jun 20 '17 19:06 sathishdsgithub