Package Products :: Package ZenModel :: Module EventView |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.EventView

      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 or (at your
      8  # option) any later version as published by the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13  import logging
     14  log = logging.getLogger("zen.EventView")
     15
     16  from decorator import decorator
     17  from copy import deepcopy
     18  from AccessControl import ClassSecurityInfo, getSecurityManager
     19  from Globals import InitializeClass
     20  from zope.interface import Interface, implements
     21
     22  from Products.Zuul import getFacade
     23  from Products.ZenUtils.guid.interfaces import IGlobalIdentifier
     24  from Products.ZenWidgets import messaging
     25  from zenoss.protocols.services import ServiceResponseError
     26  from zenoss.protocols.services.zep import ZepConnectionError
     27  from zenoss.protocols.protobufs.zep_pb2 import (STATUS_NEW, STATUS_ACKNOWLEDGED, SEVERITY_CRITICAL,
     28                                                  SEVERITY_ERROR, SEVERITY_WARNING, SEVERITY_INFO,
     29                                                  SEVERITY_DEBUG)
     30
     31 -class IEventView(Interface):
     32      """
     33      Marker interface for objects which have event views.
     34      """
     35
     36 -def zepConnectionError(retval=None):
     37      def outer(func):
     38          def inner(func, self, *args, **kwargs):
     39              try:
     40                  return func(self, *args, **kwargs)
     41              except ZepConnectionError, e:
     42                  msg = 'Connection refused. Check zeneventserver status on <a href="/zport/About/zenossInfo">Daemons</a>'
     43                  messaging.IMessageSender(self).sendToBrowser("ZEP connection error",
     44                                                          msg,
     45                                                          priority=messaging.CRITICAL,
     46                                                          sticky=True)
     47                  log.warn("Could not connect to ZEP")
     48              return deepcopy(retval)    # don't return the mutable retval
     49          return decorator(inner, func)  # for URL's through Zope we must use the same arguments as the original function
     50      return outer
     51
     52 -class EventView(object):
     53      """
     54      This class provides convenience methods for retrieving events to any subclass. Note that
     55      this class is currently transitioning between the old event system and ZEP. Most of the methods
     56      are marked as deprecated except those that go through ZEP.
     57
     58      """
     59      implements(IEventView)
     60
     61      security = ClassSecurityInfo()
     62
     63 -    def getEventManager(self, table='status'):
     64          """Return the current event manager for this object.
     65          """
     66          if table=='history':
     67              return self.ZenEventHistory
     68          return self.ZenEventManager
     69
     70 -    def getStatusString(self, statclass, **kwargs):
     71          """Return the status number for this device of class statClass.
     72          """
     73          # used to avoid pychecker complaint about wrong # of args to getStatus
     74          f = self.getStatus
     75          return self.convertStatus(f(statclass, **kwargs))
     76
     77      @zepConnectionError(())
     78 -    def getEventSummary(self, severity=1, state=1, prodState=None):
     79          """Return an event summary list for this managed entity.
     80          """
     81          zep = getFacade('zep')
     82          sevsum = []
     83          try:
     84              # Event class rainbows show all events through DEBUG severity
     85              sevs = (SEVERITY_CRITICAL,SEVERITY_ERROR,SEVERITY_WARNING,SEVERITY_INFO,SEVERITY_DEBUG)
     86              severities = zep.getEventSeveritiesByUuid(self.getUUID(), severities=sevs)
     87              getCssClass = self.getEventManager().getEventCssClass
     88              for sev in sorted(severities.keys(), reverse=True):
     89                  if sev < severity:
     90                      continue
     91                  counts = severities[sev]
     92                  count = counts.get('count', 0)
     93                  acked = counts.get('acknowledged_count', 0)
     94                  sevsum.append([getCssClass(sev), acked, count])
     95          except TypeError, e:
     96              log.warn("Attempted to query events for %r which does not have a uuid" % self)
     97          return sevsum
     98
     99 -    def getStatusImgSrc(self, status):
    100          ''' Return the image source for a status number
    101          '''
    102          return self.getEventManager().getStatusImgSrc(status)
    103
    104 -    def getStatusCssClass(self, status):
    105          """Return the css class for a status number.
    106          """
    107          return self.getEventManager().getStatusCssClass(status)
    108
    109 -    def _getCurrentUserName(self):
    110          return getSecurityManager().getUser().getId()
    111
    112 -    def _redirectToEventConsole(self, msg, REQUEST=None):
    113          messaging.IMessageSender(self).sendToBrowser("Events",
    114                                                       msg,
    115                                                       priority=messaging.INFO)
    116          if REQUEST:
    117              dest = '/zport/dmd/Events/evconsole'
    118              REQUEST['RESPONSE'].redirect(dest)
    119
    120      security.declareProtected('Manage Events','manage_ackEvents')
    121      @zepConnectionError()
    122 -    def manage_ackEvents(self, evids=(), REQUEST=None):
    123          """Set event state from this managed entity.
    124          """
    125          if not evids:
    126              self._redirectToEventConsole("No events to acknowledge", REQUEST)
    127              return
    128
    129          zep = getFacade('zep')
    130          if isinstance(evids, basestring):
    131              evids = [evids]
    132
    133          try:
    134              evids_filter = zep.createEventFilter(uuid=evids)
    135              zep.acknowledgeEventSummaries(eventFilter=evids_filter)
    136              self._redirectToEventConsole("Acknowledged events: %s" % ", ".join(evids), REQUEST)
    137          except ServiceResponseError, e:
    138              self._redirectToEventConsole("Error acknowledging events: %s" % str(e), REQUEST)
    139
    140      security.declareProtected('Manage Events','manage_deleteEvents')
    141      @zepConnectionError()
    142 -    def manage_deleteEvents(self, evids=(), REQUEST=None):
    143          """Delete events from this managed entity.
    144          """
    145          if not evids:
    146              self._redirectToEventConsole("No events to close", REQUEST)
    147              return
    148
    149          zep = getFacade('zep')
    150          if isinstance(evids, basestring):
    151              evids = [evids]
    152          try:
    153              evids_filter = zep.createEventFilter(uuid=evids)
    154              zep.closeEventSummaries(eventFilter=evids_filter)
    155              self._redirectToEventConsole("Closed events: %s" % ", ".join(evids), REQUEST)
    156          except ServiceResponseError, e:
    157              self._redirectToEventConsole("Error Closing events: %s" % str(e), REQUEST)
    158
    159      security.declareProtected('Manage Events','manage_undeleteEvents')
    160      @zepConnectionError()
    161 -    def manage_undeleteEvents(self, evids=(), REQUEST=None):
    162          """Delete events from this managed entity.
    163          """
    164          if not evids:
    165              self._redirectToEventConsole("No events to reopen", REQUEST)
    166              return
    167
    168          zep = getFacade('zep')
    169          if isinstance(evids, basestring):
    170              evids = [evids]
    171          try:
    172              evids_filter = zep.createEventFilter(uuid=evids)
    173              zep.reopenEventSummaries(eventFilter=evids_filter)
    174              self._redirectToEventConsole("Reopened events: %s" % ", ".join(evids), REQUEST)
    175          except ServiceResponseError, e:
    176              self._redirectToEventConsole("Error Reopening events: %s" % str(e), REQUEST)
    177
    178      @zepConnectionError(0)
    179 -    def getStatus(self, statusclass=None, **kwargs):
    180          """
    181          Return the status number for this device of class statClass.
    182          """
    183          zep = getFacade('zep')
    184          try:
    185              event_filter = zep.createEventFilter(tags=[self.getUUID()],
    186                                                   severity=[SEVERITY_WARNING,SEVERITY_ERROR,SEVERITY_CRITICAL],
    187                                                   status=[STATUS_NEW,STATUS_ACKNOWLEDGED],
    188                                                   event_class=filter(None, [statusclass]))
    189          except TypeError, e:
    190              log.warn("Attempted to query events for %r which does not have a uuid" % self)
    191              return 0
    192          result = zep.getEventSummaries(0, filter=event_filter, limit=0)
    193          return int(result['total'])
    194
    195 -    def getUUID(self):
    196          return IGlobalIdentifier(self).getGUID()
    197
    198      @zepConnectionError({})
    199 -    def getEventSeveritiesCount(self):
    200          """
    201          Uses the zep facade to return a list of
    202          event summaries for this entity
    203          """
    204          zep = getFacade('zep')
    205          try:
    206              # Event class rainbows show all events through DEBUG severity
    207              sevs = (SEVERITY_CRITICAL,SEVERITY_ERROR,SEVERITY_WARNING,SEVERITY_INFO,SEVERITY_DEBUG)
    208              severities = zep.getEventSeveritiesByUuid(self.getUUID(), severities=sevs)
    209          except TypeError, e:
    210              log.warn("Attempted to query events for %r which does not have a uuid" % self)
    211              return {}
    212          results = dict((zep.getSeverityName(sev).lower(), counts) for (sev, counts) in severities.iteritems())
    213          return results
    214
    215      @zepConnectionError(0)
    216 -    def getWorstEventSeverity(self):
    217          """
    218          Uses Zep to return the worst severity for this object
    219          """
    220          zep = getFacade('zep')
    221          try:
    222              result =  zep.getWorstSeverityByUuid(self.getUUID())
    223          except TypeError, e:
    224              log.warn("Attempted to query events for %r which does not have a uuid" % self)
    225              result = 0
    226          return result
    227
    228  InitializeClass(EventView)
    229