forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerta_zabbix.py
104 lines (76 loc) · 3.95 KB
/
alerta_zabbix.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import logging
import os
try:
from alerta.plugins import app # alerta >= 5.0
except ImportError:
from alerta.app import app # alerta < 5.0
from alerta.plugins import PluginBase
from pyzabbix import ZabbixAPI, ZabbixAPIException
LOG = logging.getLogger('alerta.plugins.zabbix')
DEFAULT_ZABBIX_API_URL = 'http://localhost:10080'
ZABBIX_API_URL = os.environ.get('ZABBIX_API_URL') or app.config.get('ZABBIX_API_URL', None)
ZABBIX_USER = os.environ.get('ZABBIX_USER') or app.config['ZABBIX_USER']
ZABBIX_PASSWORD = os.environ.get('ZABBIX_PASSWORD') or app.config['ZABBIX_PASSWORD']
NO_ACTION = 0
ACTION_CLOSE = 1
class ZabbixEventAck(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
return
def status_change(self, alert, status, text):
if alert.event_type != 'zabbixAlert':
return
if alert.status == status or not status in ['ack', 'closed']:
return
event_id = alert.attributes.get('eventId', None)
trigger_id = alert.attributes.get('triggerId', None)
if not event_id:
LOG.error('Zabbix: eventId missing from alert attributes')
return
# login to zabbix
zabbix_api_url = ZABBIX_API_URL or alert.attributes.get('zabbixUrl', DEFAULT_ZABBIX_API_URL)
self.zapi = ZabbixAPI(zabbix_api_url)
self.zapi.login(ZABBIX_USER, ZABBIX_PASSWORD)
LOG.debug('Zabbix: acknowledge (%s) event=%s, resource=%s (triggerId=%s, eventId=%s) ', status, alert.event, alert.resource, trigger_id, event_id)
if status == 'ack':
try:
r = self.zapi.event.get(objectids=trigger_id, acknowledged=False, output='extend', sortfield='clock', sortorder='DESC', limit=10)
event_ids = [e['eventid'] for e in r]
except ZabbixAPIException:
event_ids = None
LOG.debug('Zabbix: status=ack; triggerId %s => eventIds %s', trigger_id, ','.join(event_ids))
try:
LOG.debug('Zabbix: ack all events for trigger...')
r = self.zapi.event.acknowledge(eventids=event_ids, message='%s: %s' % (status, text), action=NO_ACTION)
except ZabbixAPIException:
try:
LOG.debug('Zabbix: ack all failed, ack only the one event')
r = self.zapi.event.acknowledge(eventids=event_id, message='%s: %s' % (status, text), action=NO_ACTION)
except ZabbixAPIException as e:
raise RuntimeError("Zabbix: ERROR - %s", e)
finally:
self.zapi.do_request('user.logout')
LOG.debug('Zabbix: event.acknowledge(ack) => %s', r)
text = text + ' (acknowledged in Zabbix)'
elif status == 'closed':
try:
r = self.zapi.event.get(objectids=trigger_id, acknowledged=True, output='extend', sortfield='clock', sortorder='DESC', limit=10)
event_ids = [e['eventid'] for e in r]
except ZabbixAPIException:
event_ids = None
LOG.debug('Zabbix: status=closed; triggerId %s => eventIds %s', trigger_id, ','.join(event_ids))
try:
LOG.debug('Zabbix: close all events for trigger...')
r = self.zapi.event.acknowledge(eventids=event_ids, message='%s: %s' % (status, text), action=ACTION_CLOSE)
except ZabbixAPIException:
try:
LOG.debug('Zabbix: ack all failed, close only the one event')
r = self.zapi.event.acknowledge(eventids=event_id, message='%s: %s' % (status, text), action=ACTION_CLOSE)
except ZabbixAPIException as e:
raise RuntimeError("Zabbix: ERROR - %s", e)
finally:
self.zapi.do_request('user.logout')
LOG.debug('Zabbix: event.acknowledge(closed) => %s', r)
text = text + ' (closed in Zabbix)'
return alert, status, text