Skip to content

Commit

Permalink
Merge pull request #512 from earthgecko/SNAB
Browse files Browse the repository at this point in the history
IssueID #4416: settings - additional SMTP_OPTS
  • Loading branch information
earthgecko committed Feb 6, 2022
2 parents f8f11a6 + 0bd6f1c commit 87997f1
Show file tree
Hide file tree
Showing 14 changed files with 1,061 additions and 14 deletions.
15 changes: 14 additions & 1 deletion docs/alerts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ and :mod:`settings.SMTP_OPTS` would need to look like this.
# that do not have a matching namespace defined in recipients
'default_recipient': ['no_email'],
'embed-images': True,
# You can specify the address of an SMTP server if you do not want to or
# cannot use a local SMTP server on 127.0.0.1
'smtp_server': {
'host': '127.0.0.1',
'port': 25,
# Whether to use SSL, not required, optional
'ssl': False,
# A SMTP username, not required, optional
'user': None,
# A SMTP password, not required, optional
'password': None,
},
}
Smart alerting
Expand Down Expand Up @@ -129,7 +141,8 @@ Boundary related alert settings (static and dynamic thresholds) are:
- :mod:`settings.BOUNDARY_ALERTER_OPTS` - can be used to change the Boundary
alert rate limits
- :mod:`settings.BOUNDARY_SMTP_OPTS` - must be defined if you want to send SMTP
alerts
alerts. Note that Boundary will use the same `smtp_server` as defined in
:mod:`settings.SMTP_OPTS`
- :mod:`settings.BOUNDARY_PAGERDUTY_OPTS` - must be defined if you want to alert
via Pagerduty
- :mod:`settings.BOUNDARY_SLACK_OPTS` - must be defined if you want to alert via
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def setup(app):
# The short X.Y version.
version = u'2.1'
# The full version, including alpha/beta/rc tags.
release = u'2.1.0-4400'
release = u'2.1.0-4416'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
6 changes: 5 additions & 1 deletion docs/readthedocs.requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Task #3960: SNYK-PYTHON-JINJA2-1012994
# @modified 20210407 - Support #4008: SNYK-PYTHON-PILLOW-1090584
# @modified 20220110 - Task #4362: snyk - numpy and pillow updates
# @modified 20220206 - Feature #4416: settings - additional SMTP_OPTS
# Use pip 10.0.1 as may be a readthedocs issue - testing
pip>=10.0.1
# @modified 20200808 - Task #3608: Update Skyline to Python 3.8.3 and deps
Expand Down Expand Up @@ -71,7 +72,10 @@ mock==4.0.3
#numpy==1.19.4
# @modified 20220110 - Task #4362: snyk - numpy and pillow updates
#numpy==1.21.5
numpy==1.22.0
# @modified 20220206 - Feature #4416: settings - additional SMTP_OPTS
# numpy 1.22.0 not available on readthedocs
#numpy==1.22.0
numpy==1.21.5
# @modified 20211220 - Task #4344: Update dependencies
#scipy==1.5.4
scipy==1.7.3
Expand Down
24 changes: 23 additions & 1 deletion skyline/analyzer/alerters.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
# @added 20210724 - Feature #4196: functions.aws.send_sms
from functions.aws.send_sms import send_sms
from functions.settings.get_sms_recipients import get_sms_recipients
# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
from functions.smtp.determine_smtp_server import determine_smtp_server

# @added 20201127 - Feature #3820: HORIZON_SHARDS
try:
Expand Down Expand Up @@ -1154,8 +1156,28 @@ def alert_smtp(alert, metric, context):
# This allows for all the steps to be processed in the testing or docker
# context without actually sending the email.
if send_email_alert:

# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
smtp_server = None
try:
smtp_server = determine_smtp_server()
except Exception as err:
logger.error(traceback.format_exc())
logger.error('error :: determine_smtp_server failed - %s' % err)
if not smtp_server:
logger.error('error :: no smtp_server, cannot send mail')

try:
s = SMTP('127.0.0.1')
# @modified 20220203 - Feature #4416: settings - additional SMTP_OPTS
# s = SMTP('127.0.0.1')
if not smtp_server['ssl']:
s = SMTP(smtp_server['host'], smtp_server['port'])
else:
from smtplib import SMTP_SSL
s = SMTP_SSL(smtp_server['host'], smtp_server['port'])
if smtp_server['user']:
s.login(smtp_server['user'], smtp_server['password'])

