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

Source Code for Module 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 """ 66 Return a list of tuples with the CSS class, acknowledged count, count 67 68 [['zenevents_5', 0, 3], ...] 69 70 select severity, count(*), group_concat(eventState), 71 from status where device="win2k.confmon.loc" 72 and eventState < 2 group by severity desc; 73 """ 74 select = "select severity, count(*), group_concat(eventState) " 75 select += "from %s where " % self.statusTable 76 where = self._wand(where, "%s >= %s", self.severityField, severity) 77 where = self._wand(where, "%s <= %s", self.stateField, state) 78 if prodState is not None: 79 where = self._wand(where, "%s >= %s", 'prodState', prodState) 80 where = self.restrictedUserFilter(where) 81 select += where 82 select += " group by severity desc" 83 #print select 84 sevsum = self.checkCache(select) 85 if sevsum: return sevsum 86 zem = self.dmd.ZenEventManager 87 conn = zem.connect() 88 try: 89 curs = conn.cursor() 90 curs.execute(select) 91 sumdata = {} 92 for row in curs.fetchall(): 93 sev, count, acks = row[:3] 94 if hasattr(acks, 'tostring'): 95 acks = acks.tostring() 96 if type(acks) in types.StringTypes: 97 acks = acks.split(",") 98 ackcount = sum([int(n) for n in acks if n.strip()]) 99 sumdata[sev] = (ackcount, count) 100 sevsum = [] 101 for name, value in self.getSeverities(): 102 if value < severity: continue 103 css = self.getEventCssClass(value) 104 ackcount, count = sumdata.get(value, [0,0]) 105 sevsum.append([css, ackcount, int(count)]) 106 finally: zem.close(conn) 107 108 self.addToCache(select, sevsum) 109 self.cleanCache() 110 return sevsum
111
112 - def countEventsSince(self, since):
113 ''' since is number of seconds since epoch, see documentation 114 for python time.time() 115 ''' 116 count = 0 117 zem = self.dmd.ZenEventManager 118 conn = zem.connect() 119 try: 120 curs = conn.cursor() 121 for table in ('status', 'history'): 122 curs.execute('select count(*) from %s where firstTime >= %s' % 123 (table, since)) 124 count += curs.fetchall()[0][0] 125 finally: zem.close(conn) 126 return count
127 128 InitializeClass(MySqlEventManager) 129