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 SMTPS notify #1556

Open
wants to merge 5 commits 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Notifications] New config option `notify_all_errors` supports all system errors, including loss of data connectivity - [#1546](https://github.com/jertel/elastalert2/pull/1546) - @jertel

## Other changes
- Add SNMPTS to notify email - [#1536] - @popexie
- [Docs] Mention the two available Spike-rule metrics that are add into the match record - [#1542](https://github.com/jertel/elastalert2/pull/1542) - @ulmako
- [OpsGenie] Corrected spelling of the `opsgenie_default_receipients` configuration option to `opsgenie_default_recipients`. Both variations will continue to work and a warning message will notify affected users. [#1539](https://github.com/jertel/elastalert2/pull/1539) - @lstyles
- [OpsGenie] Prevent templated `opsgenie_teams` and `opsgenie_recipients` from being overwritten with evaluated values first time an alert is sent. [#1540](https://github.com/jertel/elastalert2/issues/1540) [#1539](https://github.com/jertel/elastalert2/pull/1539) - @lstyles
Expand Down
13 changes: 13 additions & 0 deletions docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ rule will no longer be run until either ElastAlert 2 restarts or the rule file h

``show_disabled_rules``: If true, ElastAlert 2 show the disable rules' list when finishes the execution. This defaults to True.

``notify_email``: An email address, or list of email addresses, to which notification emails will be sent. Currently,
only an uncaught exception will send a notification email. The from address, SMTP host, and reply-to header can be set
using ``from_addr``, ``smtp_host``, ``smtp_port`` and ``email_reply_to`` options, respectively. By default, no emails will be sent.

``notify_alert``: List of alerters to execute upon encountering a system error. System errors occur when an unexpected exception is thrown during rule processing. For additional notifications, such as when ElastAlert 2 background tests encounter problems, or when connectivity to the data storage system is lost, enable ``notify_all_errors``.

See the :ref:`Alerts` section for the list of available alerters and their parameters.
Expand Down Expand Up @@ -142,6 +146,15 @@ is "ElastAlert".
``smtp_host``: The SMTP host used to send email notifications. This value will be used for email alerts as well,
unless overwritten in the rule config. The default is "localhost".

``smtp_port``: The SMTP port used to send email notifications. This value will be used for email alerts as well,
unless overwritten in the rule config.

``smtp_ssl``: This enable TLS on SMTP. The default is False.

``smtp_user``: Optional; username for connecting to ``smtp_host``.

``smtp_password``: Optional; password for connecting to ``smtp_host``.

``email_reply_to``: This sets the Reply-To header in emails. The default is the recipient address.

``aws_region``: This makes ElastAlert 2 to sign HTTP requests when using Amazon OpenSearch Service. It'll use instance role keys to sign the requests.
Expand Down
25 changes: 24 additions & 1 deletion elastalert/elastalert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import traceback
from email.mime.text import MIMEText
from smtplib import SMTP
from smtplib import SMTP_SSL
from smtplib import SMTPException
from socket import error
import statsd
Expand Down Expand Up @@ -147,6 +148,10 @@ def __init__(self, args):
self.notify_alerters = self.rules_loader.load_alerts(alert_conf_obj, self.notify_alert)
self.from_addr = self.conf.get('from_addr', 'ElastAlert')
self.smtp_host = self.conf.get('smtp_host', 'localhost')
self.smtp_port = self.conf.get('smtp_port')
self.smtp_ssl = self.conf.get('smtp_ssl', False)
self.smtp_user = self.conf.get('smtp_user')
self.smtp_password = self.conf.get('smtp_password')
self.max_aggregation = self.conf.get('max_aggregation', 10000)
self.buffer_time = self.conf['buffer_time']
self.silence_cache = {}
Expand Down Expand Up @@ -1874,7 +1879,25 @@ def send_notification_email(self, text='', exception=None, rule=None, subject=No
email['Reply-To'] = self.conf.get('email_reply_to', email['To'])

try:
smtp = SMTP(self.smtp_host)
if self.smtp_ssl:
if self.smtp_port:
smtp = SMTP_SSL(self.smtp_host, self.smtp_port)
else:
# default port : 465
smtp = SMTP_SSL(self.smtp_host)
else:
if self.smtp_port:
smtp = SMTP(self.smtp_host, self.smtp_port)
else:
# default port : 25
smtp = SMTP(self.smtp_host)
smtp.ehlo()
if smtp.has_extn('STARTTLS'):
smtp.starttls()

if self.smtp_user is not None:
smtp.login(self.smtp_user, self.smtp_password)

smtp.sendmail(self.from_addr, recipients, email.as_string())
except (SMTPException, error) as e:
elastalert_logger.error('Error connecting to SMTP host: %s' % (e), {'email_body': email_body})
Expand Down
10 changes: 10 additions & 0 deletions examples/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,13 @@ alert_time_limit:
# - console
# - file
# propagate: false
#

# SMTP configuration
#smtp_host: "smtp.example.com"
#smtp_port: 25

# Option username and password for smtp
#smtp_user: someusername
#smtp_password: somepassword