1
2
3
4
5
6
7
8
9
10
11
12
13
14 import types
15 import logging
16 log = logging.getLogger("zen.Events")
17
18 from Globals import InitializeClass
19 from Globals import DTMLFile
20 from AccessControl import ClassSecurityInfo
21
22 from EventManagerBase import EventManagerBase
23 from MySqlSendEvent import MySqlSendEventMixin
24 from Exceptions import *
25
26 -def manage_addMySqlEventManager(context, id=None, evthost="localhost",
27 evtuser="root", evtpass="", evtdb="events",
28 evtport=3306,
29 history=False, REQUEST=None):
51
52
53 addMySqlEventManager = DTMLFile('dtml/addMySqlEventManager',globals())
54
55
57
58 portal_type = meta_type = 'MySqlEventManager'
59
60 backend = "mysql"
61
62 security = ClassSecurityInfo()
63
64 - def getEventSummary(self, where="", severity=1, state=1, prodState=None,
65 parameterizedWhere=None):
66 """
67 Return a list of tuples with the CSS class, acknowledged count, count
68
69 [['zenevents_5', 0, 3], ...]
70
71 select severity, sum(eventState) as ack_events, count(*) as total_events,
72 from status where device="win2k.confmon.loc"
73 and eventState < 2 group by severity desc;
74 """
75 select = (
76 "select severity, sum(eventState) as ack_events, count(*) as total_events "
77 "from %s where " % self.statusTable
78 )
79
80 paramValues = []
81 def paramWhereAnd(where, fmt, field, value):
82 log.debug("where is %s" % where)
83 if value != None and where.find(field) == -1:
84 if where: where += " and "
85 where += fmt % (field,)
86 paramValues.append(value)
87 return where
88 where = self.restrictedUserFilter(where)
89
90 where = where.replace('%', '%%')
91 if parameterizedWhere is not None:
92 pwhere, pvals = parameterizedWhere
93 if where: where += " and "
94 where += pwhere
95 paramValues.extend(pvals)
96 where = paramWhereAnd(where, "%s >= %%s", self.severityField, severity)
97 where = paramWhereAnd(where, "%s <= %%s", self.stateField, state)
98 if prodState is not None:
99 where = paramWhereAnd(where, "%s >= %%s", 'prodState', prodState)
100 select += where
101 select += " group by severity desc"
102
103 cacheKey = select + str(paramValues)
104 sevsum = self.checkCache(cacheKey)
105 if sevsum:
106 return sevsum
107
108 zem = self.dmd.ZenEventManager
109 conn = zem.connect()
110 try:
111 curs = conn.cursor()
112 curs.execute(select, paramValues)
113
114 sumdata = dict(( (int(r[0]), (int(r[1]), int(r[2]))) for r in curs.fetchall() ))
115
116 sevsum = []
117 for name, value in self.getSeverities():
118 if value < severity:
119 continue
120 css = self.getEventCssClass(value)
121 ackcount, count = sumdata.get(value, (0, 0))
122 sevsum.append([css, ackcount, count])
123 finally:
124 zem.close(conn)
125
126 self.addToCache(cacheKey, sevsum)
127 self.cleanCache()
128 return sevsum
129
131 ''' since is number of seconds since epoch, see documentation
132 for python time.time()
133 '''
134 count = 0
135 zem = self.dmd.ZenEventManager
136 conn = zem.connect()
137 try:
138 curs = conn.cursor()
139 for table in ('status', 'history'):
140 curs.execute('select count(*) from %s where firstTime >= %s' %
141 (table, since))
142 count += curs.fetchall()[0][0]
143 finally: zem.close(conn)
144 return count
145
146 InitializeClass(MySqlEventManager)
147