elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

Error failed to delete Alert - ElastAlert

Open sathishdsgithub opened this issue 9 years ago • 28 comments

@Qmando @Dmitry1987

I notice that whenever the alert is matched the email is sent based on the aggregation time. However, I notice few issues when the alert is sent.

1.) Alert is sent twice 2,) Error to delete the Alert.

I would like to know the email is sent twice because of the failed to delete alert? how to fix this ?

Please find the below error message for example.

INFO:elastalert:Queried rule SRX Port Scan Detection from 2017-06-23 10:30 IST to 2017-06-23 10:31 IST: 112 / 112 hits INFO:elastalert:Ran SRX Port Scan Detection from 2017-06-23 10:30 IST to 2017-06-23 10:31 IST: 112 query hits (112 already seen), 0 matches, 0 alerts sent INFO:elastalert:Sleeping for 4.975736 seconds INFO:elastalert:Sent email to ['[email protected]'] INFO:elastalert:Queried rule SRX Port Scan Detection from 2017-06-23 10:30 IST to 2017-06-23 10:31 IST: 112 / 112 hits INFO:elastalert:Ran SRX Port Scan Detection from 2017-06-23 10:30 IST to 2017-06-23 10:31 IST: 112 query hits (112 already seen), 0 matches, 1 alerts sent INFO:elastalert:Sent email to ['[email protected]'] WARNING:elasticsearch:DELETE http://192.168.96.141:9200/elastalert_status/elastalert/AVzTUrFFv_ErwPo1P0FB [status:404 request:0.050s] ERROR:root:Failed to delete alert AVzTUrFFv_ErwPo1P0FB at 2017-06-23T05:01:30.914766Z INFO:elastalert:Queried rule SRX Port Scan Detection from 2017-06-23 10:30 IST to 2017-06-23 10:31 IST: 112 / 112 hits INFO:elastalert:Ran SRX Port Scan Detection from 2017-06-23 10:30 IST to 2017-06-23 10:31 IST: 112 query hits (112 already seen), 0 matches, 1 alerts sent INFO:elastalert:Sleeping for 2.438398 seconds

sathishdsgithub avatar Jun 23 '17 05:06 sathishdsgithub

What is your run_every setting? I'm wondering if this is caused by a race condition in Elasticsearch.

  1. Pending alert is found, first alert gets sent
  2. Delete is issued for that pending alert
  3. Pending alert is found again because delete operation has not registered yet
  4. Delete is issued again and fails

I'd recommend setting it slightly higher, maybe to 15 seconds or something, and see if this problem persists.

Qmando avatar Jun 23 '17 20:06 Qmando

@Qmando, thanks for reply. I was able fix the error by extending time of run_every to 20 seconds. Which is the preferred time setting for run_every ?

. And what is the command to run multiple yaml rule parallelly? ..is there any option to put all the rules in a single yaml file and run it using python?

basically how we should setup the rule running automatically in background/startup in production environment ?

sathishdsgithub avatar Jun 24 '17 05:06 sathishdsgithub

Which is the preferred time setting for run_every

Whatever you want. 20 seconds is probably fine. A minute too. 5 minutes.

And what is the command to run multiple yaml rule parallelly?

You specify rules_folder in config.yaml and all the rules in that folder run.

There are lots of ways to get python to run in the background. Set it up as an init service, run it in docker, etc.

Qmando avatar Jun 26 '17 19:06 Qmando

@Qmando Thanks for reply.

One quick question, I have specified the following details for the email alert .

attach_related: true include: [message, srx-source-address, srx-source-port, srx-destination-address, srx-destination-port]

But my email alert gives additional details like __id , _type, index details, which i dont want to be displayed in email . How do I exclude these details in email alert ?. I have copied the email alert output below

__id: f4db5517-57ed-11e7-a486-000c298681bd _index: graylog_0 _type: message message: Apr 3 20:34:18 10.212.0.20 1 2017-04-03T20:34:17.602Z RT_FLOW - RT_FLOW_SESSION_DENY [junos@ source-address="10.10.10.10" source-port="56453" destination-address="170.10.10.10" destination-port="443" service-name="None" protocol-id="17" icmp-type="0" policy-name="default-deny" source-zone-name="inside" destination-zone-name="isp" application="UNKNOWN" nested-application="UNKNOWN" username="N/A" roles="N/A" packet-incoming-interface="xe-4/0/1.10" encrypted="UNKNOWN" reason="policy deny"] num_hits: 112 num_matches: 60 srx-destination-address: 170.200.72.4 srx-destination-port: 443 srx-source-address: 10.15.166.30 srx-source-address,srx-destination-address: 10.15.166.30, 170.200.72.4 srx-source-port: 56453 timestamp: 2017-06-23T08:28:38.486Z_

sathishdsgithub avatar Jun 26 '17 19:06 sathishdsgithub

You'll have to do a custom alert text

