本文整理汇总了Python中elastalert.alerts.EmailAlerter类的典型用法代码示例。如果您正苦于以下问题:Python EmailAlerter类的具体用法?Python EmailAlerter怎么用?Python EmailAlerter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EmailAlerter类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_email_with_args
def test_email_with_args():
rule = {'name': 'test alert', 'email': ['[email protected]', '[email protected]'], 'from_addr': '[email protected]',
'type': mock_rule(), 'timestamp_field': '@timestamp', 'email_reply_to': '[email protected]',
'alert_subject': 'Test alert for {0} {1}', 'alert_subject_args': ['test_term', 'test.term'], 'alert_text': 'Test alert for {0} and {1} {2}',
'alert_text_args': ['test_arg1', 'test_arg2', 'test.arg3']}
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'test_term': 'test_value', 'test_arg1': 'testing', 'test': {'term': ':)', 'arg3': u'☃'}}])
expected = [mock.call('localhost'),
mock.call().ehlo(),
mock.call().has_extn('STARTTLS'),
mock.call().starttls(),
mock.call().sendmail(mock.ANY, ['[email protected]', '[email protected]'], mock.ANY),
mock.call().close()]
assert mock_smtp.mock_calls == expected
body = mock_smtp.mock_calls[4][1][2]
# Extract the MIME encoded message body
body_text = body.split('\n\n')[-1][:-1].decode('base64')
assert 'testing' in body_text
assert '<MISSING VALUE>' in body_text
assert '☃' in body_text
assert 'Reply-To: [email protected]' in body
assert 'To: [email protected]' in body
assert 'From: [email protected]' in body
assert 'Subject: Test alert for test_value :)' in body
示例2: test_email_with_cc_and_bcc
def test_email_with_cc_and_bcc():
rule = {'name': 'test alert', 'email': ['[email protected]', '[email protected]'], 'from_addr': '[email protected]',
'type': mock_rule(), 'timestamp_field': '@timestamp', 'email_reply_to': '[email protected]',
'cc': ['[email protected]', '[email protected]'], 'bcc': '[email protected]'}
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'test_term': 'test_value'}])
expected = [mock.call('localhost'),
mock.call().ehlo(),
mock.call().has_extn('STARTTLS'),
mock.call().starttls(),
mock.call().sendmail(mock.ANY,
['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'],
mock.ANY),
mock.call().close()]
assert mock_smtp.mock_calls == expected
body = mock_smtp.mock_calls[4][1][2]
assert 'Reply-To: [email protected]' in body
assert 'To: [email protected]' in body
assert 'CC: [email protected],[email protected]' in body
assert 'From: [email protected]' in body
示例3: test_email_with_args
def test_email_with_args():
rule = {'name': 'test alert', 'email': ['[email protected]', '[email protected]'], 'from_addr': '[email protected]',
'type': mock_rule(), 'timestamp_field': '@timestamp', 'email_reply_to': '[email protected]',
'alert_subject': 'Test alert for {0}', 'alert_subject_args': ['test_term'], 'alert_text': 'Test alert for {0} and {1}',
'alert_text_args': ['test_arg1', 'test_arg2']}
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'test_term': 'test_value', 'test_arg1': 'testing'}])
expected = [mock.call('localhost'),
mock.call().ehlo(),
mock.call().has_extn('STARTTLS'),
mock.call().starttls(),
mock.call().sendmail(mock.ANY, ['[email protected]', '[email protected]'], mock.ANY),
mock.call().close()]
assert mock_smtp.mock_calls == expected
body = mock_smtp.mock_calls[4][1][2]
assert 'testing' in body
assert '<MISSING VALUE>' in body
assert 'Reply-To: [email protected]' in body
assert 'To: [email protected]' in body
assert 'From: [email protected]' in body
assert 'Subject: Test alert for test_value' in body
示例4: test_email_with_auth
def test_email_with_auth():
rule = {'name': 'test alert', 'email': ['[email protected]', '[email protected]'], 'from_addr': '[email protected]',
'type': mock_rule(), 'timestamp_field': '@timestamp', 'email_reply_to': '[email protected]',
'alert_subject': 'Test alert for {0}', 'alert_subject_args': ['test_term'], 'smtp_auth_file': 'file.txt'}
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
with mock.patch('elastalert.alerts.yaml_loader') as mock_open:
mock_open.return_value = {'user': 'someone', 'password': 'hunter2'}
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'test_term': 'test_value'}])
expected = [mock.call('localhost'),
mock.call().login('someone', 'hunter2'),
mock.call().sendmail(mock.ANY, ['[email protected]', '[email protected]'], mock.ANY),
mock.call().close()]
assert mock_smtp.mock_calls == expected
示例5: test_email_query_key_in_subject
def test_email_query_key_in_subject():
rule = {'name': 'test alert', 'email': ['[email protected]', '[email protected]'],
'type': mock_rule(), 'timestamp_field': '@timestamp', 'email_reply_to': '[email protected]',
'query_key': 'username'}
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'test_term': 'test_value', 'username': 'werbenjagermanjensen'}])
body = mock_smtp.mock_calls[1][1][2]
lines = body.split('\n')
found_subject = False
for line in lines:
if line.startswith('Subject'):
assert 'werbenjagermanjensen' in line
found_subject = True
assert found_subject
示例6: test_email
def test_email():
rule = {'name': 'test alert', 'email': ['[email protected]', '[email protected]'],
'type': mock_rule(), 'timestamp_field': '@timestamp', 'email_reply_to': '[email protected]',
'alert_subject': 'Test alert for {0}', 'alert_subject_args': ['test_term']}
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'test_term': 'test_value'}])
expected = [mock.call('localhost'),
mock.call().sendmail(mock.ANY, ['[email protected]', '[email protected]'], mock.ANY),
mock.call().close()]
assert mock_smtp.mock_calls == expected
body = mock_smtp.mock_calls[1][1][2]
assert 'Reply-To: [email protected]' in body
assert 'To: [email protected]' in body
assert 'Subject: Test alert for test_value' in body
示例7: test_email_from_field
def test_email_from_field():
rule = {'name': 'test alert', 'email': ['[email protected]'], 'email_add_domain': 'example.com',
'type': mock_rule(), 'timestamp_field': '@timestamp', 'email_from_field': 'data.user', 'owner': 'owner_value'}
# Found, without @
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'data': {'user': 'qlo'}}])
assert mock_smtp.mock_calls[4][1][1] == ['[email protected]']
# Found, with @
rule['email_add_domain'] = '@example.com'
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'data': {'user': 'qlo'}}])
assert mock_smtp.mock_calls[4][1][1] == ['[email protected]']
# Not found
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'data': {'foo': 'qlo'}}])
assert mock_smtp.mock_calls[4][1][1] == ['[email protected]']
# Found, wrong type
with mock.patch('elastalert.alerts.SMTP') as mock_smtp:
mock_smtp.return_value = mock.Mock()
alert = EmailAlerter(rule)
alert.alert([{'data': {'user': 17}}])
assert mock_smtp.mock_calls[4][1][1] == ['[email protected]']