mirror of
https://github.com/alerta/alerta-contrib.git
synced 2025-03-16 05:23:28 +00:00
Add notifications feature for more customized options for mails.
This commit is contained in:
parent
98316560ae
commit
78e0cb43d5
3 changed files with 48 additions and 5 deletions
|
@ -52,11 +52,30 @@ skip_mta = False
|
|||
email_type = text
|
||||
```
|
||||
|
||||
Notifications by different groups of server can be enabled using the section
|
||||
called ``[notifications]`` see the example above.
|
||||
|
||||
```
|
||||
[notifications]
|
||||
rules = notification1, notification2
|
||||
|
||||
[notification1]
|
||||
field = alert.resource
|
||||
regex = db-\w+
|
||||
contacts = dba@lists.mycompany.com, dev@lists.mycompany.com
|
||||
|
||||
[notification2]
|
||||
field = alert.resource
|
||||
regex = web-\w+
|
||||
contacts = dev@lists.mycompany.com
|
||||
```
|
||||
|
||||
Environment Variables
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
``SMTP_PASSWORD`` - can be used instead of smtp_password in the configuration file.
|
||||
|
||||
|
||||
Email Format
|
||||
~~~~~~~~~~~~
|
||||
|
||||
|
@ -68,10 +87,6 @@ The variable email_type can have 2 possible values:
|
|||
etc)
|
||||
- text: for just plain text emails
|
||||
|
||||
```
|
||||
{{ alert.severity|title }}
|
||||
```
|
||||
|
||||
Deployment
|
||||
----------
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
import datetime
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import smtplib
|
||||
import socket
|
||||
import sys
|
||||
|
@ -212,6 +213,16 @@ class MailSender(threading.Thread):
|
|||
else:
|
||||
html = None
|
||||
|
||||
if 'group_rules' in OPTIONS:
|
||||
for rules in OPTIONS['group_rules']:
|
||||
contacts = []
|
||||
if re.match(rules['regex'], eval(rules['field'])):
|
||||
# check for the contacts with OPTIONS['mail_to']
|
||||
contacts = [x.strip() for x in rules['contacts'].split(',')
|
||||
if x.strip() not in OPTIONS['mail_to']]
|
||||
if len(contacts) > 0:
|
||||
OPTIONS['mail_to'].extend(contacts)
|
||||
|
||||
msg = MIMEMultipart('alternative')
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = OPTIONS['mail_from']
|
||||
|
@ -284,6 +295,20 @@ class MailSender(threading.Thread):
|
|||
mx.close()
|
||||
|
||||
|
||||
def parse_group_rules(config):
|
||||
|
||||
notifications = [x.strip() for x in
|
||||
config.get('notifications', 'rules').split(',')]
|
||||
rules = []
|
||||
for notification in notifications:
|
||||
regex = config.get(notification, 'regex')
|
||||
contacts = config.get(notification, 'contacts')
|
||||
field = config.get(notification, 'field')
|
||||
rules.append({'regex': regex, 'contacts': contacts, 'field': field})
|
||||
|
||||
return rules
|
||||
|
||||
|
||||
def main():
|
||||
global OPTIONS
|
||||
|
||||
|
@ -323,6 +348,9 @@ def main():
|
|||
if os.environ.get('DEBUG'):
|
||||
OPTIONS['debug'] = True
|
||||
|
||||
if config.has_section('notifications'):
|
||||
OPTIONS['group_rules'] = parse_group_rules(config)
|
||||
|
||||
try:
|
||||
mailer = MailSender()
|
||||
mailer.start()
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import setuptools
|
||||
|
||||
version = '3.1.0'
|
||||
version = '3.2.0'
|
||||
|
||||
setuptools.setup(
|
||||
name="alerta-mailer",
|
||||
|
|
Loading…
Reference in a new issue