alert_text_type: exclude_fields
alert_text: |
    timestamp: {}
    message: {}
    srx-source-port: {}
    srx-destination-port: {}
    ...
alert_text_args:
 - timestamp
 - message
 - srx-source-port
 - srx-destination-port
 ...

Qmando avatar Jun 26 '17 21:06 Qmando

@Qmando

Basically this will display only the fields mentioned in alert_text_args ?

Let me try and update you the status.

sathishdsgithub avatar Jun 27 '17 03:06 sathishdsgithub

Hi @Qmando , I wonder how the rules execute when I have 20-30 heavy rules (I'm getting close to it, but for now ES cpu survives the querying load), do they run one after another and wait until previous rule completed? (I have a query_key, that makes 1 rule to be like 40 separate rules, per each customer environment). If I set a short "run_every", can it happen that not all rules yet completed from previous run and it will cause issues?

Dmitry1987 avatar Jun 27 '17 08:06 Dmitry1987

@sathishdsgithub regarding run in background, I used this docker https://github.com/krizsan/elastalert-docker but to run the new version, you'll need your own Dockerfile that builds elastalert from master branch here. I can share mine (based on Alpine), you run it in git cloned folder, and it wraps it nicely to image. And the only thing you'll need to supply, is mount your rules folder and config file into container when you run it (like "-v /opt/myrules:/opt/rules", and elastalert will pick up the rules and your config when runs).

Dmitry1987 avatar Jun 27 '17 08:06 Dmitry1987

@Dmitry1987 Which Alpine flavor best suitable for ElastAlert docker setup? https://alpinelinux.org/downloads/ I see many versions under downloads, also please share me the docker file

sathishdsgithub avatar Jun 27 '17 09:06 sathishdsgithub

I just used regular one with no tag :) probably it takes the :latest of it... disclaimer: this dockerfile is based heavily on https://github.com/krizsan/elastalert-docker :) so thanks to @krizsan

FROM alpine

# Install software required for Elastalert and NTP for time synchronization.
RUN apk update && \
    apk upgrade && \
    apk add ca-certificates openssl-dev openssl libffi-dev python2 python2-dev py2-pip py2-yaml gcc musl-dev tzdata openntpd 

RUN mkdir /opt
COPY . /opt/elastalert
COPY elastalert-config /opt/elastalert/config

WORKDIR /opt/elastalert

# Install Elastalert.
RUN python setup.py install && \
    pip install -e . && \
