Compare commits
1 commit
master
...
sns-plugin
Author | SHA1 | Date | |
---|---|---|---|
![]() |
15ff1f2552 |
3 changed files with 49 additions and 24 deletions
plugins/sns
|
@ -1,7 +1,7 @@
|
||||||
AWS SNS Plugin
|
AWS SNS Plugin
|
||||||
==============
|
==============
|
||||||
|
|
||||||
Send alerts to AWS SNS topic.
|
Send alerts to AWS SNS topic, with support for Fifo.
|
||||||
|
|
||||||
For help, join [](https://slack.alerta.dev)
|
For help, join [](https://slack.alerta.dev)
|
||||||
|
|
||||||
|
@ -29,8 +29,6 @@ server configuration file or as environment variables.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
PLUGINS = ['sns']
|
PLUGINS = ['sns']
|
||||||
AWS_ACCESS_KEY_ID = '' # default="not set"
|
|
||||||
AWS_SECRET_ACCESS_KEY = '' # default="not set"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Default Configuration**
|
**Default Configuration**
|
||||||
|
@ -53,4 +51,4 @@ References
|
||||||
License
|
License
|
||||||
-------
|
-------
|
||||||
|
|
||||||
Copyright (c) 2016 Nick Satterly. Available under the MIT License.
|
Copyright (c) 2016,2021 Nick Satterly. Available under the MIT License.
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
|
|
||||||
import boto.exception
|
import boto3
|
||||||
import boto.sns
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -16,8 +15,6 @@ DEFAULT_AWS_REGION = 'eu-west-1'
|
||||||
DEFAULT_AWS_SNS_TOPIC = 'notify'
|
DEFAULT_AWS_SNS_TOPIC = 'notify'
|
||||||
|
|
||||||
AWS_REGION = os.environ.get('AWS_REGION') or app.config.get('AWS_REGION', DEFAULT_AWS_REGION)
|
AWS_REGION = os.environ.get('AWS_REGION') or app.config.get('AWS_REGION', DEFAULT_AWS_REGION)
|
||||||
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID') or app.config.get('AWS_ACCESS_KEY_ID')
|
|
||||||
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY') or app.config.get('AWS_SECRET_ACCESS_KEY')
|
|
||||||
AWS_SNS_TOPIC = os.environ.get('AWS_SNS_TOPIC') or app.config.get('AWS_SNS_TOPIC', DEFAULT_AWS_SNS_TOPIC)
|
AWS_SNS_TOPIC = os.environ.get('AWS_SNS_TOPIC') or app.config.get('AWS_SNS_TOPIC', DEFAULT_AWS_SNS_TOPIC)
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,22 +22,23 @@ class SnsTopicPublisher(PluginBase):
|
||||||
|
|
||||||
def __init__(self, name=None):
|
def __init__(self, name=None):
|
||||||
try:
|
try:
|
||||||
self.connection = boto.sns.connect_to_region(
|
self.client = boto3.client('sns')
|
||||||
region_name=AWS_REGION,
|
# region_name=AWS_REGION,
|
||||||
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
# )
|
||||||
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.error('Error connecting to SNS topic %s: %s', AWS_SNS_TOPIC, e)
|
LOG.error('Error connecting to SNS topic %s: %s', AWS_SNS_TOPIC, e)
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
if not self.connection:
|
if not self.client:
|
||||||
LOG.error('Failed to connect to SNS topic %s - check AWS credentials and region', AWS_SNS_TOPIC)
|
LOG.error('Failed to connect to SNS topic %s - check AWS credentials and region', AWS_SNS_TOPIC)
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = self.connection.create_topic(AWS_SNS_TOPIC)
|
response = self.client.create_topic(
|
||||||
except boto.exception.BotoServerError as e:
|
Name=AWS_SNS_TOPIC,
|
||||||
|
FifoTopic=True if AWS_SNS_TOPIC.endswith('fifo') else False
|
||||||
|
)
|
||||||
|
except self.client.exception.BotoServerError as e:
|
||||||
LOG.error('Error creating SNS topic %s: %s', AWS_SNS_TOPIC, e)
|
LOG.error('Error creating SNS topic %s: %s', AWS_SNS_TOPIC, e)
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
|
@ -54,16 +52,45 @@ class SnsTopicPublisher(PluginBase):
|
||||||
|
|
||||||
LOG.info('Configured SNS publisher on topic "%s"', self.topic_arn)
|
LOG.info('Configured SNS publisher on topic "%s"', self.topic_arn)
|
||||||
|
|
||||||
def pre_receive(self, alert):
|
def pre_receive(self, alert, **kwargs):
|
||||||
return alert
|
return alert
|
||||||
|
|
||||||
def post_receive(self, alert):
|
def post_receive(self, alert, **kwargs):
|
||||||
|
|
||||||
LOG.info('Sending message %s to SNS topic "%s"', alert.get_id(), self.topic_arn)
|
LOG.info('Sending message %s to SNS topic "%s"', alert.get_id(), self.topic_arn)
|
||||||
LOG.debug('Message: %s', alert.get_body())
|
LOG.debug('Message: %s', alert.get_body())
|
||||||
|
|
||||||
response = self.connection.publish(topic=self.topic_arn, message=alert.get_body())
|
response = self.client.publish(
|
||||||
|
TopicArn=self.topic_arn,
|
||||||
|
Subject='Alert {} received'.format(alert.last_receive_id),
|
||||||
|
MessageStructure='json',
|
||||||
|
Message=alert.get_body(),
|
||||||
|
MessageAttributes={
|
||||||
|
'resource': {
|
||||||
|
'DataType': 'string',
|
||||||
|
'StringValue': alert.resource
|
||||||
|
},
|
||||||
|
'event': {
|
||||||
|
'DataType': 'string',
|
||||||
|
'StringValue': alert.event
|
||||||
|
},
|
||||||
|
'environment': {
|
||||||
|
'DataType': 'string',
|
||||||
|
'StringValue': alert.environment
|
||||||
|
},
|
||||||
|
'severity': {
|
||||||
|
'DataType': 'string',
|
||||||
|
'StringValue': alert.severity
|
||||||
|
},
|
||||||
|
'status': {
|
||||||
|
'DataType': 'string',
|
||||||
|
'StringValue': alert.status
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MessageDeduplicationId=alert.last_receive_id,
|
||||||
|
MessageGroupId=alert.id
|
||||||
|
)
|
||||||
LOG.debug('Response: %s', response)
|
LOG.debug('Response: %s', response)
|
||||||
|
|
||||||
def status_change(self, alert, status, text):
|
def status_change(self, alert, status, text, **kwargs):
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
|
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
version = '5.3.1'
|
version = '6.0.0'
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="alerta-sns",
|
name="alerta-sns",
|
||||||
version=version,
|
version=version,
|
||||||
description='Alerta plugin for AWS SNS',
|
description='Alerta plugin for AWS SNS (with Fifo)',
|
||||||
url='https://github.com/alerta/alerta-contrib',
|
url='https://github.com/alerta/alerta-contrib',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
author='Nick Satterly',
|
author='Nick Satterly',
|
||||||
author_email='nick.satterly@theguardian.com',
|
author_email='nick.satterly@gmail.com',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
py_modules=['alerta_sns'],
|
py_modules=['alerta_sns'],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'boto'
|
'boto3'
|
||||||
],
|
],
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
zip_safe=True,
|
zip_safe=True,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue