0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-01-30 03:33:59 +00:00
alerta_alerta/alerta/pagerduty/daemon.py
2013-10-19 10:28:16 +01:00

91 lines
2.3 KiB
Python

import time
from alerta.common import config
from alerta.common import log as logging
from alerta.common.daemon import Daemon
from alerta.common.alert import Alert
from alerta.common.heartbeat import Heartbeat
from alerta.common import severity_code, status_code
from alerta.common.mq import Messaging, MessageHandler
from alerta.pagerduty.pdclientapi import PagerDutyClient
Version = '2.0.2'
LOG = logging.getLogger(__name__)
CONF = config.CONF
class PagerDutyMessage(MessageHandler):
def __init__(self, mq):
self.mq = mq
self.pd = PagerDutyClient()
MessageHandler.__init__(self)
def on_message(self, headers, body):
LOG.debug("Received: %s", body)
try:
pdAlert = Alert.parse_alert(body)
except ValueError:
return
if 'pagerduty' not in pdAlert.tags:
return
if pdAlert.status == status_code.OPEN:
self.pd.trigger_event(pdAlert)
elif pdAlert.status == status_code.ACK:
self.pd.acknowledge_event(pdAlert)
elif pdAlert.status == status_code.CLOSED:
self.pd.resolve_event(pdAlert)
def on_disconnected(self):
self.mq.reconnect()
class PagerDutyDaemon(Daemon):
pagerduty_opts = {
'pagerduty_endpoint': 'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
'pagerduty_api_key': '',
}
def __init__(self, prog, **kwargs):
config.register_opts(PagerDutyDaemon.pagerduty_opts)
Daemon.__init__(self, prog, kwargs)
def run(self):
self.running = True
# Connect to message queue
self.mq = Messaging()
self.mq.connect(callback=PagerDutyMessage(self.mq))
self.mq.subscribe(destination=CONF.outbound_topic) # TODO(nsatterl): use dedicated queue?
while not self.shuttingdown:
try:
LOG.debug('Waiting for PagerDuty messages...')
time.sleep(CONF.loop_every)
LOG.debug('Send heartbeat...')
heartbeat = Heartbeat(version=Version)
self.mq.send(heartbeat)
except (KeyboardInterrupt, SystemExit):
self.shuttingdown = True
LOG.info('Shutdown request received...')
self.running = False
LOG.info('Disconnecting from message broker...')
self.mq.disconnect()