1
2
3
4
5
6
7
8
9
10
11 import logging
12 log = logging.getLogger("zen.notifications")
13
14
15 from zope.interface import implements
16 from Globals import InitializeClass
17 from Globals import DTMLFile
18 from AccessControl import ClassSecurityInfo
19 from AdministrativeRoleable import AdministrativeRoleable
20 from Products.ZenRelations.RelSchema import *
21 from Products.ZenModel.ZenossSecurity import *
22 from Products.ZenModel.ZenModelRM import ZenModelRM
23 from Products.ZenUtils.guid.interfaces import IGloballyIdentifiable
24 from Products.ZenUtils.Time import LocalDateTime
25 from Products.ZenUtils.ZenTales import talesEvalStr, talEval
26 from Products.ZenEvents.events2.proxy import EventProxy, EventSummaryProxy
27 from zenoss.protocols.protobufs.zep_pb2 import Event, EventSummary
28 from zenoss.protocols.jsonformat import from_dict
29 from zenoss.protocols.wrappers import EventSummaryAdapter
30 from Products.ZenEvents.EventManagerBase import EventManagerBase
38
40 """Manage notification subscriptions.
41
42 @todo: change the icon parameter in factory_type_information.
43 """
44
45 _id = "NotificationSubscriptionManager"
46 root = 'NotificationSubscriptions'
47 meta_type = _id
48
49 sub_meta_types = ("NotificationSubscription",)
50
51 factory_type_information = (
52 {
53 'id' : _id,
54 'meta_type' : _id,
55 'description' : """Management of notification subscriptions""",
56 'icon' : 'UserSettingsManager.gif',
57 'product' : 'ZenModel',
58 'factory' : 'manage_addNotificationSubscriptionManager',
59 'immediate_view' : 'editSettings',
60 'actions' : (
61 {
62 'id' : 'settings',
63 'name' : 'Settings',
64 'action' : '../editSettings',
65 'permissions' : ( ZEN_MANAGE_DMD, )
66 })
67 },
68 )
69
70
71 addNotificationSubscription = DTMLFile('dtml/addNotificationSubscription',globals())
79
85
87 @property
89 """
90 The old event system used to export 'severityString' as a field to use in notifications.
91 """
92 try:
93 return EventManagerBase.severities[self.severity]
94 except KeyError:
95 return "Unknown"
96
98 - def __init__(self, evtsummary, clearevtsummary=None):
99 super(NotificationEventContextWrapper,self).__init__()
100 self['evt'] = NotificationEventSummaryProxy(evtsummary)
101 self['eventSummary'] = EventSummaryAdapter(evtsummary)
102 if clearevtsummary is not None:
103 self['clearEvt'] = NotificationEventSummaryProxy(clearevtsummary)
104 self['clearEventSummary'] = EventSummaryAdapter(clearevtsummary)
105 else:
106 self['clearEvt'] = NoneDefaultingDict()
107 self['clearEventSummary'] = NoneDefaultingDict()
108
110 """
111 A subscription to a signal that produces notifications in the form of
112 actions.
113 """
114 implements(IGloballyIdentifiable)
115
116 _id = "NotificationSubscription"
117 meta_type = _id
118
119 enabled = False
120 action = 'email'
121 send_clear = False
122
123 delay_seconds = 0
124 repeat_seconds = 0
125 send_initial_occurrence = True
126
127
128
129
130
131 recipients = []
132
133
134 subscriptions = []
135
136 _properties = ZenModelRM._properties + (
137 {'id':'enabled', 'type':'boolean', 'mode':'w'},
138 {'id':'send_clear', 'type':'boolean', 'mode':'w'},
139 {'id':'send_initial_occurrence', 'type':'boolean', 'mode':'w'},
140 {'id':'delay_seconds', 'type':'int', 'mode':'w'},
141 {'id':'repeat_seconds', 'type':'int', 'mode':'w'},
142 )
143
144 _relations = (
145 ("adminRoles",
146 ToManyCont(
147 ToOne,
148 "Products.ZenModel.AdministrativeRole",
149 "managedObject"
150 )),
151 ("windows",
152 ToManyCont(
153 ToOne,
154 "Products.ZenModel.NotificationSubscriptionWindow",
155 "notificationSubscription"
156 )),
157 )
158
159 factory_type_information = (
160 {
161 'id' : _id,
162 'meta_type' : _id,
163 'description' : """Define the notification and the signals to
164 which it is subscribed.""",
165
166 'icon' : 'ActionRule.gif',
167 'product' : 'ZenEvents',
168 'factory' : 'manage_addNotificationSubscription',
169 'immediate_view' : 'editNotificationSubscription',
170 'actions' :(
171 {
172 'id' : 'edit',
173 'name' : 'Edit',
174 'action' : 'editNotificationSubscription',
175 'permissions' : (ZEN_CHANGE_ALERTING_RULES,)
176 }
177 )
178 },
179 )
180
181 security = ClassSecurityInfo()
182
183 - def __init__(self, id, title=None, buildRelations=True):
191
192 security.declareProtected(ZEN_CHANGE_ALERTING_RULES, 'manage_editNotificationSubscription')
196
198 """
199 Using maintenance windows and `enabled`, determine if this notification
200 is active for right now.
201 """
202 if self.enabled:
203 log.debug('Notification is enabled: %s' % self.id)
204 windows = self.windows()
205 if windows:
206 log.debug('Notification has (%s) windows.' % len(windows))
207
208 enabled_windows = []
209 for window in windows:
210 if window.enabled:
211 log.debug('Notification has enabled window: %s' % window.id)
212 enabled_windows.append(window)
213
214 if enabled_windows:
215 for window in enabled_windows:
216 if window.isActive():
217 log.debug('Window is active: %s' % window.id)
218 return True
219
220
221
222 return False
223
224
225
226 return True
227 else:
228 log.debug('Notification is enabled, but has no windows, it is active.')
229 return True
230 else:
231 log.debug('Notification NOT enabled: %s' % self.id)
232 return False
233
234 InitializeClass(NotificationSubscriptionManager)
235 InitializeClass(NotificationSubscription)
236