1
2
3
4
5
6
7
8
9
10
11
12
13 import time
14 import re
15 from sets import Set
16 import logging
17 log = logging.getLogger("zen.ActionRule")
18
19 from Globals import InitializeClass
20 from Globals import DTMLFile
21 from AccessControl import ClassSecurityInfo
22 from Products.ZenModel.ZenossSecurity import *
23 from Acquisition import aq_parent
24
25 from Products.ZenModel.ZenModelRM import ZenModelRM
26 from Products.ZenRelations.RelSchema import *
27 from Products.ZenUtils import Time
28 from Products.ZenEvents.EventFilter import EventFilter
29 from Products.ZenWidgets import messaging
30
31 from ActionRuleWindow import ActionRuleWindow
33 return s[0:1].lower() + s[1:]
34
41
42 addActionRule = DTMLFile('dtml/addActionRule',globals())
43
45 """
46 Rule applied to events that then executes an action on matching events.
47 """
48
49 meta_type = "ActionRule"
50
51 where = "severity >= 4 and eventState = 0 and prodState = 1000"
52 delay = 0
53 repeatTime = 0
54 action = "email"
55 format = "[zenoss] %(device)s %(summary)s"
56 body = "Device: %(device)s\n" \
57 "Component: %(component)s\n" \
58 "Severity: %(severityString)s\n" \
59 "Time: %(firstTime)s\n" \
60 "Message:\n%(message)s\n" \
61 "<a href=\"%(eventUrl)s\">Event Detail</a>\n" \
62 "<a href=\"%(ackUrl)s\">Acknowledge</a>\n" \
63 "<a href=\"%(deleteUrl)s\">Delete</a>\n" \
64 "<a href=\"%(eventsUrl)s\">Device Events</a>\n"
65 sendClear = True
66 clearFormat = "[zenoss] CLEAR: %(device)s %(clearOrEventSummary)s"
67 clearBody = \
68 "Event: '%(summary)s'\n" \
69 "Cleared by: '%(clearSummary)s'\n" \
70 "At: %(clearFirstTime)s\n" \
71 "Device: %(device)s\n" \
72 "Component: %(component)s\n" \
73 "Severity: %(severityString)s\n" \
74 "Message:\n%(message)s\n" \
75 "<a href=\"%(undeleteUrl)s\">Undelete</a>\n"
76 enabled = False
77 actionTypes = ("page", "email")
78 targetAddr = ""
79 plainText = False
80
81 _properties = ZenModelRM._properties + (
82 {'id':'where', 'type':'text', 'mode':'w'},
83 {'id':'format', 'type':'text', 'mode':'w'},
84 {'id':'body', 'type':'text', 'mode':'w'},
85 {'id':'sendClear', 'type':'boolean', 'mode':'w'},
86 {'id':'clearFormat', 'type':'text', 'mode':'w'},
87 {'id':'clearBody', 'type':'text', 'mode':'w'},
88 {'id':'delay', 'type':'int', 'mode':'w'},
89 {'id':'action', 'type':'selection', 'mode':'w',
90 'select_variable': 'actionTypes',},
91 {'id':'enabled', 'type':'boolean', 'mode':'w'},
92 {'id':'targetAddr', 'type':'string', 'mode':'w'},
93 {'id':'repeatTime', 'type':'int', 'mode':'w'},
94 {'id':'plainText', 'type':'boolean', 'mode':'w'},
95 )
96
97 _relations = (
98 ("windows", ToManyCont(ToOne,"Products.ZenEvents.ActionRuleWindow","actionRule")),
99 )
100
101 factory_type_information = (
102 {
103 'id' : 'ActionRule',
104 'meta_type' : 'ActionRule',
105 'description' : """Define action taken against events""",
106 'icon' : 'ActionRule.gif',
107 'product' : 'ZenEvents',
108 'factory' : 'manage_addActionRule',
109 'immediate_view' : 'editActionRule',
110 'actions' :
111 (
112 { 'id' : 'edit'
113 , 'name' : 'Edit'
114 , 'action' : 'editActionRule'
115 , 'permissions' : (ZEN_CHANGE_ALERTING_RULES,)
116 },
117 { 'id' : 'message'
118 , 'name' : 'Message'
119 , 'action' : 'editActionRuleMessage'
120 , 'permissions' : (ZEN_CHANGE_ALERTING_RULES,)
121 },
122 { 'id' : 'schedule'
123 , 'name' : 'Schedule'
124 , 'action' : 'editActionRuleSchedule'
125 , 'permissions' : (ZEN_CHANGE_ALERTING_RULES,)
126 },
127 )
128 },
129 )
130
131 security = ClassSecurityInfo()
132
133
135 """Return the breadcrumb links for this object add ActionRules list.
136 [('url','id'), ...]
137 """
138 crumbs = super(ActionRule, self).breadCrumbs(terminator)
139 url = aq_parent(self).absolute_url_path() + "/editActionRules"
140 crumbs.insert(-1,(url,'Alerting Rules'))
141 return crumbs
142
143
161
162
171
172
182
183
185 """Return the user this action is for.
186 """
187 return self.getPrimaryParent()
188
190 """Return the userid this action is for.
191 """
192 return self.getUser().getId()
193
194
195 security.declareProtected(ZEN_CHANGE_ALERTING_RULES, 'manage_editActionRule')
215
216
218 """Clear state in alert_state before we are deleted.
219 """
220 zem = self.dmd.ZenEventManager
221 conn = zem.connect()
222 try:
223 delcmd = "delete from alert_state where %s" % self.sqlwhere()
224 log.debug("clear alert state '%s'", delcmd)
225 curs = conn.cursor()
226 curs.execute(delcmd)
227 finally: zem.close(conn)
228
230 """Return sql where to select alert_state data for this event.
231 """
232 return "userid = '%s' and rule = '%s'" % (self.getUserid(), self.id)
233
235 next = None
236 w = None
237 for ar in self.windows():
238 if next is None or ar.next() < next:
239 next = ar.next()
240 w = ar
241 return w
242
249
257
267
273
274 security.declareProtected(ZEN_CHANGE_ALERTING_RULES,
275 'manage_addActionRuleWindow')
287
288 security.declareProtected(ZEN_CHANGE_ALERTING_RULES,
289 'manage_deleteActionRuleWindow')
303
304
305 InitializeClass(ActionRule)
306