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