1
2
3
4
5
6
7
8
9
10
11 from operator import itemgetter
12 from Products import Zuul
13 from zope.component import getUtilitiesFor
14 from Products.ZenModel.interfaces import IAction
15 from Products.ZenUtils.Ext import DirectRouter
16 from Products.ZenUtils.extdirect.router import DirectResponse
17 from Products.Zuul.decorators import serviceConnectionError
18 from zenoss.protocols.protobufs.zep_pb2 import RULE_TYPE_JYTHON
19 from Products.ZenMessaging.audit import audit
20 from Products.ZenModel.Trigger import DuplicateTriggerName
21
22 import logging
23
24 log = logging.getLogger('zen.triggers');
27 """
28 Router for Triggers UI section under Events.
29 """
30
32 return Zuul.getFacade('triggers', self)
33
34 @serviceConnectionError
37
38 @serviceConnectionError
41
42 @serviceConnectionError
44 try:
45 data = self._getFacade().addTrigger(newId)
46 except DuplicateTriggerName, tnc:
47 log.debug("Exception DuplicateTriggerName: %s" % tnc)
48 return DirectResponse.fail(str(tnc))
49 else:
50 audit('UI.Trigger.Add', newId)
51 return DirectResponse.succeed(data=data)
52
53 @serviceConnectionError
55 updated_count = self._getFacade().removeTrigger(uuid)
56 audit('UI.Trigger.Remove', uuid)
57 msg = "Trigger removed successfully. {count} {noun} {verb} updated.".format(
58 count = updated_count,
59 noun = 'notification' if updated_count == 1 else 'notifications',
60 verb = 'was' if updated_count == 1 else 'were'
61 )
62 return DirectResponse.succeed(msg=msg, data=None)
63
64 @serviceConnectionError
67
68 @serviceConnectionError
70 data['rule']['api_version'] = 1
71 data['rule']['type'] = RULE_TYPE_JYTHON
72 triggerUid = data['uuid']
73 response = self._getFacade().updateTrigger(**data)
74 audit('UI.Trigger.Edit', triggerUid, data_=data)
75 return DirectResponse.succeed(msg="Trigger updated successfully.", data=response)
76
77 @serviceConnectionError
79 try:
80 response = self._getFacade().parseFilter(source)
81 return DirectResponse.succeed(data=response)
82 except Exception, e:
83 log.exception(e)
84 return DirectResponse.exception(e,
85 'Error parsing filter source. Please check your syntax.')
86
87
88
89 @serviceConnectionError
91 response = self._getFacade().getNotificationInfos()
92 return DirectResponse.succeed(data=Zuul.marshal(response))
93
94 @serviceConnectionError
96 response = self._getFacade().addNotification(newId, action)
97 audit('UI.Notification.Add', newId)
98 return DirectResponse.succeed(data=Zuul.marshal(response))
99
100 @serviceConnectionError
102 response = self._getFacade().removeNotification(uid)
103 audit('UI.Notification.Remove', uid)
104 return DirectResponse.succeed(msg="Notification removed successfully.", data=response)
105
106 @serviceConnectionError
108 utils = getUtilitiesFor(IAction)
109 actionTypes = sorted((dict(id=id, name=util.name) for id, util in utils), key=itemgetter('id'))
110 log.debug('notification action types are: %s' % actionTypes)
111 return DirectResponse.succeed(data=actionTypes)
112
113 @serviceConnectionError
117
118 @serviceConnectionError
120 notificationUid = data['uid']
121 response = self._getFacade().updateNotification(**data)
122 audit('UI.Notification.Edit', notificationUid, data_=data, maskFields_='password')
123 return DirectResponse.succeed(msg="Notification updated successfully.", data=Zuul.marshal(response))
124
125 @serviceConnectionError
129
130
131 @serviceConnectionError
133 response = self._getFacade().getWindows(uid)
134 return DirectResponse.succeed(data=Zuul.marshal(response))
135
136 @serviceConnectionError
138 response = self._getFacade().addWindow(contextUid, newId)
139 audit('UI.NotificationWindow.Add', newId, notification=contextUid)
140 return DirectResponse.succeed(data=Zuul.marshal(response))
141
142 @serviceConnectionError
144 response = self._getFacade().removeWindow(uid)
145 audit('UI.NotificationWindow.Remove', uid)
146 return DirectResponse.succeed(data=Zuul.marshal(response))
147
148 @serviceConnectionError
150 response = self._getFacade().getWindow(uid)
151 return DirectResponse.succeed(data=Zuul.marshal(response))
152
153 @serviceConnectionError
155 windowUid = data['uid']
156 response = self._getFacade().updateWindow(data)
157 audit('UI.NotificationWindow.Edit', windowUid, data_=data)
158 return DirectResponse.succeed(data=Zuul.marshal(response))
159
160 @serviceConnectionError
162 facade = self._getFacade()
163 triggers, notifications = facade.exportConfiguration(triggerIds, notificationIds)
164 msg = "Exported %d triggers and %d notifications" % (
165 len(triggers), len(notifications))
166 audit('UI.TriggerNotification.Export', msg)
167 return DirectResponse.succeed(triggers=Zuul.marshal(triggers),
168 notifications=Zuul.marshal(notifications),
169 msg=msg)
170
171 @serviceConnectionError
173 try:
174 tcount = len(triggers) if triggers is not None else 0
175 ncount = len(notifications) if notifications is not None else 0
176 facade = self._getFacade()
177 itcount, incount = facade.importConfiguration(triggers, notifications)
178 msg = "Imported %d of %d triggers and %d of %d notifications" % (
179 tcount, itcount, ncount, incount)
180 audit('UI.TriggerNotification.Import', msg)
181 return DirectResponse.succeed(msg=msg)
182 except Exception as ex:
183 audit('UI.TriggerNotification.Import', "Failed to import trigger/notification data")
184 log.exception("Unable to import data:\ntriggers=%s\nnotifications=%s",
185 repr(triggers), repr(notifications))
186 return DirectResponse.fail(str(ex))
187