Skip to content

Commit

Permalink
Merge pull request #330 from napalm-automation/alerta-pairs
Browse files Browse the repository at this point in the history
Alerta Publisher: close previous alert from another pair alert
  • Loading branch information
mirceaulinic committed Apr 28, 2020
2 parents 8fd6221 + 70a054d commit 0a778ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
25 changes: 23 additions & 2 deletions docs/publisher/alerta.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,29 @@ Configuration examples:
Available options
^^^^^^^^^^^^^^^^^

The options are generally inherited from the :ref:`publisher-http` Publisher,
with the following notes:
.. _publisher-alerta-pairs:

``pairs``
---------

.. versionadded:: 0.10.0

Hash that defines the remapping of a specific *napalm-logs* notification to a
pair notification that will close the previous alert. For example,
an ``INTERFACE_UP`` alert would close an existing ``INTERFACE_DOWN`` alert,
instead of creating an alert for ``INTERFACE_UP``, and so on.

``pair`` defaults to:

.. code-block:: yaml
pairs:
INTERFACE_UP: INTERFACE_DOWN
OSPF_NEIGHBOR_UP: OSPF_NEIGHBOR_DOWN
ISIS_NEIGHBOR_UP: ISIS_NEIGHBOR_DOWN
The next options are generally inherited from the :ref:`publisher-http`
Publisher, with the following notes:

.. _publisher-alerta-address:

Expand Down
15 changes: 14 additions & 1 deletion napalm_logs/transport/alerta.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,28 @@ def __init__(self, address, port, **kwargs):
if token and 'Authorization' not in self.headers:
self.headers.update({'Authorization': 'Bearer {}'.format(token)})
self.environment = kwargs.get('environment')
self.pairs = kwargs.get('pairs')
if not self.pairs:
self.pairs = {
'INTERFACE_UP': 'INTERFACE_DOWN',
'OSPF_NEIGHBOR_UP': 'OSPF_NEIGHBOR_DOWN',
'ISIS_NEIGHBOR_UP': 'ISIS_NEIGHBOR_DOWN'
}

def publish(self, obj):
data = napalm_logs.utils.unserialize(obj)
error = data['error']
status = 'open'
if error in self.pairs:
error = self.pairs[error]
status = 'closed'
alerta_data = {
'resource': '{host}::{msg}'.format(host=data['host'], msg=data['error']),
'resource': '{host}::{msg}'.format(host=data['host'], msg=error),
'event': data['error'],
'service': ['napalm-logs'],
'text': data['message_details']['message'].strip(),
'attributes': data,
'status': status,
}
if self.environment:
alerta_data['environment'] = self.environment
Expand Down

0 comments on commit 0a778ae

Please sign in to comment.