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
144 """Return list of fields used in format.
145 """
146 result = Set()
147 result.update(re.findall("%\((\S+)\)s", self.format))
148 result.update(re.findall("%\((\S+)\)s", self.body))
149 result.update([ f for f in re.findall("%\((\S+)\)s", self.clearFormat) \
150 if not f.startswith('clear') ])
151 result.update([ f for f in re.findall("%\((\S+)\)s", self.clearBody) \
152 if not f.startswith('clear') ])
153 result.update(map(_downcase, re.findall("%\(clear(\S+)\)s", self.clearFormat)))
154 result.update(map(_downcase, re.findall("%\(clear(\S+)\)s", self.clearBody)))
155 notDb = Set('orEventSummary eventUrl eventsUrl ackUrl deleteUrl undeleteUrl severityString'.split())
156 notMsg = ['severity', 'summary']
157 return list(result - notDb) + notMsg
158
159
168
169
179
180
182 """Return the user this action is for.
183 """
184 return self.getPrimaryParent()
185
187 """Return the userid this action is for.
188 """
189 return self.getUser().getId()
190
191
192 security.declareProtected(ZEN_CHANGE_ALERTING_RULES, 'manage_editActionRule')
212
213
221
222
224 """Clear state in alert_state before we are deleted.
225 """
226 zem = self.dmd.ZenEventManager
227 conn = zem.connect()
228 try:
229 delcmd = "delete from alert_state where %s" % self.sqlwhere()
230 log.debug("clear alert state '%s'", delcmd)
231 curs = conn.cursor()
232 curs.execute(delcmd)
233 finally: zem.close(conn)
234
236 """Return sql where to select alert_state data for this event.
237 """
238 return "userid = '%s' and rule = '%s'" % (self.getUserid(), self.id)
239
241 next = None
242 w = None
243 for ar in self.windows():
244 if next is None or ar.next() < next:
245 next = ar.next()
246 w = ar
247 return w
248
255
263
273
279
280 security.declareProtected(ZEN_CHANGE_ALERTING_RULES,
281 'manage_addActionRuleWindow')
293
294 security.declareProtected(ZEN_CHANGE_ALERTING_RULES,
295 'manage_deleteActionRuleWindow')
309
310
311 InitializeClass(ActionRule)
312