elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

After use agreation and summary_table_fields config setting, the table is not formated

Open qhh0205 opened this issue 9 years ago • 17 comments

Hello everyone, After i use the agreation and summary_table_fields config options,the table is not formated, the result as following in email:

+----------+---------+---------+---------+---------+---------+---------+-------+ | host | request | status_ | remote_ | time_lo | num_hit | num_mat | count | | | | code | addr | cal | s | ches | | +==========+=========+=========+=========+=========+=========+=========+=======+ | stcharle | GET /pr | 404 | 66.249. | 14/Aug/ | 14 | 2 | 1 | | srealest | operty/ | | 79.48 | 2017:02 | | | | | atetoday | 1704140 | | | :50:47 | | | | | .chime.m | 9 HTTP/ | | | -0700 | | | | | e | 1.1 | | | | | | | +----------+---------+---------+---------+---------+---------+---------+-------+ | www.mill | GET | 404 | 157.55. | 14/Aug/ | 39 | 5 | 1 | | erteamre | /real- | | 39.240 | 2017:02 | | | | | altors.c | estate/ | | | :51:53 | | | | | om | florane | | | -0700 | | | | | | -garden | | | | | | | | | s-sub-i | | | | | | | | | nkster_ | | | | | | | | | inkster | | | | | | | | | / HTTP/ | | | | | | | | | 1.1 | | | | | | | +----------+---------+---------+---------+---------+---------+---------+-------+ | www.mill | GET /si | 404 | 52.32.8 | 14/Aug/ | 39 | 5 | 1 | | erteamre | testatu | | 5.119 | 2017:02 | | | | | altors.c | shidden | | | :51:33 | | | | | om | .aspx H | | | -0700 | | | | | | TTP/1.1 | | | | | | | +----------+---------+---------+---------+---------+---------+---------+-------+ | www.newm | POST | 404 | 109.92. | 14/Aug/ | 39 | 5 | 1 | | anhomesa | /wp-log | | 228.12 | 2017:02 | | | | | les.com | in.php | | | :51:37 | | | | | | HTTP/1. | | | -0700 | | | | | | 1 | | | | | | | +----------+---------+---------+---------+---------+---------+---------+-------+ | www.home | GET /Bi | 404 | 40.77.1 | 14/Aug/ | 14 | 2 | 1 | | salesins | ngSiteA | | 81.156 | 2017:02 | | | | | tcharles | uth.xml | | | :51:09 | | | | | .com | HTTP/1. | | | -0700 | | | | | | 1 | | | | | | | +----------+---------+---------+---------+---------+---------+---------+-------+ | stcharle | GET /pr | 404 | 66.249. | 14/Aug/ | 39 | 5 | 1 | | srealest | operty/ | | 79.51 | 2017:02 | | | | | atetoday | 1705537 | | | :52:02 | | | | | .chime.m | 6 HTTP/ | | | -0700 | | | | | e | 1.1 | | | | | | | +----------+---------+---------+---------+---------+---------+---------+-------+ | www.sava | POST /C | 404 | 52.87.7 | 14/Aug/ | 39 | 5 | 1 | | nnahandb | ontextW | | 9.151 | 2017:02 | | | | | eyond.co | ebHook. | | | :51:24 | | | | | m | ashx HT | | | -0700 | | | | | | TP/1.1 | | | | | | | +----------+---------+---------+---------+---------+---------+---------+-------+

So, how to get a recortar table? Thanks!

qhh0205 avatar Aug 14 '17 10:08 qhh0205

I have seen the source code, the table user Texttable. Mybe use PrettyTable is Ok?

qhh0205 avatar Aug 14 '17 11:08 qhh0205

@qianghaohao

Could you please share the complete yaml rule details ?

sathishdsgithub avatar Aug 14 '17 11:08 sathishdsgithub

@sathishdsgithub The following is my full rule file, thanks!

es_host: xxxxxx
es_port: 9200
name: nginx_exception_alert
type: frequency
index: chime*
num_events: 5
timeframe:
  minutes: 5
aggregation:
    minutes: 1
realert:
  minutes: 0
filter:
- term:
    status_code: 404
alert_text_type: exclude_fields
alert_text: |
    5分钟内nginx异常状态码个数超过阀值(>=5),触发报警
    主机: {}
    请求URL: {}
    HTTP状态码: {}
    远程IP地址: {}
    日志时间戳: {}
    总查询次数: {}
    总异常次数: {}
    原始日志信息: {}
