Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dingtalk and aiops alert #3240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions elastalert/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,3 +2184,89 @@ def get_info(self):
'type': 'hivealerter',
'hive_host': self.rule.get('hive_connection', {}).get('hive_host', '')
}

class DingTalkAlerter(Alerter):

required_options = frozenset(['dingtalk_webhook', 'dingtalk_msgtype'])

def __init__(self, rule):
super(DingTalkAlerter, self).__init__(rule)
self.dingtalk_webhook_url = self.rule['dingtalk_webhook']
self.dingtalk_msgtype = self.rule.get('dingtalk_msgtype', 'text')
self.dingtalk_isAtAll = self.rule.get('dingtalk_isAtAll', False)
self.digtalk_title = self.rule.get('dingtalk_title', '')

def format_body(self, body):
return body.encode('utf8')

def alert(self, matches):
headers = {
"Content-Type": "application/json",
"Accept": "application/json;charset=utf-8"
}
body = self.create_alert_body(matches)
payload = {
"msgtype": self.dingtalk_msgtype,
"text": {
"content": body
},
"at": {
"isAtAll":False
}
}
try:
response = requests.post(self.dingtalk_webhook_url,
data=json.dumps(payload, cls=DateTimeEncoder),
headers=headers)
response.raise_for_status()
except RequestException as e:
raise EAException("Error request to Dingtalk: {0}".format(str(e)))

def get_info(self):
return {
"type": "dingtalk",
"dingtalk_webhook": self.dingtalk_webhook_url
}
pass

class AIOpsAlerter(Alerter):

required_options = frozenset(['aiops_webhook', 'aiops_appid'])

def __init__(self, rule):
super(AIOpsAlerter, self).__init__(rule)
self.aiops_webhook_url = self.rule['aiops_webhook']
self.aiops_app_key = self.rule['aiops_appid']
self.aiops_priority = self.rule.get('aiops_priority', 1)

def format_body(self, body):
return body.encode('utf8')

def alert(self, matches):
headers = {
"Content-Type": "application/json",
"Accept": "application/json;charset=utf-8"
}
body = self.create_alert_body(matches)
payload = {
"app": self.aiops_app_key,
"eventType": "trigger",
"eventId": "12345",
"alarmName": self.create_title(matches),
"alarmContent": body,
"priority": self.aiops_priority
}
try:
response = requests.post(self.aiops_webhook_url,
data=json.dumps(payload, cls=DateTimeEncoder),
headers=headers)
response.raise_for_status()
except RequestException as e:
raise EAException("Error request to aiops: {0}".format(str(e)))

def get_info(self):
return {
"type": "aiops",
"aiops_webhook": self.aiops_webhook_url
}
pass
4 changes: 3 additions & 1 deletion elastalert/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class RulesLoader(object):
'servicenow': alerts.ServiceNowAlerter,
'alerta': alerts.AlertaAlerter,
'post': alerts.HTTPPostAlerter,
'hivealerter': alerts.HiveAlerter
'hivealerter': alerts.HiveAlerter,
'dingtalk': alerts.DingTalkAlerter,
'aiops': alerts.AIOpsAlerter
}

# A partial ordering of alert types. Relative order will be preserved in the resulting alerts list
Expand Down