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

# Source Code for Module Products.ZenModel.EventView

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