# Clean up.
    apk del python2-dev && \
    apk del musl-dev && \
    apk del gcc && \
    apk del openssl-dev && \
    apk del libffi-dev && \
    rm -rf /var/cache/apk/* 

WORKDIR /opt/elastalert/elastalert
ENTRYPOINT ["/usr/bin/python", "elastalert.py", "--verbose", "--config", "/opt/elastalert/config/elastalert_config.yaml"]

you run it right from this repo after git clone. but also need the "elastalert-config" folder with your config file there. you can skip this line, and instead always mount config in needed path. feel free to remove the --verbose too.

Dmitry1987 avatar Jun 28 '17 08:06 Dmitry1987

@Qmando @Dmitry1987

Is it possible to add graylog dashboard link in email alert body similar to " - kibana_link use_kibana4_dashboard: http://mykibana.com/"

Let's say I want to insert a link in the email alert. When the link is clicked it should redirect to the graylog dashboard with some query string encoded within the url

http://192.168.96.141:9000/search?rangetype=relative&fields=message%2Csource&width=1093&highlightMessage=&relative=7200&q=srx-destination-address%3A170.2.2.4+AND+srx-source-address%3A19.15.166.30

sathishdsgithub avatar Jun 28 '17 12:06 sathishdsgithub

@sathishdsgithub yes, you can create enhancement that will 'post process' all matches, like this example:

from enhancements import BaseEnhancement

class LinkAppender(BaseEnhancement):
    # This applies to all matches, a match is a dictionary, print it all to see what's available for use
    def process(self, match):
         graylog_link_params = 'your custom Graylog URL with params, use {0} {1}  to format'.format(match[something from the match dictionary])
		 # Now this is a new field that you'll see in email. 
		 # You can create any new custom fields, from any data which exists in 'self.rules' or 'match'.
		 # Print all those objects to see what you can use
         match['new_graylog_dashboard_link'] = '{0}{1}'.format(match['kibana_link'],kibana_link_params)

save it as filename "alert_enhancement_graylog.py" in same folder where all elastalert code is.

then use this in rule file:

use_kibana4_dashboard: "some_placeholder"
generate_kibana_link: true

match_enhancements:
- "alert_enhancement_graylog.LinkAppender"

regarding the enhancement: You'll need to find out the values you can use, and then create the needed formatted string. I do the same with Kibana and it works for me. I'm not sure at all if with Graylog it will be possible or not, but you can try. (my example is based on the kibana link formatter i use now, and it has like 200 lines because of plenty custom stuff that we need, i show only the very basic)

Most important for you is the match['kibana_link'] item that you will need to parse and extract the needed "time" range values (elastalert creates this kibana link for you with needed timestamps) after you get the timestamps, you can place them in graylog URL. Same with all the "match['filter']" objects, can extract and use in link.

Hope it helps :) good luck

Dmitry1987 avatar Jun 29 '17 08:06 Dmitry1987

@Dmitry1987 thanks for your valuable feedback. Let me try that and update you the status. You're doing a great work appreciate your help.

Meanwhile, I have a query regarding whitelist. Not sure if i understand the rule type correctly.

I have a portscan detection rule which is working as expected. Now I want to whitelist a specific IP , when the portscan is initiated from this IP the match should ignore this whitelisted IP. If I apply the below rule within my current portscan rule ..will it work?

whitelist: - 10.1.1.1 - 10.2.2.2

This means when I initiate a scan from 10.1.1.1 and 10 .2.2.2 I should see the query hits but not the matches.

sathishdsgithub avatar Jun 30 '17 19:06 sathishdsgithub

that is correct. With a whitelist, a match will be created for every entry that is not one of those values. So 10.1.1.1 or 10.2.2.2 will not trigger an alert, but any of IP will.

Qmando avatar Jun 30 '17 20:06 Qmando

@Qmando Thanks again !!!. Let me try and update you

sathishdsgithub avatar Jul 01 '17 04:07 sathishdsgithub

Not able to use two different "type" in the same rule.

I'm using " type: cardinality" ,also I would like to use the type: whitelist to exclude some matches.

My current rule not allowing me to add both the type cardinality and whitelist in same yaml rule file

How do i make use of both whitelist and cardinality in same rule file ?

I tried like the below , but it does not work. Please assist

type: cardinality

type: whitelist

sathishdsgithub avatar Jul 02 '17 17:07 sathishdsgithub

@Qmando @Dmitry1987

I'm using the below rule to whitelist few IP address and its working as expected. How do I achieve the same for subnet range **example /24, /16 , 192.168.1.1-192.168.1.100** etc ...Not sure if this can be achived with ElastAlert ?

filter:
  - not:
       terms:
         src_ip: ["10.10.10.10", "20.20.20.20", "30.30.30.30"]

sathishdsgithub avatar Jul 04 '17 12:07 sathishdsgithub

You can try to use wildcard, I'm not sure will work but in the documentation it was mentioned...

I tried didn't worked for me but maybe I missed something. You should use it with string_query and not term, worth a try.

On Jul 4, 2017 3:59 PM, "sathishdsgithub" [email protected] wrote:

@Qmando https://github.com/qmando @Dmitry1987 https://github.com/dmitry1987

I'm using the below rule to whitelist few IP address and its working as expected. How do I achieve the same for subnet range example /24, /16 , 192.168.1.1-192.168.1.100 etc ...Not sure if this can be achived with ElastAlert ?

filter:

  • not: terms: src_ip: ["10.10.10.10", "20.20.20.20", "30.30.30.30"]

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Yelp/elastalert/issues/1182#issuecomment-312871894, or mute the thread https://github.com/notifications/unsubscribe-auth/AI71-Nowet3XOnP5qrQrEF_NElMUN9o1ks5sKjdKgaJpZM4ODI9G .

Dmitry1987 avatar Jul 04 '17 21:07 Dmitry1987

@Dmitry1987 let me give a try, thanks again. Also, I found the below link for IP address range query .will try that also.

https://github.com/Yelp/elastalert/issues/744

I'm wondering if you have ever used elastalert for network Scan detection in your environment ? If so could please tell me what challenge you have faced ? If possible could please share some sample example rule ?

sathishdsgithub avatar Jul 05 '17 02:07 sathishdsgithub

@Qmando @Dmitry1987

Is there any option to negate the IP address range with query_string. I tried the below option and it's giving the error message

fields:

  • src_ip
filter:
- not:
  - query:
     query_string:
        query: src_ip:[192.168.1.100 TO 192.168.1.102]

ERROR

WARNING:elasticsearch:GET http://192.168.96.141:9200/graylog_0/_search?_source_include=src_ip%2Ctimestamp%2Csrc_ip%2Cdst_ip%2Cdst_port%2Cdst_ip%2Cmessage%2Cip_address&ignore_unavailable=true&scroll=30s&size=10000 [status:500 request:0.004s]
**ERROR:root:Error running query: TransportError(500, u'search_phase_execution_exception', None)**
INFO:elastalert:Ran Port Scan Detection from 2017-07-05 12:46 IST to 2017-07-05 12:46 IST: 0 query hits (0 already seen), 0 matches, 0 alerts sent

sathishdsgithub avatar Jul 05 '17 07:07 sathishdsgithub