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

# Source Code for Module Products.ZenEvents.browser.EventPillsAndSummaries

      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 re
     15
     16  from Products.Five.browser import BrowserView
     17  from Products.ZenUtils.jsonutils import json
     18  from Products.ZenModel.DeviceOrganizer import DeviceOrganizer
     19
     20 -class SinglePill(BrowserView):
     21 -    def __call__(self):
     22          """
     23          Gets event pill for worst severity.
     24
     25          @return: HTML that will render the event pill.
     26          @rtype: str
     27          """
     28          zem = self.context.dmd.ZenEventManager
     29          pill = getEventPillME(zem, self.context)
     30          if type(pill)==type([]) and len(pill)==1: return pill[0]
     31          return pill
     32
     33
     34 -class ObjectsEventSummary(BrowserView):
     35      """
     36      Return an HTML link and event pill for each object passed as a JSON
     37      object ready for inclusion in a YUI data table.
     38
     39      @param objects: The objects for which to create links and pills.
     40      @type objects: list
     41      @return: A JSON-formatted string representation of the columns and rows
     42          of the table
     43      @rtype: string
     44      """
     45      @json
     46 -    def __call__(self):
     47          zem = self.context.dmd.ZenEventManager
     48          obs = self._getObs()
     49          return getDashboardObjectsEventSummary(zem, obs)
     50
     51 -    def _getObs(self):
     52          raise NotImplementedError
     53
     54
     55 -class SubOrganizersEventSummary(ObjectsEventSummary):
     56 -    def _getObs(self):
     57          return self.context.children()
     58
     59
     60 -class SubDevicesEventSummary(ObjectsEventSummary):
     61 -    def _getObs(self):
     62          return self.context.devices()
     63
     64
     65 -class SingleObjectEventSummary(ObjectsEventSummary):
     66 -    def _getObs(self):
     67          return [self]
     68
     69
     70 -def getObjectsEventSummary(zem, objects, prodState=None, REQUEST=None):
     71      """
     72      Return an HTML link and event pill for each object passed as a JSON
     73      object ready for inclusion in a YUI data table.
     74
     75      @param objects: The objects for which to create links and pills.
     76      @type objects: list
     77      @return: A JSON-formatted string representation of the columns and rows
     78          of the table
     79      @rtype: string
     80      """
     81      mydict = {'columns':[], 'data':[]}
     82      mydict['columns'] = ['Object', 'Events']
     83      getcolor = re.compile(r'class=\"evpill-(.*?)\"', re.S|re.I|re.M).search
     84      colors = "red orange yellow blue grey green".split()
     85      def pillcompare(a,b):
     86          a, b = map(lambda x:getcolor(x[1]), (a, b))
     87          def getindex(x):
     88              try:
     89                  color = x.groups()[0]
     90                  smallcolor = x.groups()[0].replace('-acked','')
     91                  isacked = 'acked' in color
     92                  index = colors.index(x.groups()[0].replace('-acked',''))
     93                  if isacked: index += .5
     94                  return index
     95              except: return 5
     96          a, b = map(getindex, (a, b))
     97          return cmp(a, b)
     98      devdata = []
     99      for obj in objects:
    100          alink = obj.getPrettyLink()
    101          pill = getEventPillME(zem, obj, showGreen=True, prodState=prodState)
    102          if type(pill)==type([]): pill = pill[0]
    103          devdata.append([alink, pill])
    104      devdata.sort(pillcompare)
    105      mydict['data'] = [{'Object':x[0],'Events':x[1]} for x in devdata]
    106      return mydict
    107
    108
    109 -def getDashboardObjectsEventSummary(zem, objects, REQUEST=None):
    110      """
    111      Event summary that takes dashboard production state threshold into account.
    112      """
    113      thold = zem.dmd.prodStateDashboardThresh
    114      return getObjectsEventSummary(zem, objects, thold, REQUEST)
    115
    116
    117 -def _getPill(summary, url=None, number=3):
    118      iconTemplate = """
    119          <td class="severity-icon-small
    120              %(severity)s %(noevents)s">
    121              %(count)s
    122          </td>
    123      """
    124      rainbowTemplate = """
    125      <table onclick="location.href='%(url)s';"
    126          class="eventrainbow eventrainbow_cols_%(number)s">
    127          <tr>%(cells)s</tr>
    128      </table>
    129      """
    130      stati = "critical error warning info debug".split()
    131      summary =[x[2] for x in summary]
    132
    133      cells = []
    134      for i, count in enumerate(summary[:number]):
    135          cells.append(iconTemplate % {
    136              'noevents': '' if count else 'no-events',
    137              'severity': stati[i],
    138              'count':count
    139          })
    140      return rainbowTemplate % {
    141          'url': url,
    142          'cells': ''.join(cells),
    143          'number': number
    144      }
    145
    146 -def getEventPill(zem, where, number=3, minSeverity=0, showGreen=True,
    147                   prodState=None, url=None):
    148      summary = zem.getEventSummary(where, minSeverity, prodState)
    149      return _getPill(summary, url, number)
    150
    151 -def getEventPillME(zem, me, number=3, minSeverity=0, showGreen=True,
    152                     prodState=None):
    153      """
    154      Get HTML code displaying the maximum event severity and the number of
    155      events of that severity on a particular L{ManagedEntity} in a pleasing
    156      pill-shaped container. Optionally return pills for lesser severities as
    157      well. Optionally return a green pill if there are no events (normally no
    158      events in a severity will not yield a result).
    159
    160      @param zem: A reference to ZenEventManager
    161      @type zem: L{Products.ZenEvents.EventManagerBase.EventManagerBase}
    162      @param me: The object regarding which event data should be queried.
    163      @type me: L{ManagedEntity}
    164      @param number: The number of pills to return
    165      @type number: int
    166      @param showGreen: Whether to return an empty green pill if all is well
    167      @type showGreen: bool
    168      @return: HTML strings ready for template inclusion
    169      @rtype: list
    170      """
    171      evsum = zem.getEventSummaryME(me, minSeverity, prodState=prodState)
    172      url = getEventsURL(me)
    173      return _getPill(evsum, url, number)
    174
    175
    176  organizerTypes = {
    177      'Devices': 'devices',
    178      'Groups': 'groups',
    179      'Locations': 'locs',
    180      'Systems': 'systems'
    181  }
    182
    183
    184 -def getEventsURL(me) :
    185      from Products.ZenModel.Device import Device
    186      if isinstance(me, DeviceOrganizer) :
    187          path = me.getPrimaryPath()
    188          url = ('/zport/dmd/itinfrastructure?filter=default#'
    189                 '%s:%s:events_grid' % (
    190                     organizerTypes[path[3]], '.'.join(path)))
    191      elif isinstance(me, Device) :
    192          url = (me.getPrimaryUrlPath() +
    193                 '/devicedetail?filter=default#'
    194                 'deviceDetailNav:device_events')
    195      else:
    196          url = me.getPrimaryUrlPath()+'/viewEvents?filter=default'
    197      return url
    198