# @modified 20180524 - Task #2384: Change alerters to cc other recipients
# Send to primary_recipient and cc_recipients
# s.sendmail(sender, recipient, msg.as_string())
Expand Down
23 changes: 22 additions & 1 deletion skyline/analyzer_dev/alerters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from smtplib import SMTP
# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
from smtplib import SMTP_SSL

import alerters
import urllib2

Expand All @@ -12,6 +15,9 @@
sys.path.insert(0, os.path.dirname(__file__))
import settings

# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
from functions.smtp.determine_smtp_server import determine_smtp_server

"""
Create any alerter you want here. The function will be invoked from trigger_alert.
Two arguments will be passed, both of them tuples: alert and metric.
Expand Down Expand Up @@ -98,6 +104,13 @@ def alert_smtp(alert, metric):

body = 'skyline analyzer alert <br> Anomalous value: %s <br> Next alert in: %s seconds <br> <a href="%s">%s</a>' % (metric[0], alert[2], link, img_tag)

# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
smtp_server = None
try:
smtp_server = determine_smtp_server()
except:
return

for recipient in recipients:
msg = MIMEMultipart('alternative')
msg['Subject'] = '[skyline alert] ' + metric[1]
Expand All @@ -110,7 +123,15 @@ def alert_smtp(alert, metric):
msg_attachment.add_header('Content-ID', '<%s>' % content_id)
msg.attach(msg_attachment)

s = SMTP('127.0.0.1')
# @modified 20220203 - Feature #4416: settings - additional SMTP_OPTS
# s = SMTP('127.0.0.1')
if not smtp_server['ssl']:
s = SMTP(smtp_server['host'], smtp_server['port'])
else:
s = SMTP_SSL(smtp_server['host'], smtp_server['port'])
if smtp_server['user']:
s.login(smtp_server['user'], smtp_server['password'])

s.sendmail(sender, recipient, msg.as_string())
s.quit()

Expand Down
2 changes: 0 additions & 2 deletions skyline/boundary/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,12 +1000,10 @@ def run(self):
trigger_alert(test_alerter, 1, metric_name, 10, 1, 'testing', int(time()), 0)
except:
logger.error('error :: test trigger_alert - %s' % traceback.format_exc())
logger.error('error :: failed to test trigger_alert :: %s' % metric_name)
try:
os.remove(alert_test_file)
except OSError:
logger.error('error - failed to remove %s, continuing' % alert_test_file)
pass

if len(boundary_metrics) == 0:
logger.info('no Boundary metrics in redis. try adding some - see README')
Expand Down
25 changes: 24 additions & 1 deletion skyline/boundary/boundary_alerters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# hashlib not used
# import hashlib
from smtplib import SMTP
# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
from smtplib import SMTP_SSL

import re
# from time import time
Expand Down Expand Up @@ -95,6 +97,8 @@
# @added 20210724 - Feature #4196: functions.aws.send_sms
from functions.aws.send_sms import send_sms
from functions.settings.get_sms_recipients import get_sms_recipients
# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
from functions.smtp.determine_smtp_server import determine_smtp_server

# @added 20201127 - Feature #3820: HORIZON_SHARDS
try:
Expand Down Expand Up @@ -431,7 +435,26 @@ def unique_noHash(seq):
msg_attachment.add_header('Content-ID', '<%s>' % content_id)
msg.attach(msg_attachment)

s = SMTP('127.0.0.1')
# @added 20220203 - Feature #4416: settings - additional SMTP_OPTS
smtp_server = None
try:
smtp_server = determine_smtp_server()
except Exception as err:
logger.error(traceback.format_exc())
logger.error('error :: determine_smtp_server failed' % err)
if not smtp_server:
logger.error('error :: no smtp_server mail cannot be sent')
return

# @modified 20220203 - Feature #4416: settings - additional SMTP_OPTS
# s = SMTP('127.0.0.1')
if not smtp_server['ssl']:
s = SMTP(smtp_server['host'], smtp_server['port'])
else:
s = SMTP_SSL(smtp_server['host'], smtp_server['port'])
if smtp_server['user']:
s.login(smtp_server['user'], smtp_server['password'])

# @modified 20180524 - Task #2384: Change alerters to cc other recipients
# Send to primary_recipient and cc_recipients
# s.sendmail(sender, recipient, msg.as_string())
Expand Down

0 comments on commit 87997f1

Please sign in to comment.