alert_text_args:
- host
- request
- status_code
- remote_addr
- time_local
- num_hits
- num_matches
- message
summary_table_fields:
    - host
    - request
    - status_code
    - remote_addr
    - time_local
    - num_hits
    - num_matches
alert:
- "email"
email:
- "xxxxxxxx"
corp_id: "xxxxxxxxx"
secret: "xxxxxxxxxxxxx"
agent_id: 1000003
party_id: 2
user_id: "@all"
log_path: "xxxxx.log"
phone_number:
    - xxxx

qhh0205 avatar Aug 14 '17 11:08 qhh0205

@qianghaohao

If you need a table output please remove the below content from the code and use only the summary_table_fields:

alert_text_type: exclude_fields
alert_text: |
    5分钟内nginx异常状态码个数超过阀值(>=5),触发报警
    主机: {}
    请求URL: {}
    HTTP状态码: {}
    远程IP地址: {}
    日志时间戳: {}
    总查询次数: {}
    总异常次数: {}
    原始日志信息: {}
alert_text_args:
- host
- request
- status_code
- remote_addr
- time_local
- num_hits
- num_matches
- message

sathishdsgithub avatar Aug 14 '17 12:08 sathishdsgithub

@sathishdsgithub Yes you are right. But after i remove that, the table in email still not formated. This is bug?

qhh0205 avatar Aug 14 '17 12:08 qhh0205

@qianghaohao @Qmando

yes, it needs some improvement I guess. Even I also experienced the same issue. You will get a table but it won't be in proper format.

Please look through the below issue id for reference

https://github.com/Yelp/elastalert/issues/1230#issuecomment-316115740

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

sathishdsgithub avatar Aug 14 '17 12:08 sathishdsgithub

@qianghaohao

Let me know if you find the solutions?

sathishdsgithub avatar Aug 14 '17 13:08 sathishdsgithub

@sathishdsgithub Ok, in my idea, we can try to use PrettyTable module, that support the table html output. Mybe we can modify get_aggregation_summary_text(self, matches) function in alerts.py file.

qhh0205 avatar Aug 14 '17 13:08 qhh0205

@qianghaohao

Sounds good, can you test and let me know the output?

sathishdsgithub avatar Aug 14 '17 13:08 sathishdsgithub

Yes, I just test use prettytable, it can worked, but cat display in email. In email are html source code as following(Mybe i still need to modify the email alert code.):

<table>
    <tr>
        <td>www.savannahandbeyond.com</td>
        <td>POST /ContextWebHook.ashx HTTP/1.1</td>
        <td>404</td>
        <td>34.203.195.16</td>
        <td>14/Aug/2017:05:14:52 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
    <tr>
        <td>www.priorityregroup.com</td>
        <td>GET /wp-login.php HTTP/1.1</td>
        <td>404</td>
        <td>39.46.21.126</td>
        <td>14/Aug/2017:05:37:32 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
    <tr>
        <td>podrealestate.net</td>
        <td>POST /wp-login.php HTTP/1.1</td>
        <td>404</td>
        <td>182.70.82.110</td>
        <td>14/Aug/2017:05:22:30 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
    <tr>
        <td>www.savannahandbeyond.com</td>
        <td>POST /ContextWebHook.ashx HTTP/1.1</td>
        <td>404</td>
        <td>34.229.211.34</td>
        <td>14/Aug/2017:05:40:18 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
    <tr>
        <td>www.millerteamrealtors.com</td>
        <td>GET /real-estate/217051058/4114-w-carpenter-road-mount-morris-twp-mi-48504/ HTTP/1.1</td>
        <td>404</td>
        <td>66.249.79.139</td>
        <td>14/Aug/2017:05:21:46 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
    <tr>
        <td>www.logpondrealty.com</td>
        <td>HEAD /feed/ HTTP/1.1</td>
        <td>404</td>
        <td>35.187.96.204</td>
        <td>14/Aug/2017:05:25:46 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
    <tr>
        <td>www.rondavis.com</td>
        <td>GET /adminlogin/ HTTP/1.1</td>
        <td>404</td>
        <td>166.216.157.126</td>
        <td>14/Aug/2017:05:24:52 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
    <tr>
        <td>stcharlesrealestatetoday.chime.me</td>
        <td>GET /property/17058344 HTTP/1.1</td>
        <td>404</td>
        <td>40.77.167.95</td>
        <td>14/Aug/2017:05:13:43 -0700</td>
        <td>412</td>
        <td>82</td>
        <td>1</td>
    </tr>
