How to generate alarms based on hostname and partial match of message?
We get different type of events from different hostnames and we get the same type (partial match message events) from same host. We need to pass all events and aggregate based on hostname and partial match message.
Scenario1: HostA have 3 same type partial match message event and 1 different message type event. Expectation is how to get one alarm based on hostname and partial match events and one for different message type event. each event has “message” and “hostname” and different fields.
10:00 – event 1 having message “ping Failed” on HostA 10:01 – event 2 having message “availability ping is down” on HostA 10:02 – event 3 having message “ping is failed for the last two polls” on HostA 10:03 – event 4 having message “CPU is high” on HostA. 10:04—event 5 having message “high CPU utilization” on HostA
As the first three events are similar events on HostA, we need to suppress the 2 events for 10 min using relaert. My question is how to aggregate events based on hostname and partial event message(here it has ping) ? The last two events also have same partial match message(CPU) . How to generate one alarm for the last two events based on hotname and message?
If you want separate alerts for "CPU is High"/"high CPU" versus "ping Failed"/"ping is down", I think you need to make two separate alerts.
for example, if you did something like
filter:
- query_string:
query: "message: ping"
realert:
minutes: 10
query_key: hostname
And then the same but with message: CPU.
You'd get one alert with the first ping message, and one alert with the first cpu message, and both would only send 1 alert per host per 10 minutes.
Alternatively, if you use aggregation:
filter:
- query_string:
query: "message: ping"
realert:
minutes: 0
aggregation_key: hostname
aggregation:
minutes: 10
This will send the first three ping messages together, and the two cpu messages together, but it won't actually "suppress" anything. You might get 10 ping messages in 10 minutes all together.
If you want the first ping message and first cpu message but not the 2 other pings and 1 other cpu, that's not possible, without writing some custom enhancement (python code). Or, you could explicitly filter out all of the other message types, like message: ping AND NOT "availability ping" AND NOT "ping is failed" AND NOT ....
Thanks for your replay.
I need to send first come event only based on partial match message event on same host for last 10 min.
Do I need to use two rule.yaml files for generating two alerts?
one file is with
filter:
- query_string:
query: "message: ping"
realert:
minutes: 10
query_key: hostname
and other file is with
filter:
- query_string:
query: "message: ping"
realert:
minutes: 10
query_key: hostname
and run elastalert parllely with two files?
Yeah, that's right. My examples are not complete, they are missing name, index, and alert. But if you put them both into the rules folder, elastalert will run both rules.
I have created enhancements file tried to implement to have record message is similar message for Ping partial mathced events. the rule is running but event message is not getting aggreagated.
structure is:
elastalert
-rules(folder)
--test7.yaml (rule file)
-elastalert_modules (folder)
- __init__.py
-message_enhancement.py
-config.yaml (file)
Test7.yaml has
match_enhancements:
- "elastalert_modules.message_enhancement.messageEnhancements"
include : ["Hostname" , "event_message", "event_source", "event_sev", "local_time"]
query_key : ["Hostname", "event_message"]
realert :
minutes : 10
filter:
- query:
match_all : {}
alert:
- email
from_addr: "<email>"
email: "<email>"
alert_subject: "{0} :: {1} for host {2} at {3}"
alert_subject_args:
- event_sev
- event_message
- Hostname
- local_time
message_enhancement.py
from elastalert.enhancements import BaseEnhancement
class messageEnhancements(BaseEnhancement):
def process(self, match):
if ("ping" in match["event_message"]) or ("Ping" in match["event_message"]) or ("PING" in match["event_message"]) :
match["event_message"] = " Ping failed on host "
elif ("CPU" in match["event_message"]) or ("Cpu" in match["event_message"]) or ("cpu" in match["event_message"]) :
match["event_message"] = " CPU usage is high on host "
## Need to implement for other matched events for different type of events
Config.yaml is pointing to the rule files folder.
I am trying to put the “" Ping failed on host” message if the original event message has either ping or Ping or PING then do aggregate hostname and event_message. But it seems its changing the message for events which have message has ping or Ping or PING. But its not suppressing the events because query key is applying for original event message.
How to apply query_key(realert) on modified event_message
The enhancement runs after the realert is applied, so this strategy won't quite work. I think you need to either:
- Create two separate alerts
- Filter out some message events but not others, so that there's only one event_message for cpu and one for ping
- Change the enhancement into a custom rule type. If you did this, you could modify event_message before realert is applied.
- Put your enhancement code into Logstash, so that it's normalized before it gets into elasticsearch
- Create two separate alerts Skatakar: means do I need to create two rules and run elastalert for each data type. I have given smaple event types. We have different type event message lik message having “ping” or “CPU” or”Storage” or “memory” or “physical disk” or “log file”..etc Do I need to create rule yaml for each message type and do I need to create and run elastalert for each rule?
- Filter out some message events but not others, so that there's only one event_message for cpu and one for ping Skatakar: we don’t know which message event comes first. If we filter using exclude or not , we may skip the first one.
- Change the enhancement into a custom rule type. If you did this, you could modify event_message before realert is applied. Skatakar: I feel only this is the option for me to enhance. Can you please help me how to do this?
- Put your enhancement code into Logstash, so that it's normalized before it gets into elasticsearch Skatakar: We are using python(pandas) using read_csv method to load into elasticsearch.not sure how to do with logstash
For option 3, something like
from elastalert.ruletypes import RuleType
class MyRuleType(RuleType):
def add_data(self, data):
for datum in data:
if "ping" in datum["event_message"].lower():
datum["event_message"] = " Ping failed on host "
if 'cpu' in datum["event_message"].lower():
datum["event_message"] = " CPU usage is high on host "
...
self.add_match(datum)
Then in the rule
type: modules.message_enhancement.MyRuleType
ok thanks. I will try it tomorrow
Hi Quentin,
I have added given code in file and added type with path of filename and class name in rule file.
Now File has:
from elastalert.ruletypes import RuleType class MyRuleType(RuleType): def add_data(self, data): for datum in data: if "ping" in datum["event_message"].lower(): datum["event_message"] = " Ping failed on host " if 'cpu' in datum["event_message"].lower(): datum["event_message"] = " CPU usage is high on host " self.add_match(datum)
In rule file :
… type : elastalert_modules.message_enhancement.MyRuleType query_key : ["Hostname", "event_message"] realert : minutes : 10
filter:
- query: match_all : {}
…..
Its changing the event message but its not aggregating based on message. I have inserted two records with same hostname and message for one record as “ping response is failed” and other record message as “Availability ping response failed”. As we have added code as if ping is present, it should change the message to “ping failed on host”. Its changing the message as expected but its not aggreagating.
Email content for two events triggered emails are as follows:
- First event email:
test7 rule
Hostname: lv00001 Hostname,event_message: lv00001, ping response is failed _id: 1 _index: test_elastalert1 _type: record event_message: Ping failed on host event_sev: Critical event_source: BPPM local_time: 2019-01-17T11:40:28.247000 num_hits: 1 num_matches: 1 time: 2019-01-17T17:40:28.247Z
- Second event email:
test7 rule
Hostname: lv00001 Hostname,event_message: lv00001, Availability ping response failed _id: 2 _index: test_elastalert1 _type: record event_message: Ping failed on host event_sev: Critical event_source: BPPM1 local_time: 2019-01-17T11:41:04.428000 num_hits: 2 num_matches: 1 time: 2019-01-17T17:41:04.428Z
Oh, I see the issue. When you have a query_key with 2 values, the compound field is created before the data is processed, so it doesn't get changed. You'll have to add a line to update the Hostname,event_message field. Note I've hardcoded it, you could also inspet the rule's query_key value to generate it dynamically.
if 'cpu' in datum["event_message"].lower():
datum["event_message"] = " CPU usage is high on host "
+ datum['Hostname,event_message'] = ','.join([datum['Hostname'], datum['event_message'])
self.add_match(datum)
closed list bracket is missed in the above code. updated code and now it seems its working now, I will test with different scenarios datum['Hostname,event_message'] = ','.join([datum['Hostname'], datum['event_message']])
Hi @Qmando ,
I have edited custom rule as follows to assign more fields for the custom records. but its not working
from elastalert.ruletypes import RuleType
class MyRuleType(RuleType):
def add_data(self, data):
for datum in data:
try:
print datum["Classification"]
if "vom" in datum["Classification"]:
datum["Hostname"] = datum["Source"]
datum["event_message"] = datum["Message"]
datum["event_sev"] = datum["Severity"]
except :
print "Classification is not present"
if "ping" in datum["event_message"].lower():
datum["event_message"] = " Ping failed on host "
if 'cpu' in datum["event_message"].lower():
datum["event_message"] = " CPU usage is high on host "
datum['Hostname,event_message'] = ','.join([datum['Hostname'], datum['event_message']])
self.add_match(datum)
My sample record as "_index": "test_elastalert1", "_type": "record", "_id": "1", "_score": 1, "_source": { "Source": "XXXXX. ", "Rule Name": "XXXX-Server Risks ", "Alert Topic": "event.alert.vom.host.cpu.utilization.risk", "Severity": "Warning", "Classification": "rrr", "time": "2019-02-04T16:21:18.927000Z", "Message": "CPU utilization on host XXXX violated threshold [> 98]", "local_time": "2019-02-04T10:21:18.927000", "Description": "CPU utilization violated threshold [> 98]"
Eventhough record has Classification, its printing classification is not present in the console. Can you please look into it and let me know.
INFO:elastalert:Sleeping for 59.976 seconds INFO:elastalert:Queried rule Monitoring rule from 2019-02-04 10:12 Central Standard Time to 2019-02-04 10:22 Central Standard Time: 1 / 1 hits vom Classification is not present WARNING:elasticsearch:POST http://localhost:9200/elastalert_status_silence/silence [status:403 request:0.004s] ERROR:root:Error writing alert info to Elasticsearch: AuthorizationException(403, u'cluster_block_exception', u'blocked by: [FORBIDDEN/12/index read-only /
I have included classification field in the rule file. include : ["Hostname" , "event_message", "event_source", "event_sev", "local_time", "_id", "Classification", "Source", "Message"]