Yelp / elastalert

Easy & Flexible Alerting With ElasticSearch
https://elastalert.readthedocs.org
Apache License 2.0
8k stars 1.73k forks source link

Custom alerter : '_thread._local' object has no attribute 'alerts_sent' #2546

Open Theoooooo opened 5 years ago

Theoooooo commented 5 years ago

Hello everyone,

i'm in a process of making a discord alerter (for which i submit a PR request many month ago) and i'm making a new pass on it to fix some issues with it and i'm running into an issue.

While testing a rule, my alert does get send into Discord but elastalert does no get the attribute 'alerts_send' (while it has been send correctly).

I've modified all the necessary files for my alerter to make it work but i can't pinpoint why it is not getting the desired alert_sent attribute.

My alerter :

class DiscordAlerter(Alerter):

    required_options = frozenset(['discord_webhook_url'])

    def __init__(self,rule):
        super(DiscordAlerter, self).__init__(rule)
        self.discord_webhook_url = self.rule['discord_webhook_url']
        self.discord_emoji_title = self.rule.get('discord_emoji_title', ':warning:')
        self.discord_http_proxy = self.rule.get('discord_http_proxy', None)
        self.discord_https_proxy = self.rule.get('discord_https_proxy', None)
        self.discord_proxy_login = self.rule.get('discord_proxy_login', None)
        self.discord_proxy_password = self.rule.get('discord_proxy_password', None)
        self.discord_embed_color = self.rule.get('discord_embed_color', 0xffffff)
        self.discord_embed_footer = self.rule.get('discord_embed_footer', None)
        self.discord_embed_icon_url = self.rule.get('discord_embed_icon_url', None)

    def alert(self, matches):
        body = ''
        title = u'%s' % (self.create_title(matches))
        for match in matches:
            body += str(BasicMatchString(self.rule, match))
            if len(matches) > 1:
                body += '\n----------------------------------------\n'
        if len(body) > 2047:
                body = body[0:1950] + '\n *message was cropped according to discord embed description limits!* '

        body += '```'

        proxies = {} if self.discord_http_proxy or self.discord_https_proxy else None
        if self.discord_http_proxy is not None: proxies['http'] = '%s' % (self.discord_http_proxy)
        if self.discord_https_proxy is not None: proxies['https'] = '%s' % (self.discord_http_proxys)
        auth = HTTPProxyAuth(self.discord_proxy_login, self.discord_proxy_password) if self.discord_proxy_login else None
        headers = {"Content-Type": "application/json"}

        data = {}
        data["content"] = "%s %s %s @everyone" % (self.discord_emoji_title, title, self.discord_emoji_title)
        data["embeds"] = []
        embed = {}
        embed["description"] = "%s" % (body)
        embed["color"] = (self.discord_embed_color)

        if self.discord_embed_footer:
                embed["footer"] = {}
                embed["footer"]["text"] = (self.discord_embed_footer) if self.discord_embed_footer else None
                embed["footer"]["icon_url"] = (self.discord_embed_icon_url) if self.discord_embed_icon_url else None
        else:
                None

        data["embeds"].append(embed)

        try:
                result = requests.post(self.discord_webhook_url, data=json.dumps(data), headers=headers, proxies=proxies)
        except RequestException as e:
                raise EAException("Error posting to Discord: %s. Details: %s" % (e, "" if e.response is None else e.response.text))

        elastalert_logger.info(
                "Alert sent to the webhook %s" % self.discord_webhook_url)

    def get_info(self):
            return {'type': 'discord',
                    'discord_webhook_url': self.discord_webhook_url}

If you have any idea why elastalert is not getting alerts_sent value because i can't pinpoint why :/

gcnote commented 4 years ago

add self.thread_data.alerts_sent = 0 in elastalert.py and it will works

daichi703n commented 4 years ago

@Theoooooo Do you have any question?