</table>

The code i changed as followding(in alers.py get_aggregation_summary_text function):

def get_aggregation_summary_text(self, matches):
        text = ''
        if 'aggregation' in self.rule and 'summary_table_fields' in self.rule:
            summary_table_fields = self.rule['summary_table_fields']
            if not isinstance(summary_table_fields, list):
                summary_table_fields = [summary_table_fields]
            # Include a count aggregation so that we can see at a glance how many of each aggregation_key were encountered
            summary_table_fields_with_count = summary_table_fields + ['count']
            text += "Aggregation resulted in the following data for summary_table_fields ==> {0}:\n\n".format(
                summary_table_fields_with_count
            )
            text_table = PrettyTable()
            text_table._set_field_names(summary_table_fields_with_count)
            match_aggregation = {}

            # Maintain an aggregate count for each unique key encountered in the aggregation period
            for match in matches:
                key_tuple = tuple([unicode(lookup_es_key(match, key)) for key in summary_table_fields])
                if key_tuple not in match_aggregation:
                    match_aggregation[key_tuple] = 1
                else:
                    match_aggregation[key_tuple] = match_aggregation[key_tuple] + 1
            for keys, count in match_aggregation.iteritems():
                text_table.add_row([key for key in keys] + [count])
            text = text_table.get_html_string()
        return text

qhh0205 avatar Aug 14 '17 15:08 qhh0205

@sathishdsgithub Now that can work, I also modify the EmailAlert class, i forked the project and commit the code, you can have a try and review the code: https://github.com/qianghaohao/elastalert/commit/b2db43c6235fde54abe5d496b3dfc46d9cab126e

qhh0205 avatar Aug 14 '17 16:08 qhh0205

@qianghaohao

Good work !! let me try and get back to you. Thanks for your contribution :-)

sathishdsgithub avatar Aug 14 '17 16:08 sathishdsgithub

@qianghaohao

I tried and it worked with proper table output Thanks !!. But I would like to add some message in my email body on top of the table. As you can see the alert_text message is getting appended to the bottom of the table in my email output. Please refer the picture for reference.

Please let me know is there any options to add some message in my email body?. I'm using the type: cardinality


alert_text_type: alert_text_only

alert_text: |

                This is a test email

summary_table_fields:


    - sep-source-address

    - sep-destination-address

    - sep-destination-port

    - sep-username

summary-table

sathishdsgithub avatar Aug 14 '17 16:08 sathishdsgithub

@sathishdsgithub Hi, in my idea, mybe that need to modify the source code, or you can write a enhancement to change the content? I also have the same problem, i just want to get rid of the extra text, only reserve the table.

qhh0205 avatar Aug 15 '17 09:08 qhh0205

@qianghaohao @Qmando

My email output for the summary_table_field looks good if I use the email_msg as follows. However, my other yaml rule output is getting affected with improper email format output.

Are there any options to use different alerts.py for different rule ?

email_msg = MIMEText(body.encode('UTF-8'), 'html', _charset='UTF-8')

The above one is OK for summary_table_field output but not for other email output.

For Example, the output for the below one is getting affected.

alert_text_type: exclude_fields

alert_text: |

   This is a test email please ignore. ElastAlert has detected Windows Authentication failure. Please find the details below. Do some thing about it.

   User account name: {}

   Client IP address: {}

   Timestamp: {}

   Log Source: {}

   Domain Name: {}

   No of events: {}

   Event ID : {}

   Windows Authentication failure log: {}
alert_text_args:

  - account-name
  - client-address
  - timestamp
  - log-source
  - domain-name
  - num_matches
  - event-id
  - message

sathishdsgithub avatar Aug 15 '17 10:08 sathishdsgithub

@sathishdsgithub Yes, if you modify the EmailAlert code, all the EmailAlert will use html format as email content. So, you can write a new ElastAlert that only send the html format email and reserve the default EmailAlert in alerts.py. That means we can write a custom email Alert, dont modify the default EmailAlert. Then you can use in rule file with your own Alert. I has written a SMS Alert, you can reference:https://gitlab.com/snippets/1671230 All above is my advice.

qhh0205 avatar Aug 15 '17 11:08 qhh0205

@Qmando

Do we have any existing Elastalert module specific to summary_table_field ? I would like to import the PrettyTable and the below MIME type to use it to alert my YAML rule.

MIMEText(body.encode('UTF-8'), 'html', _charset='UTF-8')

sathishdsgithub avatar Aug 15 '17 13:08 sathishdsgithub