Package Products :: Package ZenEvents :: Module MySqlEventManager
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenEvents.MySqlEventManager

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 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):
30 '''make an MySqlEventManager''' 31 if not id: 32 id = "ZenEventManager" 33 if history: id = "ZenEventHistory" 34 evtmgr = MySqlEventManager(id, hostname=evthost, username=evtuser, 35 password=evtpass, database=evtdb, 36 port=evtport) 37 context._setObject(id, evtmgr) 38 evtmgr = context._getOb(id) 39 evtmgr.buildRelations() 40 try: 41 evtmgr.manage_refreshConversions() 42 except: 43 log.warn("Failed to refresh conversions, db connection failed.") 44 if history: 45 evtmgr.defaultOrderby="%s desc" % evtmgr.lastTimeField 46 evtmgr.timeout = 300 47 evtmgr.statusTable = "history" 48 evtmgr.installIntoPortal() 49 if REQUEST: 50 REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
51 52 53 addMySqlEventManager = DTMLFile('dtml/addMySqlEventManager',globals()) 54 55
56 -class MySqlEventManager(MySqlSendEventMixin, EventManagerBase):
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 #escape any % in the where clause because of format eval later 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
130 - def countEventsSince(self, since):
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