-
Notifications
You must be signed in to change notification settings - Fork 171
/
alerta_sqs.py
executable file
·79 lines (60 loc) · 2.1 KB
/
alerta_sqs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
import logging
import os
import sys
import time
import boto.sqs
from boto.sqs.message import RawMessage
from flask.config import Config
LOG = logging.getLogger('alerta.sqs')
config = Config('/')
config.from_pyfile('/etc/alertad.conf', silent=True)
config.from_envvar('ALERTA_SVR_CONF_FILE', silent=True)
DEFAULT_AWS_REGION = 'eu-west-1'
DEFAULT_AWS_SQS_QUEUE = 'alerts'
AWS_REGION = os.environ.get('AWS_REGION') or config.get(
'AWS_REGION', DEFAULT_AWS_REGION)
AWS_ACCESS_KEY_ID = os.environ.get(
'AWS_ACCESS_KEY_ID') or config.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get(
'AWS_SECRET_ACCESS_KEY') or config.get('AWS_SECRET_ACCESS_KEY')
AWS_SQS_QUEUE = os.environ.get('AWS_SQS_QUEUE') or config.get(
'AWS_SQS_QUEUE', DEFAULT_AWS_SQS_QUEUE)
class Worker:
def __init__(self):
try:
connection = boto.sqs.connect_to_region(
AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
)
except boto.exception.SQSError as e:
LOG.error('SQS: ERROR - %s' % e)
sys.exit(1)
try:
self.sqs = connection.create_queue(AWS_SQS_QUEUE)
self.sqs.set_message_class(RawMessage)
except boto.exception.SQSError as e:
LOG.error('SQS: ERROR - %s' % e)
sys.exit(1)
def run(self):
while True:
LOG.debug('Waiting for alert on SQS queue "%s"...' % AWS_SQS_QUEUE)
try:
message = self.sqs.read(wait_time_seconds=20)
except boto.exception.SQSError as e:
LOG.error('SQS: ERROR - %s' % e)
time.sleep(20)
continue
if message:
self.process_message(message)
def process_message(self, message):
LOG.info('SQS: Received message - %s' % message.get_body())
self.sqs.delete_message(message)
def main():
try:
Worker().run()
except (SystemExit, KeyboardInterrupt):
sys.exit(0)
if __name__ == '__main__':
main()